{ "info": { "author": "Ketan Patel", "author_email": "ketan86ecer@gmail.com", "bugtrack_url": null, "classifiers": [ "License :: OSI Approved :: MIT License", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7" ], "description": "# Finite Transactional State Machine \nFinite Transactional State Machine is a Transaction driven finite state machine. \nTransaction can be any Python callable object that is reverted when exceptions \noccur.\n\n## Installation \n`pip3 install ftsm`\n\n\n## How does it work ?\n\n1. Create states and list of possible transitions the state is allowed \nto transition to.\n ```python\n UNLOCKED = State('UNLOCKED', initial=True, allowed_transitions=['LOCKED'])\n LOCKED = State('LOCKED', initial=False, allowed_transitions=['UNLOCKED'])\n ```\n2. Initialize the transitional state machine.\n ```python\n tsm = TransactionalFiniteStateMachine(name='Lock')\n ```\n3. Add defined states to a state machine.\n ```python\n tsm.add(LOCKED)\n tsm.add(UNLOCKED)\n ```\n4. Create transaction and define rollback transactions with or without conditions.\n ```python\n t1 = Transaction(\n target=func,\n args=('name',),\n rb_transactions=[t2],\n rb_conditions=[ExceptionCondition(KeyError)])\n ```\n5. Transition to a new state with transactions.\n ```python\n with tsm.managed_transition(\n state=LOCKED,\n pre_transactions=[t1, t3],\n on_error_transactions=[t4],\n post_transactions=[t5]):\n func()\n ```\n## Example \n\n```python\nfrom ftsm import State, Transaction, TransactionalFiniteStateMachine\n\nclass LightController:\n def turn_off_light(self, room):\n print('turning the {} room light off.'.format(room))\n\n def turn_on_light(self, room):\n print('turning the {} room light on.'.format(room))\n\nlight_controller = LightController()\n\ndef turn_off_water():\n print('turning off the water.')\n\ndef turn_on_water():\n print('turning on the water.')\n\ndef water_plants():\n print('watering the plants.')\n\ndef lock_the_door():\n print('locking the door.')\n\ndef unlock_the_door():\n print('unlocking the door.')\n\nUNLOCKED = State('UNLOCKED', initial=True, allowed_transitions=['LOCKED'])\nLOCKED = State('LOCKED', initial=False, allowed_transitions=['UNLOCKED'])\n\ntsm = TransactionalFiniteStateMachine(name='Lock')\ntsm.add(LOCKED)\ntsm.add(UNLOCKED)\n\nlight_transaction = Transaction(\n target=light_controller.turn_off_light,\n args=('Living',),\n rb_transactions=[\n Transaction(target=light_controller.turn_on_light,\n args=('Living',))\n ])\n\nwater_transaction = Transaction(\n target=turn_off_water,\n rb_transactions=[\n Transaction(target=turn_on_water)\n ]\n)\n\nwith tsm.managed_transition(\n state=LOCKED,\n pre_transactions=[light_transaction, water_transaction],\n on_error_transactions=[Transaction(unlock_the_door)],\n post_transactions=[Transaction(water_plants)]):\n lock_the_door()\n\nprint(tsm.current_state)\n```\n\nAbove sample code would result in following output. \n```python\nturning the Living room light off.\nturning off the water.\nlocking the door.\nwatering the plants.\n\n```\n\nIf errors occur while performing the transactions, revert transactions are performed \nin the reverse order and state transition does not happens.\n\nRollback transaction can also be made conditional using the `ExceptionCondition`\nclass provided. \n\n```\nlight_transaction = Transaction(\n target=light_controller.turn_off_light,\n args=('Living',),\n rb_transactions=[\n Transaction(target=light_controller.turn_on_light,\n args=('Living',))\n ],\n rb_conditions=[ExceptionCondition(KeyError)])\n```\nAbove transaction now only be reverted if KeyError is encountered during the\ntransaction execution. \n\nUser can extend the abstract `Condition` class to defined new Condition.\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/ketan86/ftsm", "keywords": "ftsm package", "license": "MIT License", "maintainer": "", "maintainer_email": "", "name": "ftsm", "package_url": "https://pypi.org/project/ftsm/", "platform": "", "project_url": "https://pypi.org/project/ftsm/", "project_urls": { "Homepage": "https://github.com/ketan86/ftsm" }, "release_url": "https://pypi.org/project/ftsm/0.1.0/", "requires_dist": null, "requires_python": "", "summary": "A Finite Transactional State Machine.", "version": "0.1.0" }, "last_serial": 5528515, "releases": { "0.1.0": [ { "comment_text": "", "digests": { "md5": "8e41bcf2e5882c3d023ccd68fa63e042", "sha256": "64f7dc795c4b7cf01bedd9fdfd91f0dc1d89497b23d246a9bd9932e494a49a38" }, "downloads": -1, "filename": "ftsm-0.1.0-py3-none-any.whl", "has_sig": false, "md5_digest": "8e41bcf2e5882c3d023ccd68fa63e042", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 7763, "upload_time": "2019-07-14T00:13:27", "url": "https://files.pythonhosted.org/packages/07/07/2f9f780cd379ef8ae093a1256d1107ba37f0d2657dd6127d31ed7c6b6f72/ftsm-0.1.0-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "cdf0529c8bddf9ad34bf2d6c72ff7513", "sha256": "84303d9a975d54473116df46605f35a514d6eabcfde346590d6a4a64c5ae2153" }, "downloads": -1, "filename": "ftsm-0.1.0.tar.gz", "has_sig": false, "md5_digest": "cdf0529c8bddf9ad34bf2d6c72ff7513", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7153, "upload_time": "2019-07-14T00:13:29", "url": "https://files.pythonhosted.org/packages/48/17/92669b96219ab2ec6e400a0c42743acd6cae2cb3ef83436ad61cf0af3c62/ftsm-0.1.0.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "8e41bcf2e5882c3d023ccd68fa63e042", "sha256": "64f7dc795c4b7cf01bedd9fdfd91f0dc1d89497b23d246a9bd9932e494a49a38" }, "downloads": -1, "filename": "ftsm-0.1.0-py3-none-any.whl", "has_sig": false, "md5_digest": "8e41bcf2e5882c3d023ccd68fa63e042", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 7763, "upload_time": "2019-07-14T00:13:27", "url": "https://files.pythonhosted.org/packages/07/07/2f9f780cd379ef8ae093a1256d1107ba37f0d2657dd6127d31ed7c6b6f72/ftsm-0.1.0-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "cdf0529c8bddf9ad34bf2d6c72ff7513", "sha256": "84303d9a975d54473116df46605f35a514d6eabcfde346590d6a4a64c5ae2153" }, "downloads": -1, "filename": "ftsm-0.1.0.tar.gz", "has_sig": false, "md5_digest": "cdf0529c8bddf9ad34bf2d6c72ff7513", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7153, "upload_time": "2019-07-14T00:13:29", "url": "https://files.pythonhosted.org/packages/48/17/92669b96219ab2ec6e400a0c42743acd6cae2cb3ef83436ad61cf0af3c62/ftsm-0.1.0.tar.gz" } ] }