Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] [Request] Implement State History #313

Closed
doanguyen opened this issue Sep 23, 2018 · 4 comments
Closed

[Feature] [Request] Implement State History #313

doanguyen opened this issue Sep 23, 2018 · 4 comments

Comments

@doanguyen
Copy link

As the title, it would be super helpful but I have not come up with a feasible idea yet, but people in event sourcing has done it before. Do you have time to implement or discuss it?

Btw, thanks a lot for a very good job!

@aleneum
Copy link
Member

aleneum commented Sep 24, 2018

Hi @doanguyen,

what are you looking for? Just a list of state names? Since the stateful object with transitions is the model you can use the state property to track state changes:

from transitions import Machine
import collections


# if you like your machine to act as a model, let Model inherit from Machine
class Model(object):

    def __init__(self, history_length):
        self.state_history = collections.deque(maxlen=history_length)

    @property
    def state(self):
        return self.state_history[-1]

    @state.setter
    def state(self, value):
        self.state_history.append(value)


model = Model(3)
machine = Machine(model, states=['A', 'B', 'C', 'D'], initial='A')
print(model.state)  # >>> A

model.to_B()
model.to_C()
model.to_A()
model.to_D()

print("->".join(model.state_history))  # >>> C->A->D

@doanguyen
Copy link
Author

Hi,
I'm actually looking for a way of storing states and its transitions into database and re-initialize later.
After a while, I think storing events with triggered time could work as well.

@aleneum
Copy link
Member

aleneum commented Sep 24, 2018

I'm actually looking for a way of storing states and its transitions into database and re-initialize later

States are persistent objects maintained by a Machine. States can be pickled (serialized) and deserialized if necessary. However, the State objects themselves do not (or should not) contain actual state related properties. They are rather 'blueprints' to determine what should happen with a 'stateful' object which is a model.

You might want to have a look at this issue or this notebook to get an idea how models and database table schemes can be connected.
This way you can save and load complete model/object states.

@doanguyen
Copy link
Author

Thanks a bunch! I've got it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants