Skip to content
Snippets Groups Projects
Commit 10e312d6 authored by Jacob Wahlman's avatar Jacob Wahlman :ok_hand:
Browse files

Added save and load of data structures, updated w/ requirement

parent feab0166
No related branches found
No related tags found
2 merge requests!33Resolve "Load/Save SFG to file",!27Resolve "Load/Save SFG to file"
Pipeline #13764 passed
......@@ -4,12 +4,14 @@ This module contains functions that are used as utilities by other modules or by
"""
from typing import Optional
import pickle
from os import getcwd, path
from b_asic import AbstractOperation
import dill # Used instead of pickle to support serializing lambda functions
def save_structure(struct: AbstractOperation, path: Optional[str] = None) -> str:
def save_structure(struct: AbstractOperation, _path: Optional[str] = None, _name: Optional[str] = None) -> str:
"""Saves the structure to a specific path using the pickle module.
Returns the path to the struct if save succeeds.
......@@ -17,15 +19,35 @@ def save_structure(struct: AbstractOperation, path: Optional[str] = None) -> str
struct: The structure to save.
Keyword Arguments:
path: The path to which the structure will be saved.
_path: The path (str) to which the structure will be saved.
_name: The name (str) of the file to be saved. Only used if _path is not defined and is not None.
"""
pass
try:
_name = _name if _name is not None else f"{struct.type_name}.pickle"
if _path is None:
_path = path.join(getcwd(), _name)
def load_structure(path: str) -> AbstractOperation:
print(_path)
with open(_path, "wb") as handle:
dill.dump(struct, handle, protocol=dill.HIGHEST_PROTOCOL)
except Exception as e:
print("Unexpected error occured while saving structure: ", e)
return None
return _path
def load_structure(_path: str) -> AbstractOperation:
"""Saves the structure to a specific path using the pickle module.
Returns the struct that was loaded from the path.
Keyword Arguments:
path: The path to which the structure will be loaded from.
"""
pass
\ No newline at end of file
try:
with open(_path, "rb") as handle:
return dill.load(handle)
except Exception as e:
print("Unexpected error occured while saving structure: ", e)
return None
......@@ -71,7 +71,8 @@ setuptools.setup(
install_requires = [
"pybind11>=2.3.0",
"numpy",
"install_qt_binding"
"install_qt_binding",
"dill>=0.3.1.1"
],
packages = ["b_asic"],
ext_modules = [CMakeExtension("b_asic")],
......
......@@ -20,22 +20,4 @@ class TestSaveStructures:
_sfg = load_structure(path)
assert isinstance(_sfg, SFG)
assert sfg.components == _sfg.components
def test_save_sfg_custom_path(self, large_operation_tree):
sfg = SFG(outputs=[Output(large_operation_tree)])
path = save_structure(sfg, path="structures/test")
assert path is not None
assert isfile(path)
def test_load_sfg_custom_path(self, large_operation_tree):
sfg = SFG(outputs=[Output(large_operation_tree)])
path = save_structure(sfg, path="structures/test")
_sfg = load_structure(path)
assert isinstance(_sfg, SFG)
assert sfg.components == _sfg.components
assert sfg.evaluate() == _sfg.evaluate()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment