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
self
withother
.other
may 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 (
Optional
[Callable
]) – Callable to convert ‘obj’ to mapping objectoptions – Optional keyword arguments.
- Return type:
Dict
[str
,Any
]- Returns:
A dict or OrderedDict or object of ‘cls’