mirror of
https://github.com/miaowware/qrm2.git
synced 2025-08-01 04:42:25 -04:00
convert Unit into a dataclass, move parse to converter (#257)
fixes #256
This commit is contained in:
parent
2ac13346d4
commit
77e14a109c
@ -11,6 +11,7 @@ the GNU General Public License, version 2.
|
|||||||
import math
|
import math
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
import discord.ext.commands as commands
|
import discord.ext.commands as commands
|
||||||
|
|
||||||
@ -21,41 +22,30 @@ from data import options as opt
|
|||||||
# not sure why but UnitConverter and Unit need to be defined before DbConvCog and convert()
|
# not sure why but UnitConverter and Unit need to be defined before DbConvCog and convert()
|
||||||
class UnitConverter(commands.Converter):
|
class UnitConverter(commands.Converter):
|
||||||
async def convert(self, ctx: commands.Context, argument: str):
|
async def convert(self, ctx: commands.Context, argument: str):
|
||||||
|
is_db = None
|
||||||
|
mult = None
|
||||||
|
unit = None
|
||||||
|
utype = None
|
||||||
try:
|
try:
|
||||||
return Unit(argument)
|
s = argument.lower()
|
||||||
except ValueError as e:
|
|
||||||
raise commands.BadArgument(message=str(e))
|
|
||||||
|
|
||||||
|
|
||||||
class Unit:
|
|
||||||
def __init__(self, raw: str):
|
|
||||||
self.raw: str = raw
|
|
||||||
self.unit: str
|
|
||||||
self.type: UnitType
|
|
||||||
self.is_db: bool
|
|
||||||
self.mult: int
|
|
||||||
self._parse()
|
|
||||||
|
|
||||||
def _parse(self):
|
|
||||||
s = self.raw.lower()
|
|
||||||
if len(s) > 2 and s[:2] == "db":
|
if len(s) > 2 and s[:2] == "db":
|
||||||
self.is_db = True
|
is_db = True
|
||||||
if s[2:] in units:
|
if s[2:] in units:
|
||||||
u = units[s[2:]]
|
u = units[s[2:]]
|
||||||
self.mult = u["mult"]
|
mult = u["mult"]
|
||||||
self.unit = u["log"]
|
unit = u["log"]
|
||||||
self.type = u["type"]
|
utype = u["type"]
|
||||||
elif s in units:
|
elif s in units:
|
||||||
self.is_db = False
|
is_db = False
|
||||||
u = units[s]
|
u = units[s]
|
||||||
self.mult = u["mult"]
|
mult = u["mult"]
|
||||||
self.unit = u["scalar"]
|
unit = u["scalar"]
|
||||||
self.type = u["type"]
|
utype = u["type"]
|
||||||
else:
|
else:
|
||||||
raise ValueError(f"Invalid unit: {self.raw}")
|
raise ValueError(f"Invalid unit: {argument}")
|
||||||
|
return Unit(argument, unit, utype, is_db, mult)
|
||||||
def __str__(self):
|
except ValueError as e:
|
||||||
return self.unit
|
raise commands.BadArgument(message=str(e))
|
||||||
|
|
||||||
|
|
||||||
class UnitType(Enum):
|
class UnitType(Enum):
|
||||||
@ -64,6 +54,15 @@ class UnitType(Enum):
|
|||||||
antenna = 3
|
antenna = 3
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Unit:
|
||||||
|
raw: str
|
||||||
|
unit: str
|
||||||
|
type: UnitType
|
||||||
|
is_db: bool
|
||||||
|
mult: int
|
||||||
|
|
||||||
|
|
||||||
class DbConvCog(commands.Cog):
|
class DbConvCog(commands.Cog):
|
||||||
def __init__(self, bot: commands.Bot):
|
def __init__(self, bot: commands.Bot):
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
@ -85,7 +84,7 @@ class DbConvCog(commands.Cog):
|
|||||||
if value is not None and unit_from is not None and unit_to is not None:
|
if value is not None and unit_from is not None and unit_to is not None:
|
||||||
converted = convert(value, unit_from, unit_to)
|
converted = convert(value, unit_from, unit_to)
|
||||||
|
|
||||||
embed.title = f"{value:.3g} {unit_from} = {converted:.3g} {unit_to}"
|
embed.title = f"{value:.3g} {unit_from.unit} = {converted:.3g} {unit_to.unit}"
|
||||||
embed.colour = cmn.colours.good
|
embed.colour = cmn.colours.good
|
||||||
else:
|
else:
|
||||||
embed.title = "Decibel Quick Reference"
|
embed.title = "Decibel Quick Reference"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user