easycore.common.config¶
-
class
easycore.common.config.
CfgNode
(init_dict: dict = None, copy=True)[source]¶ Bases:
dict
Config Node
-
freeze
(frozen: bool = True)[source]¶ freeze or unfreeze the CfgNode and all of its children
Parameters: frozen (bool) – freeze or unfreeze the config
-
merge
(cfg)[source]¶ merge another CfgNode into this CfgNode, the another CfgNode will override this CfgNode.
Parameters: cfg (CfgNode) –
-
classmethod
open
(file, encoding='utf-8')[source]¶ load a CfgNode from file.
Parameters: 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: - cfg (CfgNode) –
- stream (io.IOBase or None) – if set to a file object, the CfgNode will be dumpped into stream and return None, if set to None, return a string instead.
- encoding (str or None) –
- **kwargs –
options of the yaml dumper.
Some useful options: [“allow_unicode”, “line_break”, “explicit_start”, “explicit_end”, “version”, “tags”].
See more details at https://github.com/yaml/pyyaml/blob/2f463cf5b0e98a52bc20e348d1e69761bf263b86/lib3/yaml/__init__.py#L252
Returns: None or str
-
-
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 onexample-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 inexample-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: Returns: CfgNode
-
classmethod