Reimplemented serialising LST

This commit is contained in:
Joel Collins 2020-02-06 13:35:05 +00:00
parent ccd5e9b891
commit cf66359559
5 changed files with 48 additions and 67 deletions

View file

@ -4,6 +4,7 @@ import operator
import base64
from uuid import UUID
import numpy as np
import logging
from collections import abc
from functools import reduce
from contextlib import contextmanager
@ -21,6 +22,27 @@ def serialise_array_b64(npy_arr):
return b64_string, dtype, shape
def ndarray_to_json(arr: np.ndarray):
b64_string, dtype, shape = serialise_array_b64(arr)
return {
"@type": "ndarray",
"dtype": dtype,
"shape": shape,
"base64": b64_string
}
def json_to_ndarray(json_dict: dict):
if not json_dict.get("@type") != "ndarray":
logging.warning("No valid @type attribute found. Conversion may fail.")
for required_param in ("dtype", "shape", "base64"):
if not json_dict.get(required_param):
raise KeyError(f"Missing required key {required_param}")
return deserialise_array_b64(json_dict.get("base64"), json_dict.get("dtype"), json_dict.get("shape"))
@contextmanager
def set_properties(obj, **kwargs):
"""A context manager to set, then reset, certain properties of an object.