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 dict

  • val (Any) – Value to set

  • seps (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] object

  • path (str) – Path expression to point object wanted

  • seps (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 operations

  • path (str) – Path expression to point object wanted

  • seps (Tuple[str, ...]) – Separator char candidates

Return type:

None

anyconfig.dicts.merge(self, other, ac_merge='merge_dicts', **options)

Update (merge) a mapping object self with other.

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 object

  • ac_ordered (bool) – Use OrderedDict instead of dict to keep order of items

  • ac_dict (Optional[Callable]) – Callable to convert ‘obj’ to mapping object

  • options – Optional keyword arguments.

Return type:

Dict[str, Any]

Returns:

A dict or OrderedDict or object of ‘cls’