cogwheel.utils.JSONMixin

class cogwheel.utils.JSONMixin

Bases: InitDictMixin

Provide JSON output to subclasses. Register subclasses in JSONMixin.subclass_registry.

Define a method get_init_dict which works for classes that store their init parameters as attributes with the same names. If this is not the case, the subclass should override get_init_dict.

Define a method reinstantiate that allows to safely modify attributes defined at init.

Methods

get_init_dict

Return dictionary with keyword arguments to __init__.

get_module_name

Name of the module that defines the instance's class.

reinstantiate

Return a new instance of the class, possibly updating init_kwargs.

to_json

Write class instance to json file.

Attributes

subclass_registry

get_init_dict(**kwargs)

Return dictionary with keyword arguments to __init__.

Only works if the class stores its init parameters as attributes with the same names. Otherwise, the subclass should override this method.

Parameters:
**kwargs

Allows to manually override some keys. The remaining ones will be read from the instance’s attributes. All keywords must be in the __init__ signature. It’s mostly here to facilitate overriding by subclasses.

get_module_name()

Name of the module that defines the instance’s class.

reinstantiate(**new_init_kwargs)

Return a new instance of the class, possibly updating init_kwargs.

Values not passed will be taken from the current instance.

to_json(dirname, basename=None, *, dir_permissions=493, file_permissions=420, overwrite=False)

Write class instance to json file.

It can then be loaded with read_json.