SharedStateReference

class narupatools.state.view.reference.SharedStateReference(view: MutableMapping[str, Union[None, str, int, float, bool, narupatools.state.typing.SerializableIterable, narupatools.state.typing.SerializableMapping]], key: str, snapshot_class: Optional[Type[_TSpecificType]] = None)

Represents a reference to an item in a shared state dictionary.

As with the SharedStateView, the current value of the reference can be obtained by using snapshot(), which returns a deep copy of the current value.

The value may also be modified, either by directly calling the update() function with a new value, or by using the modify() context manager:

# takes a snapshot and returns the current value
with reference.modify() as current_value:
    # modify the value directly
    current_value.x = 2
# when the block ends, the value is automatically updated

Another feature of a SharedStateReference is that it can optionally be passed a type which inherits from SerializableObject. Setting values in a dictionary from a SerializableObject is fine, as we know what the type is and can call save().

However, to read a value from a dictionary as a certain object, we need to know what kind of object to interpret it as. This is the point of the snapshot_class argument. Whenever you obtain a snapshot of a value using a reference with this defined, the returned value will be of the provided type.

Inheritance

Inheritance diagram of SharedStateReference

Methods

__init__

Create a new reference to a shared state object.

has_value

Does this reference currently have a value in the shared state dictionary?

modify

Get the current value, and apply changes after the scope is over.

remove

Remove the key this reference references from the shared state.

set

Update the current value.

snapshot

Return a snapshot of the current value of the item.

typed_reference

Get a reference to the specified key, represented as the specified type.

untyped_reference

Get a reference to the specified key, without assuming its type.

update

Update keys of this value.

__init__(view: MutableMapping[str, Union[None, str, int, float, bool, narupatools.state.typing.SerializableIterable, narupatools.state.typing.SerializableMapping]], key: str, snapshot_class: Optional[Type[_TSpecificType]] = None)

Create a new reference to a shared state object.

Parameters
  • view – A view of the shared state dictionary that handles getting, updating and removing keys.

  • key – The key in the shared state dictionary that this reference references.

  • snapshot_class – The class that snapshots of this reference should implement.

has_value()bool

Does this reference currently have a value in the shared state dictionary?

modify()Iterator[_TValue]

Get the current value, and apply changes after the scope is over.

remove()None

Remove the key this reference references from the shared state.

Raises

KeyError – If this reference doesn’t have a value.

set(snapshot: Optional[_TValue] = None)None

Update the current value.

snapshot()_TValue

Return a snapshot of the current value of the item.

Raises

KeyError – The shared state does not currently have a value with this key

classmethod typed_reference(view: MutableMapping[str, Union[None, str, int, float, bool, narupatools.state.typing.SerializableIterable, narupatools.state.typing.SerializableMapping]], key: str, snapshot_class: Type[_TSpecificType], /)narupatools.state.view.reference.SharedStateReference[_TSpecificType]

Get a reference to the specified key, represented as the specified type.

classmethod untyped_reference(view: MutableMapping[str, Union[None, str, int, float, bool, narupatools.state.typing.SerializableIterable, narupatools.state.typing.SerializableMapping]], key: str, /)narupatools.state.view.reference.SharedStateReference[Union[None, str, int, float, bool, narupatools.state.typing.SerializableIterable, narupatools.state.typing.SerializableMapping]]

Get a reference to the specified key, without assuming its type.

update(**kwargs: Union[None, str, int, float, bool, narupatools.state.typing.SerializableIterable, narupatools.state.typing.SerializableMapping])None

Update keys of this value.