loom.eka.utilities.serialization

Copyright 2024 Entropica Labs Pte Ltd

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

loom.eka.utilities.serialization.apply_to_nested(nested_list, func, param=None)[source]

Apply a function to all elements of a nested list.

loom.eka.utilities.serialization.dump(data, file)[source]

Write a pydantic dataclass as JSON to a file.

Parameters:
  • data (dataclass) – Pydantic dataclass to be saved as JSON

  • file (str) – Filename to write to

loom.eka.utilities.serialization.dumps(data)[source]

Return the pydantic dataclass as a JSON string. If the dataclass defines its own custom dumps() method, the custom method is used. Otherwise, the dataclass object is converted to a dictionary using the asdict method of the dataclasses module and then converted to a JSON string.

Parameters:

data (dataclass) – Pydantic dataclass to be saved as JSON

Returns:

JSON string representation of the Eka

Return type:

str

loom.eka.utilities.serialization.findall(in_list, value)[source]

Find all the indices of a value in a list.

loom.eka.utilities.serialization.load(cls, file)[source]

Load a pydantic dataclass from a JSON file.

Parameters:
  • cls (dataclass) – The dataclass to be loaded from the JSON file

  • file (str) – Filename to read from

Return type:

An instance of cls containing the data from the JSON file

loom.eka.utilities.serialization.loads(cls, data_json)[source]

Load the a pydantic dataclass from a JSON string. If the dataclass defines its own custom loads() method, the custom method is used, giving the loaded dictionary as input.

Parameters:
  • cls (dataclass) – The dataclass to be loaded from the JSON string

  • data_json (str) – Dataclass saved as JSON string

Return type:

An instance of cls containing the data from the JSON string