mixins

ReadsJSON

class wysdom.mixins.ReadsJSON(value: Mapping[str, Any] = None, json_dom_info: DOMInfo = None)

Bases: wysdom.dom.DOMObject.DOMObject

Adds JSON reading and writing functionality to a DOMObject.

Parameters:
  • value – A dict (or any collections.abc.Mapping) containing the data to populate this object’s properties.
  • json_dom_info – A DOMInfo named tuple containing information about this object’s position in the DOM.
classmethod from_json(json_string: str) → wysdom.mixins.ReadsJSON.ReadsJSON

Create a new DOM object by from a JSON string.

Parameters:json_string – JSON string to read
Returns:New DOM object instance
classmethod from_json_file(filename: str) → wysdom.mixins.ReadsJSON.ReadsJSON

Create a new DOM object from a file on disk.

Parameters:filename – File path on disk of JSON file
Returns:New DOM object instance
to_json() → str

Serialize the DOM object to JSON.

return: The DOM object, serialized as a JSON string

ReadsYAML

class wysdom.mixins.ReadsYAML(value: Mapping[str, Any] = None, json_dom_info: DOMInfo = None)

Bases: wysdom.dom.DOMObject.DOMObject

Adds YAML reading and writing functionality to a DOMObject.

Parameters:
  • value – A dict (or any collections.abc.Mapping) containing the data to populate this object’s properties.
  • json_dom_info – A DOMInfo named tuple containing information about this object’s position in the DOM.
classmethod from_yaml(yaml_string: Union[str, TextIO]) → wysdom.mixins.ReadsYAML.ReadsYAML

Create a new DOM object by from a YAML string.

Parameters:yaml_string – YAML string to read
Returns:New DOM object instance
classmethod from_yaml_file(filename: str) → wysdom.mixins.ReadsYAML.ReadsYAML

Create a new DOM object from a file on disk.

Parameters:filename – File path on disk of YAML file
Returns:New DOM object instance
to_yaml(**kwargs) → str

Serialize the DOM object to YAML.

Parameters:kwargs – Optional keyword arguments to pass to PyYAML’s safe_dump method. See parameters for Dumper in https://pyyaml.org/wiki/PyYAMLDocumentation
Returns:The DOM object, serialized as a YAML string

RegistersSubclasses

class wysdom.mixins.RegistersSubclasses(*args, **kwargs)

Bases: abc.ABC

A mixin class that allows subclasses to be registered in the main class by a registered name, using the register_as parameter in the subclass declaration.

registered_name = None
classmethod registered_subclass(name: str, return_common_superclass: bool = True) → Type[wysdom.mixins.RegistersSubclasses.RegistersSubclasses]

Return a registered subclass by name.

Parameters:
  • name – The registered name of the subclass, as defined in register_as when the class was declared.
  • return_common_superclass – Disambiguate multiple valid subclasses by returning the common superclass among them, if one exists. Defaults to True.
Returns:

A subclass of the class from which this method was called.

Raises:

KeyError – If no matching subclass is found or if multiple ambiguous subclasses are found.

classmethod registered_subclass_instance(name: str, *args, **kwargs) → wysdom.mixins.RegistersSubclasses.RegistersSubclasses

Create a new instance of a registered subclass by name.

Parameters:
  • name – The registered name of the subclass, as defined in register_as when the class was declared.
  • args – Positional arguments to pass to the subclass.
  • kwargs – Keyword arguments to pass to the subclass.
Returns:

An instance of a subclass of the class from which this method was called.

classmethod registered_subclasses() → Dict[str, List[Type[wysdom.mixins.RegistersSubclasses.RegistersSubclasses]]]

Return all of the registered subclasses in this class’s namespace.

Returns:A dictionary of subclasses, indexed by registered name.
classmethod registered_subclasses_by_name(name) → List[Type[wysdom.mixins.RegistersSubclasses.RegistersSubclasses]]

Return all of the registered subclasses for a given name that are a proper subclass of this class.

Parameters:name – The registered name of the subclass, as defined in register_as when the class was declared.
Returns:A list of subclasses of the class from which this method was called.