| For typical use cases, you're probably persisting the data, so I'll assume your use case is "Python with a database ORM like Django or SQLAlchemy". The really short version of "use statemachine" is: store a single, explicit state for every 'thing' and create a log row every time that state changes. - Make a python model / database table to represent "the things whose state can change" (like an Order or a Task). - Give each a unique id and a 'state' column that is either a text field or an enumerated type (or a foreign key to a 'valid_states' table if you want to get fancy). Do this _instead_ of adding boolean columns like 'is_completed' or 'is_deleted'. - Create a log table with the same columns as your Order table -- whenever you update an Order's state (or create a new one), add a new row to the log table with the new state and a timestamp. Now your 'Order' table shows the current state of every Order, while your log table shows every state that every Order has ever been in, which will come in _super_ handy for analytics later. Everything else can be built on top of the above: - defining valid states and their transitions (aka formalizing the state machine) - preventing 'invalid' transitions if you want - creating analytics tables to show the 'typical' flow of an Order (e.g. columns like 'creation_date', 'payment_date', 'ship_date', 'return_date') - triggering other systems when Orders enter or leave a given state
- etc |