You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

460 lines
16 KiB

4 years ago
  1. Automat
  2. =======
  3. .. image:: https://readthedocs.org/projects/automat/badge/?version=stable
  4. :target: http://automat.readthedocs.io/en/latest/
  5. :alt: Documentation Status
  6. .. image:: https://travis-ci.org/glyph/automat.svg?branch=master
  7. :target: https://travis-ci.org/glyph/automat
  8. :alt: Build Status
  9. .. image:: https://coveralls.io/repos/glyph/automat/badge.png
  10. :target: https://coveralls.io/r/glyph/automat
  11. :alt: Coverage Status
  12. Self-service finite-state machines for the programmer on the go.
  13. ----------------------------------------------------------------
  14. Automat is a library for concise, idiomatic Python expression of finite-state
  15. automata (particularly deterministic finite-state transducers).
  16. Read more here, or on `Read the Docs <https://automat.readthedocs.io/>`_\ , or watch the following videos for an overview and presentation
  17. Overview and presentation by **Glyph Lefkowitz** at the first talk of the first Pyninsula meetup, on February 21st, 2017:
  18. .. image:: https://img.youtube.com/vi/0wOZBpD1VVk/0.jpg
  19. :target: https://www.youtube.com/watch?v=0wOZBpD1VVk
  20. :alt: Glyph Lefkowitz - Automat - Pyninsula #0
  21. Presentation by **Clinton Roy** at PyCon Australia, on August 6th 2017:
  22. .. image:: https://img.youtube.com/vi/TedUKXhu9kE/0.jpg
  23. :target: https://www.youtube.com/watch?v=TedUKXhu9kE
  24. :alt: Clinton Roy - State Machines - Pycon Australia 2017
  25. Why use state machines?
  26. ^^^^^^^^^^^^^^^^^^^^^^^
  27. Sometimes you have to create an object whose behavior varies with its state,
  28. but still wishes to present a consistent interface to its callers.
  29. For example, let's say you're writing the software for a coffee machine. It
  30. has a lid that can be opened or closed, a chamber for water, a chamber for
  31. coffee beans, and a button for "brew".
  32. There are a number of possible states for the coffee machine. It might or
  33. might not have water. It might or might not have beans. The lid might be open
  34. or closed. The "brew" button should only actually attempt to brew coffee in
  35. one of these configurations, and the "open lid" button should only work if the
  36. coffee is not, in fact, brewing.
  37. With diligence and attention to detail, you can implement this correctly using
  38. a collection of attributes on an object; ``has_water``\ , ``has_beans``\ ,
  39. ``is_lid_open`` and so on. However, you have to keep all these attributes
  40. consistent. As the coffee maker becomes more complex - perhaps you add an
  41. additional chamber for flavorings so you can make hazelnut coffee, for
  42. example - you have to keep adding more and more checks and more and more
  43. reasoning about which combinations of states are allowed.
  44. Rather than adding tedious 'if' checks to every single method to make sure that
  45. each of these flags are exactly what you expect, you can use a state machine to
  46. ensure that if your code runs at all, it will be run with all the required
  47. values initialized, because they have to be called in the order you declare
  48. them.
  49. You can read about state machines and their advantages for Python programmers
  50. in considerably more detail
  51. `in this excellent series of articles from ClusterHQ <https://clusterhq.com/blog/what-is-a-state-machine/>`_.
  52. What makes Automat different?
  53. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  54. There are
  55. `dozens of libraries on PyPI implementing state machines <https://pypi.org/search/?q=finite+state+machine>`_.
  56. So it behooves me to say why yet another one would be a good idea.
  57. Automat is designed around this principle: while organizing your code around
  58. state machines is a good idea, your callers don't, and shouldn't have to, care
  59. that you've done so. In Python, the "input" to a stateful system is a method
  60. call; the "output" may be a method call, if you need to invoke a side effect,
  61. or a return value, if you are just performing a computation in memory. Most
  62. other state-machine libraries require you to explicitly create an input object,
  63. provide that object to a generic "input" method, and then receive results,
  64. sometimes in terms of that library's interfaces and sometimes in terms of
  65. classes you define yourself.
  66. For example, a snippet of the coffee-machine example above might be implemented
  67. as follows in naive Python:
  68. .. code-block:: python
  69. class CoffeeMachine(object):
  70. def brew_button(self):
  71. if self.has_water and self.has_beans and not self.is_lid_open:
  72. self.heat_the_heating_element()
  73. # ...
  74. With Automat, you'd create a class with a ``MethodicalMachine`` attribute:
  75. .. code-block:: python
  76. from automat import MethodicalMachine
  77. class CoffeeBrewer(object):
  78. _machine = MethodicalMachine()
  79. and then you would break the above logic into two pieces - the ``brew_button``
  80. *input*\ , declared like so:
  81. .. code-block:: python
  82. @_machine.input()
  83. def brew_button(self):
  84. "The user pressed the 'brew' button."
  85. It wouldn't do any good to declare a method *body* on this, however, because
  86. input methods don't actually execute their bodies when called; doing actual
  87. work is the *output*\ 's job:
  88. .. code-block:: python
  89. @_machine.output()
  90. def _heat_the_heating_element(self):
  91. "Heat up the heating element, which should cause coffee to happen."
  92. self._heating_element.turn_on()
  93. As well as a couple of *states* - and for simplicity's sake let's say that the
  94. only two states are ``have_beans`` and ``dont_have_beans``\ :
  95. .. code-block:: python
  96. @_machine.state()
  97. def have_beans(self):
  98. "In this state, you have some beans."
  99. @_machine.state(initial=True)
  100. def dont_have_beans(self):
  101. "In this state, you don't have any beans."
  102. ``dont_have_beans`` is the ``initial`` state because ``CoffeeBrewer`` starts without beans
  103. in it.
  104. (And another input to put some beans in:)
  105. .. code-block:: python
  106. @_machine.input()
  107. def put_in_beans(self):
  108. "The user put in some beans."
  109. Finally, you hook everything together with the ``upon`` method of the functions
  110. decorated with ``_machine.state``\ :
  111. .. code-block:: python
  112. # When we don't have beans, upon putting in beans, we will then have beans
  113. # (and produce no output)
  114. dont_have_beans.upon(put_in_beans, enter=have_beans, outputs=[])
  115. # When we have beans, upon pressing the brew button, we will then not have
  116. # beans any more (as they have been entered into the brewing chamber) and
  117. # our output will be heating the heating element.
  118. have_beans.upon(brew_button, enter=dont_have_beans,
  119. outputs=[_heat_the_heating_element])
  120. To *users* of this coffee machine class though, it still looks like a POPO
  121. (Plain Old Python Object):
  122. .. code-block:: python
  123. >>> coffee_machine = CoffeeMachine()
  124. >>> coffee_machine.put_in_beans()
  125. >>> coffee_machine.brew_button()
  126. All of the *inputs* are provided by calling them like methods, all of the
  127. *outputs* are automatically invoked when they are produced according to the
  128. outputs specified to ``upon`` and all of the states are simply opaque tokens -
  129. although the fact that they're defined as methods like inputs and outputs
  130. allows you to put docstrings on them easily to document them.
  131. How do I get the current state of a state machine?
  132. --------------------------------------------------
  133. Don't do that.
  134. One major reason for having a state machine is that you want the callers of the
  135. state machine to just provide the appropriate input to the machine at the
  136. appropriate time, and *not have to check themselves* what state the machine is
  137. in. So if you are tempted to write some code like this:
  138. .. code-block:: python
  139. if connection_state_machine.state == "CONNECTED":
  140. connection_state_machine.send_message()
  141. else:
  142. print("not connected")
  143. Instead, just make your calling code do this:
  144. .. code-block:: python
  145. connection_state_machine.send_message()
  146. and then change your state machine to look like this:
  147. .. code-block:: python
  148. @_machine.state()
  149. def connected(self):
  150. "connected"
  151. @_machine.state()
  152. def not_connected(self):
  153. "not connected"
  154. @_machine.input()
  155. def send_message(self):
  156. "send a message"
  157. @_machine.output()
  158. def _actually_send_message(self):
  159. self._transport.send(b"message")
  160. @_machine.output()
  161. def _report_sending_failure(self):
  162. print("not connected")
  163. connected.upon(send_message, enter=connected, [_actually_send_message])
  164. not_connected.upon(send_message, enter=not_connected, [_report_sending_failure])
  165. so that the responsibility for knowing which state the state machine is in
  166. remains within the state machine itself.
  167. Input for Inputs and Output for Outputs
  168. ---------------------------------------
  169. Quite often you want to be able to pass parameters to your methods, as well as
  170. inspecting their results. For example, when you brew the coffee, you might
  171. expect a cup of coffee to result, and you would like to see what kind of coffee
  172. it is. And if you were to put delicious hand-roasted small-batch artisanal
  173. beans into the machine, you would expect a *better* cup of coffee than if you
  174. were to use mass-produced beans. You would do this in plain old Python by
  175. adding a parameter, so that's how you do it in Automat as well.
  176. .. code-block:: python
  177. @_machine.input()
  178. def put_in_beans(self, beans):
  179. "The user put in some beans."
  180. However, one important difference here is that *we can't add any
  181. implementation code to the input method*. Inputs are purely a declaration of
  182. the interface; the behavior must all come from outputs. Therefore, the change
  183. in the state of the coffee machine must be represented as an output. We can
  184. add an output method like this:
  185. .. code-block:: python
  186. @_machine.output()
  187. def _save_beans(self, beans):
  188. "The beans are now in the machine; save them."
  189. self._beans = beans
  190. and then connect it to the ``put_in_beans`` by changing the transition from
  191. ``dont_have_beans`` to ``have_beans`` like so:
  192. .. code-block:: python
  193. dont_have_beans.upon(put_in_beans, enter=have_beans,
  194. outputs=[_save_beans])
  195. Now, when you call:
  196. .. code-block:: python
  197. coffee_machine.put_in_beans("real good beans")
  198. the machine will remember the beans for later.
  199. So how do we get the beans back out again? One of our outputs needs to have a
  200. return value. It would make sense if our ``brew_button`` method returned the cup
  201. of coffee that it made, so we should add an output. So, in addition to heating
  202. the heating element, let's add a return value that describes the coffee. First
  203. a new output:
  204. .. code-block:: python
  205. @_machine.output()
  206. def _describe_coffee(self):
  207. return "A cup of coffee made with {}.".format(self._beans)
  208. Note that we don't need to check first whether ``self._beans`` exists or not,
  209. because we can only reach this output method if the state machine says we've
  210. gone through a set of states that sets this attribute.
  211. Now, we need to hook up ``_describe_coffee`` to the process of brewing, so change
  212. the brewing transition to:
  213. .. code-block:: python
  214. have_beans.upon(brew_button, enter=dont_have_beans,
  215. outputs=[_heat_the_heating_element,
  216. _describe_coffee])
  217. Now, we can call it:
  218. .. code-block:: python
  219. >>> coffee_machine.brew_button()
  220. [None, 'A cup of coffee made with real good beans.']
  221. Except... wait a second, what's that ``None`` doing there?
  222. Since every input can produce multiple outputs, in automat, the default return
  223. value from every input invocation is a ``list``. In this case, we have both
  224. ``_heat_the_heating_element`` and ``_describe_coffee`` outputs, so we're seeing
  225. both of their return values. However, this can be customized, with the
  226. ``collector`` argument to ``upon``\ ; the ``collector`` is a callable which takes an
  227. iterable of all the outputs' return values and "collects" a single return value
  228. to return to the caller of the state machine.
  229. In this case, we only care about the last output, so we can adjust the call to
  230. ``upon`` like this:
  231. .. code-block:: python
  232. have_beans.upon(brew_button, enter=dont_have_beans,
  233. outputs=[_heat_the_heating_element,
  234. _describe_coffee],
  235. collector=lambda iterable: list(iterable)[-1]
  236. )
  237. And now, we'll get just the return value we want:
  238. .. code-block:: python
  239. >>> coffee_machine.brew_button()
  240. 'A cup of coffee made with real good beans.'
  241. If I can't get the state of the state machine, how can I save it to (a database, an API response, a file on disk...)
  242. --------------------------------------------------------------------------------------------------------------------
  243. There are APIs for serializing the state machine.
  244. First, you have to decide on a persistent representation of each state, via the
  245. ``serialized=`` argument to the ``MethodicalMachine.state()`` decorator.
  246. Let's take this very simple "light switch" state machine, which can be on or
  247. off, and flipped to reverse its state:
  248. .. code-block:: python
  249. class LightSwitch(object):
  250. _machine = MethodicalMachine()
  251. @_machine.state(serialized="on")
  252. def on_state(self):
  253. "the switch is on"
  254. @_machine.state(serialized="off", initial=True)
  255. def off_state(self):
  256. "the switch is off"
  257. @_machine.input()
  258. def flip(self):
  259. "flip the switch"
  260. on_state.upon(flip, enter=off_state, outputs=[])
  261. off_state.upon(flip, enter=on_state, outputs=[])
  262. In this case, we've chosen a serialized representation for each state via the
  263. ``serialized`` argument. The on state is represented by the string ``"on"``\ , and
  264. the off state is represented by the string ``"off"``.
  265. Now, let's just add an input that lets us tell if the switch is on or not.
  266. .. code-block:: python
  267. @_machine.input()
  268. def query_power(self):
  269. "return True if powered, False otherwise"
  270. @_machine.output()
  271. def _is_powered(self):
  272. return True
  273. @_machine.output()
  274. def _not_powered(self):
  275. return False
  276. on_state.upon(query_power, enter=on_state, outputs=[_is_powered],
  277. collector=next)
  278. off_state.upon(query_power, enter=off_state, outputs=[_not_powered],
  279. collector=next)
  280. To save the state, we have the ``MethodicalMachine.serializer()`` method. A
  281. method decorated with ``@serializer()`` gets an extra argument injected at the
  282. beginning of its argument list: the serialized identifier for the state. In
  283. this case, either ``"on"`` or ``"off"``. Since state machine output methods can
  284. also affect other state on the object, a serializer method is expected to
  285. return *all* relevant state for serialization.
  286. For our simple light switch, such a method might look like this:
  287. .. code-block:: python
  288. @_machine.serializer()
  289. def save(self, state):
  290. return {"is-it-on": state}
  291. Serializers can be public methods, and they can return whatever you like. If
  292. necessary, you can have different serializers - just multiple methods decorated
  293. with ``@_machine.serializer()`` - for different formats; return one data-structure
  294. for JSON, one for XML, one for a database row, and so on.
  295. When it comes time to unserialize, though, you generally want a private method,
  296. because an unserializer has to take a not-fully-initialized instance and
  297. populate it with state. It is expected to *return* the serialized machine
  298. state token that was passed to the serializer, but it can take whatever
  299. arguments you like. Of course, in order to return that, it probably has to
  300. take it somewhere in its arguments, so it will generally take whatever a paired
  301. serializer has returned as an argument.
  302. So our unserializer would look like this:
  303. .. code-block:: python
  304. @_machine.unserializer()
  305. def _restore(self, blob):
  306. return blob["is-it-on"]
  307. Generally you will want a classmethod deserialization constructor which you
  308. write yourself to call this, so that you know how to create an instance of your
  309. own object, like so:
  310. .. code-block:: python
  311. @classmethod
  312. def from_blob(cls, blob):
  313. self = cls()
  314. self._restore(blob)
  315. return self
  316. Saving and loading our ``LightSwitch`` along with its state-machine state can now
  317. be accomplished as follows:
  318. .. code-block:: python
  319. >>> switch1 = LightSwitch()
  320. >>> switch1.query_power()
  321. False
  322. >>> switch1.flip()
  323. []
  324. >>> switch1.query_power()
  325. True
  326. >>> blob = switch1.save()
  327. >>> switch2 = LightSwitch.from_blob(blob)
  328. >>> switch2.query_power()
  329. True
  330. More comprehensive (tested, working) examples are present in ``docs/examples``.
  331. Go forth and machine all the state!