fuzzy_types Reference¶
Config¶
- fuzzy_types.configuration.get_config(name: str, config_file: Optional[str] = None, allow_user: bool = True, user_path: Optional[str] = None, config_envvar: Optional[str] = None, merge_mode: str = 'update') → dict[source]¶
Returns a configuration dictionary.
The configuration dictionary is created by merging the default configuration file that is part of the library (normally in
etc/<name>.yml
) with a user configuration file. The path to the user configuration file can be defined as an environment variable to be passed to this function inconfig_envvar
or as a path inuser_path
. The environment variable, if exists, always takes precedence.- Parameters
name (str) – The name of the package.
config_file (str) – The path to the configuration file. If
None
, defaults toetc/<name>.yml
relative to the file that calledget_config
.allow_user (bool) – If
True
, looks for an user configuration file and merges is to the default configuration. Otherwise it returns just the default configuration.user_path (str) – The path to the user configuration file. Defaults to
~/.config/<name>/<name>.yml
. Ignored if the file does not exist.config_envvar (str) – The environment variable that contains the path to the user configuration file. Defaults to
<name>_CONFIG_PATH
. If the environment variable exists, theuser_path
is ignored.merge_mode (str) – Defines how the default and user dictionaries will be merged. If
update
, the user dictionary will be used to update the default configuration. Ifreplace
, only the user configuration will be returned.
- Returns
config (dict) – A dictionary containing the configuration.
- fuzzy_types.configuration.merge_config(user: dict, default: dict) → dict[source]¶
Merges a user configuration with the default one.
- fuzzy_types.configuration.read_yaml_file(path: Union[str, pathlib.Path]) → dict[source]¶
Read a YAML file and returns a dictionary.
Fuzzy Types¶
- class fuzzy_types.fuzzy.FuzzyBase(the_items: Union[list, dict], use_fuzzy: Optional[Callable] = None, dottable: bool = True)[source]¶
Bases:
abc.ABC
Abstract Base Class for all Fuzzy objects
- copy() → Union[fuzzy_types.fuzzy.FL, fuzzy_types.fuzzy.FD, fuzzy_types.fuzzy.FOD, fuzzy_types.fuzzy.FS][source]¶
Returns a copy of the fuzzy instance
- Returns
Union[list, dict] – A copy of the fuzzy instance
- static mapper(item: Union[str, object]) → str[source]¶
Mapper between list/dict item and rapidfuzz choices
Static method used to map a list’s items or dict’s keys to a string representation used by
rapidfuzz
for. By default returns an explicit string case of the item. To see the output, view thechoices
property. Can be overridden to customize what is input intorapidfuzz
.
- abstract property choices¶
- class fuzzy_types.fuzzy.FuzzyBaseDict(the_dict: dict, use_fuzzy: Optional[Callable] = None, dottable: bool = True)[source]¶
Bases:
fuzzy_types.fuzzy.FuzzyBase
- property choices: list¶
A list of choices used during fuzzy matching
The list of choices passes into
rapidfuzz
to use for fuzzy-matching a string. The list of choices is computed by iterating over the dictionary keys, passing each item through themapper
method.- Returns
list – The list of options used by
rapidfuzz
when fuzzy matching
- class fuzzy_types.fuzzy.FuzzyDict(the_dict: dict, use_fuzzy: Optional[Callable] = None, dottable: bool = True)[source]¶
Bases:
fuzzy_types.fuzzy.FuzzyBaseDict
,dict
A dotable dictionary that uses rapidfuzz to select the key.
- Parameters
the_items (dict) – A dictionary of items to make fuzzy
use_fuzzy (Callable) – The function used to perform the fuzzy-matching. Default is
fuzzy_types.utils.get_best_fuzzy()
.dottable (bool) – If False, turns off dottable attributes. Default is True.
- Returns
A python dictionary with fuzzy keys
- class fuzzy_types.fuzzy.FuzzyList(the_items: Union[list, dict], use_fuzzy: Optional[Callable] = None, dottable: bool = True)[source]¶
Bases:
fuzzy_types.fuzzy.FuzzyBase
,list
A dottable python list that uses rapidfuzz to select a string item
- Parameters
the_items (list) – A list of items to make fuzzy
use_fuzzy (Callable) – The function used to perform the fuzzy-matching. Default is
fuzzy_types.utils.get_best_fuzzy()
.dottable (bool) – If False, turns off dottable attributes. Default is True.
- Returns
A python list with fuzzy items
- property choices: list¶
A list of choices used during fuzzy matching
The list of choices passes into
rapidfuzz
to use for fuzzy-matching a string. The list of choices is computed by iterating over the list items, passing each item through themapper
method.- Returns
list – The list of options used by
rapidfuzz
when fuzzy matching
- class fuzzy_types.fuzzy.FuzzyOrderedDict(the_dict: dict, use_fuzzy: Optional[Callable] = None, dottable: bool = True)[source]¶
Bases:
fuzzy_types.fuzzy.FuzzyBaseDict
,collections.OrderedDict
A dotable ordered dictionary that uses rapidfuzz to select the key.
- Parameters
the_items (dict) – A dictionary of items to make fuzzy
use_fuzzy (Callable) – The function used to perform the fuzzy-matching. Default is
fuzzy_types.utils.get_best_fuzzy()
.dottable (bool) – If False, turns off dottable attributes. Default is True.
- Returns
A python ordered dictionary with fuzzy keys
- class fuzzy_types.fuzzy.FuzzyStr(the_string: str, use_fuzzy: Optional[Callable] = None)[source]¶
Bases:
str
A fuzzy string that uses rapidfuzz for equality checks
- Parameters
the_string (str) – A string to make fuzzy
use_fuzzy (Callable) – The function used to perform the fuzzy-matching. Default is
fuzzy_types.utils.get_best_fuzzy()
.
Helpers¶
- fuzzy_types.utils.get_best_fuzzy(value: str, choices: list, min_score: Optional[int] = None, scorer: Callable = <cyfunction WRatio>, return_score: bool = False) → Union[None, str][source]¶
Returns the best match in a list of choices using rapidfuzz.
- Parameters
value (str) – A string to match on
choices (list) – A list of string choices to match from
min_score (int) – The score cutoff threshold. The minimum score to consider when matching. By default, None
scorer (Callable) – The rapidfuzz score ratio to use. By default, WRatio.
return_score (bool) – If True, also returns the score value of the match. By default, False.
- Returns
Union[None, str] – Either None if no matches found above score, or the best match from list of choices
- Raises
ValueError – when rapidfuzz cannot find a single best match