{"id":28559204,"url":"https://github.com/toastdriven/definite","last_synced_at":"2025-06-22T09:34:22.397Z","repository":{"id":37074028,"uuid":"503626751","full_name":"toastdriven/definite","owner":"toastdriven","description":"Simple finite state machines.","archived":false,"fork":false,"pushed_at":"2022-07-07T00:47:26.000Z","size":67,"stargazers_count":28,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-06-15T19:12:58.578Z","etag":null,"topics":["machine","python","state","workflow"],"latest_commit_sha":null,"homepage":"https://definite.rtfd.io/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/toastdriven.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":"2022-06-15T05:26:00.000Z","updated_at":"2024-04-23T23:07:12.000Z","dependencies_parsed_at":"2022-06-24T19:34:56.954Z","dependency_job_id":null,"html_url":"https://github.com/toastdriven/definite","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/toastdriven/definite","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toastdriven%2Fdefinite","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toastdriven%2Fdefinite/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toastdriven%2Fdefinite/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toastdriven%2Fdefinite/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/toastdriven","download_url":"https://codeload.github.com/toastdriven/definite/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toastdriven%2Fdefinite/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261270258,"owners_count":23133524,"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":["machine","python","state","workflow"],"created_at":"2025-06-10T08:36:13.050Z","updated_at":"2025-06-22T09:34:17.382Z","avatar_url":"https://github.com/toastdriven.png","language":"Python","readme":"[![Documentation Status](https://readthedocs.org/projects/definite/badge/?version=latest)](https://definite.readthedocs.io/en/latest/?badge=latest)\n\n# `definite`\n\nSimple finite state machines.\n\nPerfect for representing workflows.\n\n\n## Quickstart\n\n```python\nfrom definite import FSM\n\n# You define all the valid states, as well as what their allowed\n# transitions are.\nclass Workflow(FSM):\n    allowed_transitions = {\n        \"draft\": [\"awaiting_review\", \"rejected\"],\n        \"awaiting_review\": [\"draft\", \"reviewed\", \"rejected\"],\n        \"reviewed\": [\"published\", \"rejected\"],\n        \"published\": None,\n        \"rejected\": [\"draft\"],\n    }\n    default_state = \"draft\"\n\n# Right away, you can use the states/transitions as-is to enforce changes.\nworkflow = Workflow()\nworkflow.current_state() # \"draft\"\n\nworkflow.transition_to(\"awaiting_review\")\nworkflow.transition_to(\"reviewed\")\n\nworkflow.is_allowed(\"published\") # True\n\n# Invalid/disallowed transitions will throw an exception.\nworkflow.current_state() # \"reviewed\"\n# ...which can only go to \"published\" or \"rejected\", but...\nworkflow.transition_to(\"awaiting_review\")\n# Traceback (most recent call last):\n# ...\n# workflow.TransitionNotAllowed: \"reviewed\" cannot transition to \"awaiting_review\"\n\n\n# Additionally, you can set up extra code to fire on given state changes.\nclass Workflow(FSM):\n    # Same transitions \u0026 default state.\n    allowed_transitions = {\n        \"draft\": [\"awaiting_review\", \"rejected\"],\n        \"awaiting_review\": [\"draft\", \"reviewed\", \"rejected\"],\n        \"reviewed\": [\"published\", \"rejected\"],\n        \"published\": None,\n        \"rejected\": [\"draft\"],\n    }\n    default_state = \"draft\"\n\n    # Define a `handle_\u003cstate_name\u003e` method on the class.\n    def handle_awaiting_review(self, new_state):\n        spell_check_results = check_spelling(self.obj.content)\n        msg = (\n            f\"{self.obj.title} ready for review. \"\n            f\"{len(spell_check_results)} spelling errors.\"\n        )\n        send_email(to=editor_email, message=msg)\n\n    def handle_published(self, new_state):\n        self.obj.pub_date = datetime.datetime.utcnow()\n        self.obj.save()\n\n    # You can also setup code that fires on **ANY** valid transition with the\n    # special `handle_any` method.\n    def handle_any(self, new_state):\n        self.obj.state = new_state\n        self.obj.save()\n\n\n# We can pull in any Python object, like a database-backed model, that we\n# want to associate with our FSM.\nfrom news.models import NewsPost\nnews_post = NewsPost.objects.create(\n    title=\"Hello world!\",\n    content=\"This iz our frist post!\",\n    state=\"draft\",\n)\n\n# We start mostly the same, but this time pass an `obj` kwarg!\nworkflow = Workflow(obj=news_post)\n\n# If you wanted to be explicit, you could also pass along the `initial_state`:\nworkflow = Workflow(\n    obj=news_post,\n    initial_state=news_post.state\n)\n\nworkflow.current_state() # \"draft\"\n\n# But when we trigger this change...\nworkflow.transition_to(\"awaiting_review\")\n# ...it triggers the spell check \u0026 the email we defined above, as well as\n# hitting the `handle_any` method \u0026 updating the `state` field in the DB.\nnews_post.refresh_from_db()\nnews_post.state # \"awaiting_review\" !\n```\n\n\n## Installation\n\n`pip install definite`\n\n\n## Requirements\n\n* Python 3.6+\n\n\n## Testing\n\n`$ pytest .`\n\n\n## License\n\nNew BSD\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftoastdriven%2Fdefinite","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftoastdriven%2Fdefinite","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftoastdriven%2Fdefinite/lists"}