{ "info": { "author": "Catstyle", "author_email": "catstyle.lee@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 3 - Alpha", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3", "Topic :: Software Development :: Build Tools" ], "description": "yasm\n==============\n\nstate machine for humans\n\nThere are two types of developers in this world: those who love state\nmachines and those who *will* eventually.\n\nI fall in the first camp. I think it is really important to have a\ndeclarative way to define the states of an object. That\u2019s why I\ndeveloped ``yasm``.\n\nInstall\n-------\n\n.. code:: bash\n\n pip install yasm\n\nBasic Usage\n-----------\n\n.. code:: python\n\n from collections import deque\n import operator\n import six\n from string import whitespace, digits\n\n from yasm import Event, state_machine\n from yasm.utils import dispatch\n\n\n @state_machine('calculator')\n class Calculator(object):\n\n operators = {\n '+': operator.add,\n '-': operator.sub,\n '*': operator.mul,\n }\n if six.PY3:\n operators['/'] = operator.truediv\n else:\n operators['/'] = operator.div\n\n def __init__(self):\n self.stack = deque()\n self.result = None\n\n def reset(self):\n self.stack.clear()\n self.result = None\n self.machine.reinit_instance(self)\n\n def calculate(self, string):\n self.reset()\n for char in string:\n dispatch(self, Event('parse', input=char))\n return self.result\n\n def start_building_number(self, state, event, instance):\n digit = event.input\n self.stack.append(int(digit))\n\n def build_number(self, state, event, instance):\n digit = event.input\n number = str(self.stack.pop())\n number += digit\n self.stack.append(int(number))\n\n def do_operation(self, state, event, instance):\n operation = event.input\n y = self.stack.pop()\n x = self.stack.pop()\n self.stack.append(self.operators[operation](float(x), float(y)))\n\n def do_equal(self, state, event, instance):\n number = self.stack.pop()\n self.result = number\n\n\n def is_digit(state, event, instance):\n return event.input in digits\n\n\n sm = Calculator.machine\n sm.add_states(['initial', 'number', 'result'], initial='initial')\n\n sm.add_transitions([\n {'from_state': 'initial', 'to_state': 'number', 'event': 'parse',\n 'conditions': [is_digit], 'before': 'start_building_number'},\n {'from_state': 'number', 'to_state': 'number', 'event': 'parse',\n 'conditions': [is_digit], 'before': 'build_number'},\n {'from_state': 'number', 'to_state': 'initial', 'event': 'parse',\n 'conditions': [lambda state, evt, ins: evt.input in whitespace]},\n {'from_state': 'initial', 'to_state': 'initial', 'event': 'parse',\n 'conditions': [lambda state, evt, ins: evt.input in '+-*/'],\n 'before': 'do_operation'},\n {'from_state': 'initial', 'to_state': 'result', 'event': 'parse',\n 'conditions': [lambda state, evt, ins: evt.input == '='],\n 'before': 'do_equal'},\n ])\n\n\n calc = Calculator()\n for syntax, value in ((' 167 3 2 2 * * * 1 - =', 2003),\n (' 167 3 2 2 * * * 1 - 2 / =', 1001.5),\n (' 3 5 6 + * =', 33),\n (' 3 4 + =', 7),\n ('2 4 / 5 6 - * =', -0.5),):\n result = calc.calculate(syntax)\n assert result == value, (syntax, result, value)\n calc.reset()\n\n\nThank you\n---------\n\nto `aasm`_ and ruby\u2019s `state\\_machine`_ and jtushman's `jtushman/state\\_machine`_ and\nall other state machines that I loved before\n\n.. _aasm: https://github.com/aasm/aasm\n.. _state\\_machine: https://github.com/pluginaweek/state_machine\n.. _jtushman/state\\_machine: https://github.com/jtushman/state_machine\n\n\n", "description_content_type": "", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "http://github.com/Catstyle/yasm", "keywords": "state_machine", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "yasm", "package_url": "https://pypi.org/project/yasm/", "platform": "", "project_url": "https://pypi.org/project/yasm/", "project_urls": { "Homepage": "http://github.com/Catstyle/yasm" }, "release_url": "https://pypi.org/project/yasm/0.1.0a1/", "requires_dist": null, "requires_python": "", "summary": "Python State Machines for Humans", "version": "0.1.0a1" }, "last_serial": 4244011, "releases": { "0.1.0a1": [ { "comment_text": "", "digests": { "md5": "8d61c33752e5f9844fda039ba9098437", "sha256": "268aaee7a0e35da90183687c8393a41cba6d2bccfaecd37905386ede9e81976c" }, "downloads": -1, "filename": "yasm-0.1.0a1-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "8d61c33752e5f9844fda039ba9098437", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 13648, "upload_time": "2018-09-06T08:50:19", "url": "https://files.pythonhosted.org/packages/e3/a2/3a9e6b11dad093dcd0232b565b06fefa97e8c40bf7b834bb123dd989dc46/yasm-0.1.0a1-py2.py3-none-any.whl" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "8d61c33752e5f9844fda039ba9098437", "sha256": "268aaee7a0e35da90183687c8393a41cba6d2bccfaecd37905386ede9e81976c" }, "downloads": -1, "filename": "yasm-0.1.0a1-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "8d61c33752e5f9844fda039ba9098437", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 13648, "upload_time": "2018-09-06T08:50:19", "url": "https://files.pythonhosted.org/packages/e3/a2/3a9e6b11dad093dcd0232b565b06fefa97e8c40bf7b834bb123dd989dc46/yasm-0.1.0a1-py2.py3-none-any.whl" } ] }