narupatools.state.state_object¶
Generic implementation of a serializable object.
SharedStateObject is an implementation of a SerializableObject, that can store arbitrary data as a dictionary, but also automatically use any properties defined in the class. In most cases, this class is the appropriate one to subclass for defining a specific kind of object that’s storable in the shared state dictionary.
from narupa.state.state_object import SharedStateObject
# Define a custom class, with a property x that must be non-negative.
class MyObject(SharedStateObject):
_x: int
def __init__(self, **kwargs):
super().__init__(**kwargs)
x = 0
@property
def x(self) -> int:
return self._x
@x.setter
def x(self, value):
if value < 0:
value = 0
self._x = value
# Define a dictionary we want to deserialize
dictionary = {'x': -1, 'y': 2}
# Create the object using the class method load()
myobj = MyObject.load(dictionary)
# Check that the property setter was called
print(myobj['x']) # we could also use myobj.x
# Check that y was also stored
print(myobj['y'])
Classes
|
Base class for an object which is stored in the shared state dictionary. |