Skip to content

config module

Utilities for configuration.


convert_to_dict function

convert_to_dict(
    dct,
    nested=True
)

Convert any dict (apart from atomic_dict) to dict.

Set nested to True to convert all child dicts in recursive manner.


copy_dict function

copy_dict(
    dct,
    copy_mode='shallow',
    nested=True
)

Copy dict based on a copy mode.

The following modes are supported:

  • 'shallow': Copies keys only.
  • 'hybrid': Copies keys and values using copy.copy.
  • 'deep': Copies the whole thing using copy.deepcopy.

Set nested to True to copy all child dicts in recursive manner.


get_func_arg_names function

get_func_arg_names(
    func,
    arg_kind=None
)

Get argument names of a function.


get_func_kwargs function

get_func_kwargs(
    func
)

Get keyword arguments with defaults of a function.


merge_dicts function

merge_dicts(
    *dicts,
    to_dict=True,
    copy_mode='shallow',
    nested=True,
    same_keys=False
)

Merge dicts.

Args

*dicts : dict
Dicts.
to_dict : bool
Whether to call convert_to_dict() on each dict prior to copying.
copy_mode : str

Mode for copy_dict() to copy each dict prior to merging.

Pass None to not copy.

nested : bool
Whether to merge all child dicts in recursive manner.
same_keys : bool
Whether to merge on the overlapping keys only.

resolve_dict function

resolve_dict(
    dct,
    i=None
)

Select keyword arguments.


set_dict_item function

set_dict_item(
    dct,
    k,
    v,
    force=False
)

Set dict item.

If the dict is of the type Config, also passes force keyword to override blocking flags.


update_dict function

update_dict(
    x,
    y,
    nested=True,
    force=False,
    same_keys=False
)

Update dict with keys and values from other dict.

Set nested to True to update all child dicts in recursive manner. For force, see set_dict_item().

If you want to treat any dict as a single value, wrap it with atomic_dict.

Note

If the child dict is not atomic, it will copy only its values, not its meta.


AtomicConfig class

AtomicConfig(
    dct=None,
    copy_kwargs=None,
    reset_dct=None,
    reset_dct_copy_kwargs=None,
    frozen_keys=None,
    readonly=None,
    nested=None,
    convert_dicts=None,
    as_attrs=None
)

Config that behaves like a single value when merging.

Superclasses

Inherited members


Config class

Config(
    dct=None,
    copy_kwargs=None,
    reset_dct=None,
    reset_dct_copy_kwargs=None,
    frozen_keys=None,
    readonly=None,
    nested=None,
    convert_dicts=None,
    as_attrs=None
)

Extends dict with config features such as nested updates, frozen keys/values, and pickling.

Args

dct : dict
Dict to construct this config from.
copy_kwargs : dict

Keyword arguments passed to copy_dict() for copying dct and reset_dct.

Copy mode defaults to 'shallow' if readonly, otherwise to 'hybrid'.

reset_dct : dict

Dict to fall back to in case of resetting.

If None, copies dct using reset_dct_copy_kwargs.

reset_dct_copy_kwargs : dict
Keyword arguments that override copy_kwargs for reset_dct.
frozen_keys : bool

Whether to deny updates to the keys of the config.

Defaults to False.

readonly : bool

Whether to deny updates to the keys and values of the config.

Defaults to False.

nested : bool

Whether to do operations recursively on each child dict.

Such operations include copy, update, and merge. Disable to treat each child dict as a single value. Defaults to True.

convert_dicts : bool or type

Whether to convert child dicts to configs with the same configuration.

This will trigger a waterfall reaction across all child dicts. Won't convert dicts that are already configs. Apart from boolean, you can set it to any subclass of Config to use it for construction. Requires nested to be True. Defaults to False.

as_attrs : bool

Whether to enable accessing dict keys via the dot notation.

Enables autocompletion (but only during runtime!). Raises error in case of naming conflicts. Defaults to True if frozen or readonly, otherwise False.

Defaults can be overridden with settings under config in settings.

If another config is passed, its properties are copied over, but they can still be overridden with the arguments passed to the initializer.

Note

All arguments are applied only once during initialization.

Superclasses

Inherited members

Subclasses


as_attrs_ property

Whether to enable accessing dict keys via dot notation.


clear method

Config.clear(
    force=False
)

Remove all items.


convert_dicts_ property

Whether to convert child dicts to configs with the same configuration.


copy method

Config.copy(
    reset_dct_copy_kwargs=None,
    **copy_kwargs
)

Copy the instance in the same way it's done during initialization.

copy_kwargs override Config.copy_kwargs_ and Config.reset_dct_copy_kwargs_ via merging. reset_dct_copy_kwargs override merged Config.reset_dct_copy_kwargs_.


copy_kwargs_ property

Parameters for copying dct.


frozen_keys_ property

Whether to deny updates to the keys and values of the config.


load_update method

Config.load_update(
    fname,
    **kwargs
)

Load dumps from a file and update this instance.

Note

Updates both the config properties and dictionary.


make_checkpoint method

Config.make_checkpoint(
    force=False,
    **reset_dct_copy_kwargs
)

Replace reset_dct by the current state.

reset_dct_copy_kwargs override Config.reset_dct_copy_kwargs_.


merge_with method

Config.merge_with(
    other,
    nested=None,
    **kwargs
)

Merge with another dict into one single dict.

See merge_dicts().


nested_ property

Whether to do operations recursively on each child dict.


pop method

Config.pop(
    k,
    v=<object object>,
    force=False
)

Remove and return the pair by the key.


popitem method

Config.popitem(
    force=False
)

Remove and return some pair.


readonly_ property

Whether to deny any updates to the config.


reset method

Config.reset(
    force=False,
    **reset_dct_copy_kwargs
)

Clears the config and updates it with the initial config.

reset_dct_copy_kwargs override Config.reset_dct_copy_kwargs_.


reset_dct_ property

Dict to fall back to in case of resetting.


reset_dct_copy_kwargs_ property

Parameters for copying reset_dct.


to_dict method

Config.to_dict(
    nested=None
)

Convert to dict.


update method

Config.update(
    *args,
    nested=None,
    force=False,
    **kwargs
)

Update the config.

See update_dict().


Configured class

Configured(
    **config
)

Class with an initialization config.

All subclasses of Configured are initialized using Config, which makes it easier to pickle.

Settings are defined under configured in settings.

Warning

If any attribute has been overwritten that isn't listed in Configured.writeable_attrs, or if any Configured argument depends upon global defaults, their values won't be copied over. Make sure to pass them explicitly to make the saved & loaded / copied instance resilient to changes in globals.

Superclasses

Inherited members

Subclasses


config property

Initialization config.


copy method

Configured.copy(
    copy_mode='shallow',
    nested=None,
    cls=None
)

Create a new instance by copying the config.

See Configured.replace().


replace method

Configured.replace(
    copy_mode_='shallow',
    nested_=None,
    cls_=None,
    **new_config
)

Create a new instance by copying and (optionally) changing the config.

Warning

This operation won't return a copy of the instance but a new instance initialized with the same config and writeable attributes (or their copy, depending on copy_mode).


update_config method

Configured.update_config(
    *args,
    **kwargs
)

Force-update the config.


writeable_attrs property

Set of writeable attributes that will be saved/copied along with the config.


Default class

Default(
    value
)

Class for wrapping default values.


DumpTuple class

DumpTuple(
    cls,
    dumps
)

DumpTuple(cls, dumps)

Superclasses

  • builtins.tuple

cls property

Alias for field number 0


dumps property

Alias for field number 1


Pickleable class

Pickleable()

Superclass that defines abstract properties and methods for pickle-able classes.

Subclasses


dumps method

Pickleable.dumps(
    **kwargs
)

Pickle to bytes.


load class method

Pickleable.load(
    fname,
    **kwargs
)

Load dumps from a file and create new instance.


loads class method

Pickleable.loads(
    dumps,
    **kwargs
)

Unpickle from bytes.


save method

Pickleable.save(
    fname,
    **kwargs
)

Save dumps to a file.


PickleableDict class

PickleableDict(
    *args,
    **kwargs
)

Dict that may contain values of type Pickleable.

Superclasses

Inherited members

Subclasses


load_update method

PickleableDict.load_update(
    fname,
    **kwargs
)

Load dumps from a file and update this instance.


atomic_dict class

atomic_dict(
    *args,
    **kwargs
)

Dict that behaves like a single value when merging.

Superclasses

  • builtins.dict

Subclasses