anyconfig.dicts¶
Utility functions to operate on mapping objects such as get, set and merge.
- anyconfig.dicts.mk_nested_dic(path, val, seps=('/', '.'))¶
Make a nested dict iteratively.
- Parameters:
path (
str) – Path expression to make a nested dictval (
Any) – Value to setseps (
tuple[str,...]) – Separator char candidates
- Return type:
dict[str,Any]
- anyconfig.dicts.get(dic, path, seps=('/', '.'), idx_reg=re.compile('(?:0|[1-9][0-9]*)'))¶
Getter for nested dicts.
- Parameters:
dic (
dict[str,Any]) – a dict[-like] objectpath (
str) – Path expression to point object wantedseps (
tuple[str,...]) – Separator char candidates
- Return type:
tuple[Any,str]- Returns:
A tuple of (result_object, error_message)
- anyconfig.dicts.set_(dic, path, val, seps=('/', '.'))¶
Setter for nested dicts.
- Parameters:
dic (
dict[str,Any]) – a dict[-like] object support recursive merge operationspath (
str) – Path expression to point object wantedseps (
tuple[str,...]) – Separator char candidates
- Return type:
None
- anyconfig.dicts.merge(self, other, ac_merge='merge_dicts', **options)¶
Update (merge) a mapping object
selfwithother.othermay be a mapping object or an iterable yields (key, value) tuples based on merge strategy ‘ac_merge’.- Parameters:
others – a list of dict[-like] objects or (key, value) tuples
another – optional keyword arguments to update self more
ac_merge (
str) – Merge strategy to choose
- Return type:
None
- anyconfig.dicts.convert_to(obj, *, ac_ordered=False, ac_dict=None, **options)¶
Convert a mapping objects to a dict or object of ‘to_type’ recursively.
Borrowed basic idea and implementation from bunch.unbunchify. (bunch is distributed under MIT license same as this.)
- Parameters:
obj (
Any) – A mapping objects or other primitive objectac_ordered (
bool) – Use OrderedDict instead of dict to keep order of itemsac_dict (
Callable|None) – Callable to convert ‘obj’ to mapping objectoptions (
Any) – Optional keyword arguments.
- Return type:
dict[str,Any]- Returns:
A dict or OrderedDict or object of ‘cls’