Configer
Configer
class Configer(object)
The core module of easy_configer to implement the user configuration system.
__init__
def __init__(description: str = "",
cmd_args: bool = False,
split_chr: str = " = ") -> None
Constructor of Configer.
Arguments:
descriptionstr, optional - A customized helper information which describe the role of config file. Defaults to empty string.cmd_argsbool, optional - A flag to indicate reading argument from commendline and override the default config. Defaults to False.split_chrstr, optional - A char-string used to format the config syntax. Defaults to “ = “. For example, “a*13@int” which means the argument ‘a’ contain interger value 13, and the “*” is the split_chr. Note that better not to change this char-string to prevent the symbol conflict.
Returns:
None. Don’t accept anything return from constructor.
cfg_from_cli
def cfg_from_cli() -> None
Building config from the commendline input and only apply the arguments from commend-line. ( only recommend for very lightweight config )
cfg_from_str
def cfg_from_str(raw_cfg_text: str, allow_override: bool = False) -> None
Building config from the given config string.
Arguments:
raw_cfg_textstr - The string which declare the arguments with the same syntax used in config file.allow_overridebool, optional - A flag allow override config from the other source, such as the other .ini config file, config string. Default to False.
cfg_from_ini
def cfg_from_ini(cfg_path: str, allow_override: bool = False) -> None
Building config from the given .ini config file.
Arguments:
cfg_pathstr - The path which locate the .ini config file.allow_overridebool, optional - A flag allow override config from the other source, such as the other .ini config file, config string. Default to False.
args_from_cmd
def args_from_cmd() -> None
Update the arguments by commend line input string. Note that this method allow override the pre-define config natively (with silent mode). ( Because commentline inputs are explicitly given by user, we don’t need to warn that )
__or__
def __or__(cfg)
Support merge two config ‘with override’ the left-hand side config. For example. cfg_a = cfg_a | cfg_b, cfg_a will be overrided by cfg_b.
Arguments:
cfgAttributeDict - A container used to store the argument. it inherit from dict and the given input could be a nested dict.
__add__
def __add__(cfg)
Support merge two config ‘without override’ the any config. This method call self.concate_cfg(.) underhood.
Arguments:
cfgAttributeDict - A container used to store the argument. it inherit from dict and the given input could be a nested dict.Raise: RuntimeError with re-define argument.
concate_cfg
def concate_cfg(cfg)
Merge two config ‘without override’ the any config.
Arguments:
cfgAttributeDict - A container used to store the argument. it inherit from dict and the given input could be a nested dict.Raise: RuntimeError with re-define argument.
Returns:
Configer.
merge_conf
def merge_conf(cfg, override=True)
Merge two config ‘with override’ the config. The config will be overrided by the given config cfg.
Arguments:
cfgAttributeDict - A container used to store the argument. it inherit from dict and the given input could be a nested dict.overridebool - A flag to indicate overriding value by the given config cfg. Default to True.
Returns:
None. This is inplace operation.
__str__
def __str__()
Present all ‘non-private’ arguments defined in config.
__iter__
def __iter__()
Return iterator for Configer. Because Configer itself isn’t dict.
__getitem__
def __getitem__(key)
Support getitem for Configer. Because Configer itself isn’t dict.
__setitem__
def __setitem__(key, value)
Support setitem for Configer. Because Configer itself isn’t dict.
get
def get(key, default_value=None)
Support get for Configer. Because Configer itself isn’t dict.
get_cfg_flag
def get_cfg_flag()
Return the FLAG object which ‘sync’ the config.
get_doc_str
def get_doc_str()
Return the helper information string.
regist_cnvtor
def regist_cnvtor(type_name: str = None, cnvt_func: callable = None)
Declare the user customized class. The registered type (class) can be used to declare the argument in the config file.
Arguments:
type_namestr - type name used in config file. i.e. registered as ‘dummy’, then declare a argument with such type will bevar = {'arg1':42}@dummy.cnvt_funccallable - typically it’s the constructor of your customized class. So, you can just directly feed the customized class as this arguemnt.
Returns:
None. This registered method doesn’t return any flag.
split_char
@property
def split_char()
Show the split char used in config file.