State#

Sometimes it is useful to remember the current ‘state’ of your Python application. Using the PersistentDict class and an appropriate handler, key state parameters can be stored persistently at a remote location, and be available next time the application runs.

PersistentDict behaves similar to dictionaries, and can keep track of state parameters in key/value pairs.

from fourinsight.engineroom.utils import PersistentDict


state = PersistentDict(handler)

Values can be updated,

new_state = {"state parameter #1": 0, "state parameter #2": "some text value"}
state.update(new_state)

And retrieved,

state.get("state parameter #1")

As well as deleted, printed, etc…

# Delete
del state["state parameter #1]

# Print
print(state)

# etc...

To store the state for later, you simply just update the source with a push().

# Update remote source
state.push()

Then, the state is available next time you run your script by doing a pull().

# Update state from remote source
state.pull()