{ "info": { "author": "Len Wanger", "author_email": "len_wanger@hotmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 5 - Production/Stable", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python", "Programming Language :: Python :: 2.5", "Programming Language :: Python :: 2.6", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3.3", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6" ], "description": "FiniteStateMachine - A finite state machine class using coroutines\r\n\r\n FiniteStateMachine is a class representing a finite state machine. Each state is represented by an instance of the\r\n State class. Each state also has a state handler function defined for it. The handler function is a co-routine that\r\n excepts an event and performs an action based on it.\r\n\r\n To define the state machine:\r\n\r\n 1) define the states (e.g. STATE_A = State('STATE_A'))\r\n 2) define the state handler functions. See below for the structure of a state handling function.\r\n 3) create an instance of the state machine (e.g. fsm = Fsm())\r\n 4) add states to the state machine, including exactly one state marked as the initial state. Each state also takes\r\n a sequence of states that it can be transitioned from (from_states).\r\n 5) call the start function on the FSM (e.g. fsm.start())\r\n\r\n For each event you will need to call the dispatch_event function (e.g. fsm.dispatch_event()) to route the event\r\n to the co-routine. An event can be anything you want (e.g. a tuple with event_id and arguments). The main loops\r\n generally looks like:\r\n\r\n try:\r\n while True:\r\n event = get_next_event()\r\n fsm.dispatch_event(event)\r\n except ExpectedExit as e:\r\n pass\r\n\r\n The basic structure of a state handler is:\r\n\r\n def state_handler_(fsm):\r\n # Enter the main loop for the co-routine\r\n while True:\r\n event = yield\r\n\r\n if event == 'EVENT_1':\r\n # Transition to another state\r\n fsm.transition_to(STATE_X)\r\n elif event == 'EVENT_2':\r\n # Do some processing but stay in this state\r\n print('Got EVENT_2')\r\n elif event == 'TERMINATING_EVENT':\r\n raise FsmExit\r\n else:\r\n print('Unrecognized event (%s)' % event)\r\n\r\n A simple example of this is shown in the turnstile_test.py test case.\r\n\r\n For convenience this can be wrapped with a @state_handler decorator. The decorator takes care of the co-routine\r\n boiler plate and hands the handler function an fsm and event. This would look like:\r\n\r\n @pystate.state_handler\r\n def state_locked_handler(event, fsm):\r\n if event == 'EVENT_1':\r\n # Transition to another state\r\n fsm.transition_to(STATE_X)\r\n elif event == 'EVENT_2':\r\n # Do some processing but stay in this state\r\n print('Got EVENT_2')\r\n elif event == 'TERMINATING_EVENT':\r\n raise FsmExit\r\n else:\r\n print('Unrecognized event (%s)' % event)\r\n\r\n There are two ways to handle a state that needs to keep persistant data. You can create a callable clas (i.e. define\r\n the __call__ dunder method to call as the state handler.) This allows you to use the state_handler decorator around\r\n the __call__ method. Alternatively, you can set the state data above the while loop if you define the co-routine by\r\n hand, however, this precludes using the decorator. See the callable_test.py test case for an example.\r\n\r\nAuthor: Len Wanger\r\nLast Updated: 7/7/2016\r\nCopyright (c) 2016 Len Wanger\r\n\r\nPermission is hereby granted, free of charge, to any person obtaining a copy\r\nof this software and associated documentation files (the \"Software\"), to deal\r\nin the Software without restriction, including without limitation the rights\r\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r\ncopies of the Software, and to permit persons to whom the Software is\r\nfurnished to do so, subject to the following conditions:\r\n\r\nThe above copyright notice and this permission notice shall be included in all\r\ncopies or substantial portions of the Software.\r\n\r\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r\nSOFTWARE.\r\n\r\nNote: Substantial code was adapted from Christian Maugg's pystatemachine code\r\nCopyright (c) 2015 Christian Maugg\r\n(https://raw.githubusercontent.com/cmaugg/pystatemachine/master/pystatemachine.py)\r\n", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/lwanger/pystate", "keywords": "state machine,coroutine,co-routine", "license": "MIT", "maintainer": null, "maintainer_email": null, "name": "pystate", "package_url": "https://pypi.org/project/pystate/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/pystate/", "project_urls": { "Download": "UNKNOWN", "Homepage": "https://github.com/lwanger/pystate" }, "release_url": "https://pypi.org/project/pystate/0.1.1/", "requires_dist": null, "requires_python": null, "summary": "Python package for co-routine base state machines.", "version": "0.1.1" }, "last_serial": 2209309, "releases": { "0.0.1": [ { "comment_text": "", "digests": { "md5": "b64afe8521ee5b5daf29e0ac839df0cd", "sha256": "aed4205046af1ac332d0c2979f31f3ae44173fe8f343408148904304c8c7a728" }, "downloads": -1, "filename": "pystate-0.0.1.zip", "has_sig": false, "md5_digest": "b64afe8521ee5b5daf29e0ac839df0cd", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7609, "upload_time": "2016-07-08T05:28:19", "url": "https://files.pythonhosted.org/packages/0b/c2/359f0d7123f892fb53dcf885dd4eca5a775bbdeef5d5fcf9a1fc5d112dae/pystate-0.0.1.zip" } ], "0.1.0": [ { "comment_text": "", "digests": { "md5": "755eaac62cd66e73f152a2810bd1d563", "sha256": "50c15116ab0f6ec77d39e384b56ec59eb10576dbb9c26f705b198be16c7431e0" }, "downloads": -1, "filename": "pystate-0.1.0.zip", "has_sig": false, "md5_digest": "755eaac62cd66e73f152a2810bd1d563", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9005, "upload_time": "2016-07-08T05:39:53", "url": "https://files.pythonhosted.org/packages/bb/53/e54c667ce26f73059a609d7c2c57e9c1bdc0e31aac9a5c8a57012dbf7875/pystate-0.1.0.zip" } ], "0.1.1": [ { "comment_text": "", "digests": { "md5": "0810dc1dd9e7bcd61b861c16ff742ce6", "sha256": "a51d9b3189155e7dd681e032d17dbae82b0a9805467f742d873259eaf5ee5f01" }, "downloads": -1, "filename": "pystate-0.1.1.zip", "has_sig": false, "md5_digest": "0810dc1dd9e7bcd61b861c16ff742ce6", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9001, "upload_time": "2016-07-08T06:24:23", "url": "https://files.pythonhosted.org/packages/98/05/02bfb21b4d2e35210fe1836b4375ba27c82e5de840347b82e43dcf4a4002/pystate-0.1.1.zip" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "0810dc1dd9e7bcd61b861c16ff742ce6", "sha256": "a51d9b3189155e7dd681e032d17dbae82b0a9805467f742d873259eaf5ee5f01" }, "downloads": -1, "filename": "pystate-0.1.1.zip", "has_sig": false, "md5_digest": "0810dc1dd9e7bcd61b861c16ff742ce6", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9001, "upload_time": "2016-07-08T06:24:23", "url": "https://files.pythonhosted.org/packages/98/05/02bfb21b4d2e35210fe1836b4375ba27c82e5de840347b82e43dcf4a4002/pystate-0.1.1.zip" } ] }