utils.Container
AttributeDict
class AttributeDict(dict)
A dict-like container to store nested arguments defined in config.
Since the container inherit python native dict (base class), we simply store
arguments in the base dict with such navie way : dict[key] = value,
by calling super().setitem(key, value) in subclass. self.setattr
also call super().setitem underhood! According to the way we store args,
we access args by the navie way self[key] (namely, dict[key]).
Additionally, we build the getattr guard for implement the defaultdict functionality.
self.getitem no need to override, just as same as base class.
__init__
def __init__(init_dict={})
Constructor of container.
Arguments:
init_dictdict - typically it’ll be value dict parsed in Configer. Default to empty dict.
Returns:
None.
__getattribute__
def __getattribute__(attr)
Override getattribute dunder method. Since we apply base class (dict) to store all args, self.dict should not be used in anyway.
- Raise:
Runtime Error, while user attempt to access self.dict.
__getattr__
def __getattr__(key)
Override getattr dunder method to silently build the ‘empty dict’. So that we can assign value to the specific argument without define their parent dict. Note : same behavior as defaultdict, allow recursively self.setattr.
Returns:
AttributeDict, the returned dict have pre-defined empty dict, then the specific argument could be updated to the empty dict.
set_attr_dict
def set_attr_dict(raw_dict)
Make input dict become AttributeDict instance, call setitem underhood.
Arguments:
raw_dictdict - python native dict, it’ll be turn into AttributeDict after calling this method.
__setattr__
def __setattr__(key, value)
Override setattr dunder method. call setitem underhood.
__setitem__
def __setitem__(key, value)
Override setitem dunder method. Wrap any input value with AttributeDict container.
__deepcopy__
def __deepcopy__(memo=None)
Support deepcopy, src:https://stackoverflow.com/questions/49901590/python-using-copy-deepcopy-on-dotdict.
__getstate__
def __getstate__()
Basic serialized interface (i.e. pickle). Return serielized python object.
__setstate__
def __setstate__(de_ser_self)
Basic serialized interface (i.e. pickle). Accept de-serielized object, replace default self into it.
Flag
class Flag(object)
A synchronized object to defined config in Configer. It’s inspired by absl flag in tensorflow. Although this is not useful, but we keep this class for competible with easy_config early version. You still can use it.
__new__
def __new__(cls, *args, **kwargs)
Override new dunder method. new is used to create the class before calling init, so we set the singleton guard in here for implement the singleton design pattern.
Arguments:
*args, **kwargs : will be passed into FLAG_spec instance, which is the singleton instance.
Returns:
FLAG_spec, a sync object of config.
FLAGS
@property
def FLAGS()
Interface to access FLAG_spec object.