Utilities
These are miscellaneous utilities included in Open Mafia Engine.
open_mafia_engine.util.matcher
FuzzyMatcher
Fuzzy matcher, with a score cutoff, using fuzzywuzzy
.
Parameters
choices : Dict[str, T]
Mapping of name to whatever you want.
score_cutoff : int
The minimum score to use.
Default is 0, in which case we use the best match, even if it's bad.
If set higher, match()
may raise KeyError.
use_lower : bool
Whether to use str.lower() for keys and queries.
This probably improves matching in real-world cases.
Default is True.
match(self, query: str) -> ~T
Tries to match query
to your object.
Source code in util/matcher.py
def match(self, query: str) -> T:
if self.use_lower:
query = query.lower()
options = list(self._choices.keys())
# see: https://github.com/seatgeek/fuzzywuzzy
res = process.extractOne(query, options, score_cutoff=self.score_cutoff)
if res is None:
raise KeyError(query)
return self._choices[res[0]]
Matcher
Base class for target matching.
The base version only does exact matches, and throws KeyError if not found.
(Essentially, just a Dict[str, T]
)
match(self, query: str) -> ~T
Tries to match query
to your object.
Source code in util/matcher.py
def match(self, query: str) -> T:
"""Tries to match `query` to your object."""
return self._choices[query]
MatcherLowercase
Matches everything by query.lower()
.
match(self, query: str) -> ~T
Tries to match query
to your object.
Source code in util/matcher.py
def match(self, query: str) -> T:
return self._choices[query.lower()]
open_mafia_engine.util.enums
make_str_enum(name: str, values: List[str], doc: str = '') -> Type[str]
Makes a string enumeration with particuluar values.
Source code in util/enums.py
def make_str_enum(name: str, values: List[str], doc: str = "") -> Type[str]:
"""Makes a string enumeration with particuluar values."""
T = Enum(name, {v: v for v in values}, qualname=name, type=str)
def __repr__(self: T) -> str:
cn = type(self).__qualname__
return f"{cn}({self.value!r})"
T.__repr__ = __repr__
T.__doc__ = doc
return T
open_mafia_engine.util.namedlist
NamedList
Named list: works like a list and a dict based on the key.
open_mafia_engine.util.classes
class_name(cls: Type[object]) -> str
Returns the class name.
Source code in util/classes.py
def class_name(cls: Type[object]) -> str:
"""Returns the class name."""
return cls.__qualname__
open_mafia_engine.util.repr
ReprMixin
This mixin gives a reasonably smart automatic repr.
It hasn't been extensively tested - might fail, so test yourself!