tools.helpers.config_manager package¶
Submodules¶
tools.helpers.config_manager.config_conversion module¶
Functions to load/save configuration with typed values.
-
tools.helpers.config_manager.config_conversion.
convert_dict_from_str
(dico, allow_multiple=True, error='ignore', drop_none=False, drop_empty_iterable=False, ascendant=False, no_flag='ignore', inplace=False, duplicates='first', **parser_cfg)¶ Convert a dict of str (generated from a file for example) to a typed dict. Simple types that can be recognised: str, bool, int, float, list, tuple. Custom classes: Reference, Path Advanced types (with ‘auto’ flag): expressions with dict, set, bytes, None and simple types.
- Parameters
dico – dictionary-like object to convert with keys and values of type ‘str’.
allow_multiple – if True, multiple flags are allowed (applied from left to right)
error – behavior on casting error. Possible values: ‘ignore’ (returns the initial string), ‘drop’ (returns None), ‘error’ (raise an error if casting fails), ‘auto-conversion’ (try to convert automatically; if it fails, returns the initial string)
drop_none – if True, None values are dropped
drop_empty_iterable – if True, empty iterable objects (list, tuple) are dropped
ascendant – if True, the flags are applied from the last to the first
no_flag – behavior if no flag found. ‘ignore’, ‘error’, ‘drop’, ‘auto-conversion’
inplace – returns dico inplace
duplicates – behavior if duplicates found. ‘drop’, ‘first’, ‘last’, ‘error’.
parser_cfg – kwargs for _parse_key function
- Returns
dictionary-like object (same type as ‘dico’)
# Simple test
>>> test_dict = {"a": "without_flag", "@i-b": "1", "@f-c": "9.2", "@b-d": "", "@b-e": "5", "@b-f": "False"} >>> convert_dict_from_str(test_dict) {'a': 'without_flag', 'b': 1, 'c': 9.2, 'd': False, 'e': True, 'f': False}
# Numbers test
>>> num_dict = {"@f-d1": "1.6", "@i-d2": "1.7", "@f-@i-d3": "1.8", "@i-@f-d4": "1.9", "d5": 2.0, "@ftwsdc-d6": "2 252,9"} >>> convert_dict_from_str(num_dict) {'d1': 1.6, 'd2': '1.7', 'd3': 1, 'd4': 1.9, 'd5': 2.0, 'd6': 2252.9}
# Duplicates handling test
>>> dup_dict = OrderedDict([("@s-overwritten", "value1"), ("overwritten", "value2"), ("@auto-overwritten", "value3")]) >>> convert_dict_from_str(dup_dict, duplicates='rename') OrderedDict([('overwritten', 'value1'), ('overwritten_1', 'value2'), ('overwritten_2', 'value3')]) >>> convert_dict_from_str(dup_dict, duplicates='first') OrderedDict([('overwritten', 'value1')]) >>> convert_dict_from_str(dup_dict, duplicates='last') OrderedDict([('overwritten', 'value3')])
# Date test
>>> date_dict = {"@date-date": "2019-04-01", "@date-date2": "04-13-2018", "@date-date3": "13/04/2018"} >>> convert_dict_from_str(date_dict) {'date': Timestamp('2019-04-01 00:00:00'), 'date2': Timestamp('2018-04-13 00:00:00'), 'date3': Timestamp('2018-04-13 00:00:00')} >>> date_special_dict = {"@date-date_std": "04/11/2018", "@datedb-date_day_before": "04/11/2018"} >>> convert_dict_from_str(date_special_dict) {'date_std': Timestamp('2018-04-11 00:00:00'), 'date_day_before': Timestamp('2018-11-04 00:00:00')}
# List test
>>> list_dict = {"@auto-list1": "[18, 13]", "@l-list2": "[19, 13]", "@auto-list3": "[{(18, 13): 'a'}, 'end']"} >>> convert_dict_from_str(list_dict) {'list1': [18, 13], 'list2': ['19', '13'], 'list3': [{(18, 13): 'a'}, 'end']}
# Auto conversion test
>>> auto_dict = {"a": "True", "b": "False", "c": "None", "d": "[{(18, 13): 'a'}, 'end']", "e": '9.9', "f": 9.9} >>> convert_dict_from_str(auto_dict, no_flag="auto-conversion") {'a': True, 'b': False, 'c': None, 'd': [{(18, 13): 'a'}, 'end'], 'e': 9.9, 'f': 9.9}
-
tools.helpers.config_manager.config_conversion.
convert_dict_to_str
(dico, auto=True, inplace=False)¶ Convert a dico (dictionary-like object) to make it writable to a file as text and easily loadable with the previous types, using convert_dict_from_str function.
- Parameters
dico – dictionary-like object to convert. Flags are added to keys and values are casted to str.
auto – if True, keys corresponding to complex objects get the flag ‘auto’
inplace – modify dico itself
- Returns
dictionary-like object (same type as ‘dico’)
>>> dico = {'a': True, 'b': False, 'c': None, 'd': [{(18, 3.1): 'a'}, 'end'], 'e': 9.9} >>> convert_dict_to_str(dico) {'@b-a': 'True', '@b-b': 'False', '@auto-c': 'None', '@auto-d': "[{(18, 3.1): 'a'}, 'end']", '@f-e': '9.9'}
-
tools.helpers.config_manager.config_conversion.
to_bool
(v)¶
-
tools.helpers.config_manager.config_conversion.
to_char
(v)¶
-
tools.helpers.config_manager.config_conversion.
to_float
(v, thousand_sep=None, decimal_sep=None)¶
-
tools.helpers.config_manager.config_conversion.
to_list
(v, sep=', ')¶
-
tools.helpers.config_manager.config_conversion.
to_tuple
(v, sep=', ')¶
tools.helpers.config_manager.config_dict module¶
Dictionary-like classes used in configuration.
-
class
tools.helpers.config_manager.config_dict.
ConfigDict
(dico=None, auto_cast=False, default_section=<object object>, section=<object object>)¶ Bases:
tools.helpers.models.dict_models.BaseDict
ConfigDict class
>>> def_conf_d = ConfigDict({'a': {1: 5, 2: 6, 5: 7}, 2: {1: 8, 3: 9}, 'other': {1: 10, 4: 11}}) >>> conf_d = ConfigDict({'a': {1: 12, 2: 13, 6: 14}, 2: {1: 15}, 'other2': {1: 16, 4: 17}}) >>> conf_d.config OrderedDict([('a', SectionDict: OrderedDict([('1', 12), ('2', 13), ('6', 14)])), ('2', SectionDict: OrderedDict([('1', 15)])), ('other2', SectionDict: OrderedDict([('1', 16), ('4', 17)]))]) >>> conf_d.config = conf_d >>> conf_d.config OrderedDict([('a', SectionDict: OrderedDict([('1', 12), ('2', 13), ('6', 14)])), ('2', SectionDict: OrderedDict([('1', 15)])), ('other2', SectionDict: OrderedDict([('1', 16), ('4', 17)]))]) >>> conf_d.config = def_conf_d >>> conf_d.config OrderedDict([('a', SectionDict: OrderedDict([('1', 5), ('2', 6), ('5', 7)])), ('2', SectionDict: OrderedDict([('1', 8), ('3', 9)])), ('other', SectionDict: OrderedDict([('1', 10), ('4', 11)]))])
-
TO_KEY_FUNC
()¶ Converts the last argument into a valid section of ConfigDict.
-
add_section
(section, section_dict=None, auto_cast=False, exist_ok=False, set_section=False) → None¶
-
append
(other: Union[dict, ConfigDict]) → None¶
-
clear
(section=None)¶
-
property
config
¶
-
property
default_section
¶
-
get
(item: str, default=None)¶
-
get_section
(section=None, set_section=False, add_section=False) → Optional[tools.helpers.config_manager.config_dict.SectionDict]¶ Get the ConfigDict section (SectionDict). If section is None, the default section is used.
- Parameters
section – section string
set_section – if True, set section
add_section – if True and section doesn’t exist, create it
- Returns
SectionDict associated to section
-
property
isempty
¶
-
property
isnone
¶
-
merge
(config_dict: Union[dict, ConfigDict], how='outer', how_section=None, inplace=False, deepcopy_right=True) → Optional[tools.helpers.config_manager.config_dict.ConfigDict]¶
-
static
merge_config_dict
(left_dict: tools.helpers.config_manager.config_dict.ConfigDict, right_dict: tools.helpers.config_manager.config_dict.ConfigDict, how='outer', how_section=None) → tools.helpers.config_manager.config_dict.ConfigDict¶ Modify left_dict inplace, updated with right_dict
-
property
section
¶
-
sections
() → list¶
-
set_section
(section=None, add_section=True) → None¶
-
to_str
(write_flags=False) → str¶
-
update
(other: Union[dict, ConfigDict]) → None¶
-
-
class
tools.helpers.config_manager.config_dict.
SectionDict
(dico=None, auto_cast=False)¶ Bases:
tools.helpers.models.dict_models.BaseDict
Dictionary-like class.
-
TO_KEY_FUNC
()¶ Converts the last argument into a valid key of SectionDict.
-
clear
()¶
-
merge
(section_dict, how='outer', inplace=False)¶
-
setdefault
(k, default=None)¶
-
to_str
(write_flags=False)¶
-
update
(other)¶
-
-
tools.helpers.config_manager.config_dict.
to_key
(*args)¶ Converts the last argument into a valid key of SectionDict.
-
tools.helpers.config_manager.config_dict.
to_section
(*args)¶ Converts the last argument into a valid section of ConfigDict.
tools.helpers.config_manager.config_models module¶
Configuration classes.
-
class
tools.helpers.config_manager.config_models.
Config
(*args, **kwargs)¶ Bases:
tools.helpers.config_manager.config_models._Config
Unique configuration (singleton)
tools.helpers.config_manager.simple_config module¶
Hard coded configuration (no configuration file)
-
class
tools.helpers.config_manager.simple_config.
SimpleConfig
(*args, **kwargs)¶ Bases:
tools.helpers.config_manager.simple_config._SimpleConfig
Unique configuration (singleton)
Module contents¶
Configuration loaded from/saved to disk with INI syntax.
As the configuration is a singleton (the configuration class can only have a unique instance), it can be accessed by calling Config class directly. For example:
>>> CONFIG = Config()
>>> CONFIG2 = Config()
>>> CONFIG is CONFIG2
True
- Configuration is 2-levels dictionary-like object, but its values can be accessed like a 1-level dictionary.
Config
_____________| |______________________________________________________________________________________________ | | | | | |
- config (ConfigDict) default_config (ConfigDict) temp_config (OrderedDict) path (Path) default_path (Path) other
- |__________________________________________________________________
- | |
- config (OrderedDict) default_section (default key of config) section (current key of config)
- SectionDict
config (OrderedDict)
# Methods to get elements
- ## Get item: CONFIG[item]
Search in the current section ‘section’ of the current config ‘config’ (loaded from disk) Equivalent 2-levels dictionary: CONFIG[section][item] On KeyError, continue to search:
Search in the default section ‘default_section’ of the current config ‘config’ Equivalent 2-levels dictionary: CONFIG[default_section][item] On KeyError, continue to search:
Search in the current section ‘section’ of the default config ‘default_config’ (hard coded) Equivalent 2-levels dictionary: CONFIG.default_config[section][item] On KeyError, continue to search:
Search in the default section ‘default_section’ of the default config ‘default_config’ Equivalent 2-levels dictionary: CONFIG.default_config[default_section][item] On KeyError, raise KeyError
## Call: CONFIG(item) or CONFIG(section, item) or CONFIG(section, item, default) Possible cases:
item is the only argument: CONFIG(item)
- item and default are the only arguments: CONFIG(item, default=default)
search in default section of current configuration,
search in default section of default configuration,
3) a) raise KeyError 3) b) return default
section is referenced: CONFIG(section, item) or CONFIG(item, section=section)
section and default are referenced: CONFIG(section, item, default) or CONFIG(section, item, default=default) or CONFIG(item, section=section, default=default)
search in current section of current configuration,
search in default section of current configuration,
search in current section of default configuration,
search in default section of default configuration,
5) c) raise KeyError 5) d) return default
# Initialization TODO
# Load/Save configuration TODO
-
class
tools.helpers.config_manager.
Config
(*args, **kwargs)¶ Bases:
tools.helpers.config_manager.config_models._Config
Unique configuration (singleton)