qewton.config.saving package

Module contents

Submodules

qewton.config.saving.callables module

qewton.config.saving.callables.save(obj: Serializable, path: str | Path, replace: bool = False, compress=False) → None[source]

Saves a Serializable object to the specified path.

Parameters:
  • obj (Serializable) – The object to be saved.

  • path (str | Path) – The path where the object will be saved.

  • replace (bool, optional) – If True, replaces existing files. Defaults to False.

  • compress (bool, optional) – If True, compresses the saved files. Defaults to False.

qewton.config.saving.callables.load(path: str | Path) → Any[source]

Loads a Serializable object from the specified path.

Parameters:

path (str | Path) – The path from which the object will be loaded.

Returns:

The loaded object.

Return type:

Serializable

qewton.config.saving.loading module

class qewton.config.saving.loading.Deserializer(path: str | Path)[source]

Bases: object

Handles the deserialization of objects from a saved state, reconstructing them based on their serialized representation.

Parameters:

path (str | Path) – The path from which to load the serialized data.

version: int

Format version of the file.

load()[source]

Loads the serialized data from the specified path and reconstructs the objects. Saves the reconstructed objects in the id_to_obj dictionary,

Raises:

RuntimeError – If there are circular dependencies or missing dependencies that prevent the complete loading of objects.

update_reference_node(node) → None[source]

Updates the reference node to ensure unique node IDs.

Parameters:

node – The node to be set as the reference node.

qewton.config.saving.saving module

class qewton.config.saving.saving.Serializer(path: str | Path, replace: bool = False, compress=False)[source]

Bases: object

Handles the serialization of objects into a structured format for saving to disk.

Parameters:
  • path (str | Path) – The path where the serialized data will be saved.

  • replace (bool, optional) – If True, replaces existing files. Defaults to False.

  • compress (bool, optional) – If True, compresses the saved files. Defaults to False.

add_objects(parent_obj, objects: list) → list[int][source]

Adds a list of objects to the serialization dictionary, assigning each a unique index. The index depends on the internal python id and whether the object has already been added to the Serializer.

Parameters:
  • parent_obj (_type_) – The original object that is adding the objects. This is used for error reporting and circumvent circular references.

  • objects (list) – All objects that should be added to the serialization dictionary.

Raises:

TypeError – If an object is not serializable, a TypeError is raised.

Returns:

The list of indices corresponding to the added objects. Each index maps to the position of the object in the serialization dictionary.

Return type:

list[int]

set_serialization_data(obj_id: int, data: dict) → None[source]
save() → None[source]

Saves all data stored in the Serializer to disk. It writes the serialized data to a JSON file, or compresses it if specified. It also saves general configuration data to a separate JSON file.

class qewton.config.saving.saving.Serializable(*args, **kwargs)[source]

Bases: Protocol

A protocol that defines the interface for serializable objects. Any class that implements this protocol should provide methods for saving and loading its state. By default, it collects all attributes in the object’s __dict__ for serialization.

save(serializer: Serializer) → None[source]

The save method is responsible for serializing the object’s state. It collects the object’s attributes and adds them to the serializer.

Parameters:

serializer (Serializer) – The serializer instance that manages the serialization process.

classmethod construct_new_object(serializer: Deserializer, data_config: dict) → Any[source]

Generates a new instance of the class without calling its __init__ method.

Parameters:
  • serializer (Deserializer) – The deserializer instance that manages the loading process.

  • data_config (dict) – A dictionary containing the serialized data for the object.

Returns:

A new instance of the class, without calling its __init__ method.

The attributes will be set later.

Return type:

Any

ready_to_load(serializer: Deserializer, data_config: dict) → bool[source]

Checks if all dependencies of the object are ready to be loaded.

Parameters:
  • serializer (Deserializer) – The deserializer instance that manages the loading process.

  • data_config (dict) – A dictionary containing the serialized data for the object.

Returns:

If the object is ready to be loaded (i.e., all its

dependencies are ready).

Return type:

bool

upgrade(version: int) → None[source]

Bring this object, loaded from a file of the older format version, up to date with its class. Called after every object of the file is loaded. Does nothing by default.

Parameters:

version – The format version of the file.

load(serializer: Deserializer, data_config: dict) → None[source]

Loads the object’s state from the serialized data. It sets the object’s attributes based on the serialized data.

Parameters:
  • serializer (Deserializer) – The deserializer instance that manages the loading process.

  • data_config (dict) – A dictionary containing the serialized data for the object.

qewton.config.saving.schema_keys module

class qewton.config.saving.schema_keys.SavingKeys[source]

Bases: object

Helper class that defines the keys used in the saving and loading process.

KEY_VERSION = 'version'
VERSION = 2

Format of the files written now. Version 1 (before structured dtypes) is still loaded, see Serializable.upgrade.

KEY_TYPE = 'type'
KEY_VALUES = 'values'
KEY_COMPRESSED = 'compressed'
KEY_LIST = 'list'
KEY_TUPLE = 'tuple'
KEY_DICT = 'dict'
KEY_SET = 'set'
KEY_ENUM = 'enum'
KEY_CLASS_OBJ = 'class_obj'
KEY_SERIALIZABLE = 'serializable'
KEY_CLASS = 'class'
KEY_SELF_ARGS = 'self_args'
KEY_MODULE = 'module'
KEY_ELLIPSIS = '...'
KEY_BACKEND_PARAMETER = 'backend_param'
KEY_SLICE = 'slice'
NODE_ID = 'node_id'
NODE_MODE = 'node_mode'
NODE_STATE = 'node_state'
FILE_CONFIG = 'config.json'
FILE_DATA = 'data.json'
FILE_PARAMETERS = 'parameters'