easycore.common.config

class easycore.common.config.CfgNode(init_dict: dict = None, copy=True)[source]

Bases: dict

Config Node

__init__(init_dict: dict = None, copy=True)[source]
Parameters:
  • init_dict (dict) – a possibly-nested dictionary to initialize the CfgNode.
  • copy (bool) – if this option is set to False, the CfgNode instance will share the value with the init_dict, otherwise the contents of init_dict will be deepcopied.
freeze(frozen: bool = True)[source]

freeze or unfreeze the CfgNode and all of its children

Parameters:frozen (bool) – freeze or unfreeze the config
is_frozen()[source]

get the state of the config.

Returns:bool – whether the config tree is frozen.
copy()[source]

deepcopy this CfgNode

Returns:CfgNode
merge(cfg)[source]

merge another CfgNode into this CfgNode, the another CfgNode will override this CfgNode.

Parameters:cfg (CfgNode) –
save(save_path, encoding='utf-8')[source]

save the CfgNode into a yaml file

Parameters:save_path
classmethod open(file, encoding='utf-8')[source]

load a CfgNode from file.

Parameters:
  • file (io.IOBase or str) – file object or path to the yaml file.
  • encoding (str) –
Returns:

CfgNode

classmethod load(yaml_str: str)[source]

load a CfgNode from a string of yaml format

Parameters:yaml_str (str) –
Returns:CfgNode
classmethod dump(cfg, stream=None, encoding=None, **kwargs)[source]

dump CfgNode into yaml str or yaml file

Note

if stream option is set to non-None object, the CfgNode will be dumpped into stream and return None, if stream option is not given or set to None, return a string instead.

Parameters:
Returns:

None or str

dict()[source]

convert to a dict

Returns:dict
__str__()[source]
Returns:str – a str of dict format
class easycore.common.config.HierarchicalCfgNode[source]

Bases: object

Config Node help class for open yaml file that depends on another yaml file.

You can specify the dependency between yaml files with __BASE__ tag.

Example

We can load yaml file example-A.yaml which depends on example-B.yaml in the following way.

example-A.yaml :

__BASE__: ./example-B.yaml
A: in example-A.yaml
C: in example-A.yaml

example-B.yaml :

A: in example-B.yaml
B: in example-B.yaml

Now, you can open example-A.yaml:

>>> import easycore.common.config import HierarchicalCfgNode
>>> cfg = HierarchicalCfgNode.open("./example-A.yaml")
>>> print(cfg)
{"A" : "in example-A.yaml", "B" : "in example-B.yaml", "C" : "in example-A.yaml"}

Attributes in example-A.yaml will cover attributes in example-B.yaml.

Note

__BASE__ can be an absolute path or a path relative to the yaml file. And it will be first considered as a path relative to the yaml file then an absolute path.

classmethod open(file, encoding='utf-8')[source]

load a CfgNode from file.

Parameters:
  • file (str) – path to the yaml file.
  • encoding (str) –
Returns:

CfgNode

classmethod save(cfg, save_path, base_cfg_path=None, base_path_relative=True, encoding='utf-8')[source]

save the CfgNode into a yaml file

Parameters:
  • cfg (CfgNode) –
  • save_path (str) –
  • base_cfg_path (str) – if not specified, it behavior like cfg.save(save_path, encoding).
  • base_path_relative (bool) – whether to set base cfg path to a path relative to the save_path.
  • encoding (str) –