hespas.utils.serialisable
Classes
This is the base class for generically serialisable dataclases, and handles serialisation and deserialisation. |
- class hespas.utils.serialisable.Serialisable
Bases:
objectThis is the base class for generically serialisable dataclases, and handles serialisation and deserialisation. Users shouldn’t worry about this class or modify it - they should only use its public interfaces.
- __to_dict(d)
- to_dict() dict[str, Any]
Converts the current serialisable Counter object into a dictionary.
- Returns:
The current serialisable object as a dict
- classmethod from_dict(input: dict[str, Any]) Serialisable
Instantiates an object of this class from a dictionary. This function also does some type checking, as it is also used for processing the dict that is generated from loading the JSON representation. Because of this, it is stricter than the standard __init__, requiring all fields to be present (even if they have defaults in the dataclass implementation).
- Parameters:
input – dict to convert of an object of this class
- Returns:
An instantiated object of this class
- classmethod from_json(file_path: str | Path) Serialisable
Instantiates an object of this class from the file path given. Loads the file as JSON, then calls from_dict() to actually construct the object. This should be the standard inferface for deserialising the objects of this class.
- Parameters:
file_path – Path to the JSON file to load
- Returns:
An instantiated object of this class
- classmethod from_json_str(input: str) Serialisable
Instantiates an object of this class from a JSON-formatted string Parses the string as JSON, then calls from_dict() to actually construct the object.
- Parameters:
input – JSON formatted string to parse
- Returns:
An instantiated object of this class
- to_json(file_path: str | Path) Never
Converts the current object to a dict, then converts that dict to JSON and writes it to a file. This should be the standard way of deserialising objects of this class.
- Parameters:
file_path – Path to the JSON file to write out
- __init__() None
- to_json_str() str
Converts the current object to a dict, then converts that dict to a JSON string.
- Returns:
This object serialised as a JSON formatted string