{"id":20238501,"url":"https://github.com/marcinn/dsm","last_synced_at":"2025-04-10T19:35:12.134Z","repository":{"id":34552370,"uuid":"38497362","full_name":"marcinn/dsm","owner":"marcinn","description":"Damn simple finite state machine for Python","archived":false,"fork":false,"pushed_at":"2020-03-17T22:54:47.000Z","size":16,"stargazers_count":5,"open_issues_count":2,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-06T10:10:01.899Z","etag":null,"topics":["declarative","django","finite-state-machine","fsm","imperative","library","python","python2","python3","state-machine"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-4-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/marcinn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-07-03T14:43:09.000Z","updated_at":"2021-10-28T10:46:27.000Z","dependencies_parsed_at":"2022-08-03T21:31:06.235Z","dependency_job_id":null,"html_url":"https://github.com/marcinn/dsm","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcinn%2Fdsm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcinn%2Fdsm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcinn%2Fdsm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcinn%2Fdsm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/marcinn","download_url":"https://codeload.github.com/marcinn/dsm/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247653868,"owners_count":20973923,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["declarative","django","finite-state-machine","fsm","imperative","library","python","python2","python3","state-machine"],"created_at":"2024-11-14T08:34:30.604Z","updated_at":"2025-04-10T19:35:12.113Z","avatar_url":"https://github.com/marcinn.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# dsm\nDamn simple finite state machine\n\n[![Build Status](https://travis-ci.org/marcinn/dsm.svg?branch=master)](https://travis-ci.org/marcinn/dsm)\n\n## About\n\nDSM is a observable simple finite state machine implementation for Python.\nTransitions may be programmed declaratively or imperatively.\nInputs and state changes are emitting observable events.\n\n## Requirements\n\n  - Python 2.7, 3.5, 3.6\n  - ``observable``\n  - ``six`` for compatibility between Python 2 and Python 3\n\n## Installation\n\npip install dsm\n\n\n## Usage\n\n### Django integration\n\nIt is possible to integrate `dsm` with Django models by\ndeclaring a `StateMachineField`.\n\n```python\n\nfrom django.db import models\nfrom dsm.fields import StateMachineField\n\n\nclass Order(models.Model):\n    status = StateMachineField(\n        transitions=(\n            ('new', ['confirmed'], 'processing'),\n            ('processing', ['cancel'], 'cancelled'),\n            ('processing', ['send'], 'sending'),\n            ('sending', ['deliver'], 'finished'),\n        ),\n        max_length=16,\n        choices=(\n            ('new', _('New')),\n            ('processing', _('Processing')),\n            ('sending', _('Sending')),\n            ('finished', _('Finished')),\n            ('canceled', _('Cancelled')),\n        ),\n        db_index=True,\n        default='new'\n    )\n```\n\nNow you can create an `Order` and check it's status:\n\n```python\n\u003e\u003e\u003e order = Order.objects.create()\n\u003e\u003e\u003e order.status\nnew\n\u003e\u003e\u003e type(order.status)\ndsm.fields.MachineState\n```\n\nThe string representation of `status` field is same as state name\nprovided in transitions declaration, but internally there is always\n`dsm.fields.MachineState` instance.\n\n\n### Declarative\n\n\nFSM declaration:\n\n```python\nimport string\nimport dsm\n\nclass SumatorMachine(dsm.StateMachine):\n    class Meta:\n        initial = 'init'\n        transitions = (\n            ('init', list(string.digits), 'digit_enter'),\n            ('digit_enter', list(string.digits), 'digit_enter'),\n            ('digit_enter', '=', 'summarize'),\n        )\n```\n\n### Usage:\n\nInitialization:\n\n```python\nfsm = SumatorMachine()\n```\n\nProcessing one value:\n\n```python\nfsm.process(value)\n```\n\nProcessing multiple values:\n\n```python\nfsm.process_many(iterable)\n```\n\nGathering the current state:\n\n```python\n\u003e\u003e\u003e fsm.state\n'summarize'\n```\n\nResetting to the intial state:\n\n```python\nfsm.reset()\n```\n\nListening on events:\n\n```python\nfsm.when('state', func)\n```\n\nEvents example:\n\n```python\n\u003e\u003e\u003e the_sum = 0\n\n\u003e\u003e\u003e def add_digit(x): global the_sum; the_sum += int(x)\n\u003e\u003e\u003e def reset(x): global the_sum; the_sum = 0\n\n\u003e\u003e\u003e fsm = SumatorMachine()\n\u003e\u003e\u003e fsm.when('digit_enter', add_digit)\n\u003e\u003e\u003e fsm.when('init', reset)\n\n\u003e\u003e\u003e fsm.process_many('666=')\n'summarize'\n\n\u003e\u003e\u003e the_sum\n18\n```\n\nEvents example (class based):\n\n```python\n\u003e\u003e\u003e class Sumator(object):\n...     def __init__(self):\n...         self.total = 0\n...         self.fsm = SumatorMachine()\n...         self.fsm.when('digit_enter', self.add)\n...         self.fsm.when('init', self.reset)\n...\n...     def add(self, x):\n...         self.total += int(x)\n...\n...     def reset(self, x):\n...         self.total = 0\n...\n...     def summarize(self, values):\n...         self.fsm.reset()\n...         self.fsm.process_many(values+'=')\n...         return self.total\n\n\u003e\u003e\u003e s = Sumator()\n\u003e\u003e\u003e s.summarize('666')\n18\n```\n### Imperative\n\n```python\nimport string\nimport dsm\n\nfsm = dsm.StateMachine(\n        initial='init',\n        transitions=dsm.Transitions((\n                ('init', list(string.digits), 'digit_enter'),\n                ('digit_enter', list(string.digits), 'digit_enter'),\n                ('digit_enter', '=', 'summarize'),\n            ))\n        )\n```\n\n## License\n\nBSD\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarcinn%2Fdsm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarcinn%2Fdsm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarcinn%2Fdsm/lists"}