egrecho.core.config#

class egrecho.core.config.DataclassConfig[source]#

Bases: ConfigFileMixin, DataclassSerialMixin

Base class for dataclass configuration.

Surely we could directly interact args in dataclass with outside argparse cli. However, cli can hardly parse nest cases (e.g., dict, nest sequence, etc.). What’more, adding all args to cli is redundant. So our configs folllow below rules:

  • We abandon the InitVar set for dataclasses, since InitVar is

    tricky to serialize/deserialize.

  • additional keys in fields’ metadata:
    • ‘to_dict’: if set False, will be ignored when serialization. defaults to True.

    • ‘encoding_fn’: custom fn when serialization.

    • ‘decoding_fn’: custom fn when desrialization.

classmethod from_config(config=None, strict=False, **kwargs)[source]#

Creates an instance from config.

Input config can be an instance or a dict, the invalid overwrite args in **kwargs will be informed and ignored if strict=True.

Parameters:
  • config (Union[dict, DataclassConfig]) -- The configuration.

  • strict (bool) -- if True, raise exception if got unexpected arguments. Defautls to False.

Returns:

The new config instance.

Return type:

DataclassConfig