Skip to content

Commit

Permalink
Implemented type sensitive AliasMapper
Browse files Browse the repository at this point in the history
  • Loading branch information
nfearnley committed May 21, 2024
1 parent aad5611 commit 55b32dd
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
14 changes: 7 additions & 7 deletions sizebot/lib/gender.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
from typing import Literal, get_args

from sizebot.lib import errors
from sizebot.lib.utils import AliasMapper

Gender = Literal["m", "f"]
GENDERS = get_args(Gender)

genders: dict[Gender, list[str]] = {
"m": ["m", "male", "man", "boy"],
"f": ["f", "female", "woman", "girl"]
}
gendermap: dict[str, Gender] = {alias: key for key, aliases in genders.items() for alias in aliases}
gendermap = AliasMapper[Gender]({
"m": ["male", "man", "boy"],
"f": ["female", "woman", "girl"]
})


def parse_gender(s: str) -> Gender:
gender = gendermap.get(s.lower(), None)
if gender is None or gender not in GENDERS:
if s not in gendermap:
raise errors.ArgumentException
gender = gendermap[s]
return gender
14 changes: 7 additions & 7 deletions sizebot/lib/unitsystem.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
from typing import Literal, get_args

from sizebot.lib import errors
from sizebot.lib.utils import AliasMapper

UnitSystem = Literal["m", "u"]
UNITSYSTEMS = get_args(UnitSystem)

systems: dict[UnitSystem, list[str]] = {
"m": ["m", "b", "e", "metric", "british", "europe", "european"],
"u": ["u", "i", "c", "a", "us", "imperial", "customary", "american"]
}
systemmap: dict[str, UnitSystem] = {alias: key for key, aliases in systems.items() for alias in aliases}
systemmap = AliasMapper[UnitSystem]({
"m": ["b", "e", "metric", "british", "europe", "european"],
"u": ["i", "c", "a", "us", "imperial", "customary", "american"]
})


def parse_unitsystem(s: str) -> UnitSystem:
unitsystem = systemmap.get(s.lower(), None)
if unitsystem is None or unitsystem not in UNITSYSTEMS:
if s not in systemmap:
raise errors.ArgumentException
unitsystem = systemmap[s]
return unitsystem
27 changes: 26 additions & 1 deletion sizebot/lib/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any
from typing import Any, Generic, TypeVar
from collections.abc import Iterable

import random
Expand Down Expand Up @@ -259,3 +259,28 @@ def randrange_log(minval: Decimal, maxval: Decimal, precision: int = 26) -> Deci
def round_fraction(number: Decimal, denominator: int) -> Decimal:
rounded = round(number * denominator) / denominator
return rounded


T = TypeVar("T", bound=str)
AliasList = dict[T, Iterable[str]]
AliasMap = dict[str, T]


class AliasMapper(Generic[T]):
def __init__(self, aliases: AliasList[T]):
self._map = map_aliases(aliases)

def __getitem__(self, key: str) -> T:
return self._map[key.lower()]

def __contains__(self, key: str) -> bool:
return key.lower() in self._map


def map_aliases[T: str](alias_dict: AliasList[T]) -> AliasMap[T]:
aliasmap: AliasMap[T] = {}
for key, aliases in alias_dict:
aliasmap[key.lower()] = key.lower()
for alias in aliases:
aliasmap[alias.lower()] = key.lower()
return aliasmap

0 comments on commit 55b32dd

Please sign in to comment.