Skip to content

Commit

Permalink
Merge pull request #67 from offby1/master
Browse files Browse the repository at this point in the history
Braino: next => itemgetter(0)
  • Loading branch information
markrwilliams committed May 21, 2017
2 parents 6abd8b6 + 301be8d commit fc6eccb
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions docs/examples/lightswitch.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from operator import itemgetter

from automat import MethodicalMachine


class LightSwitch(object):
machine = MethodicalMachine()

@machine.state(serialized="on")
def on_state(self):
"the switch is on"
Expand All @@ -13,27 +17,37 @@ def flip(self):
"flip the switch"
on_state.upon(flip, enter=off_state, outputs=[])
off_state.upon(flip, enter=on_state, outputs=[])

@machine.input()
def query_power(self):
"return True if powered, False otherwise"
@machine.output()
def _is_powered(self):
return True

@machine.output()
def _not_powered(self):
return False
on_state.upon(query_power, enter=on_state, outputs=[_is_powered],
collector=next)
collector=itemgetter(0))
off_state.upon(query_power, enter=off_state, outputs=[_not_powered],
collector=next)
collector=itemgetter(0))

@machine.serializer()
def save(self, state):
return {"is-it-on": state}

@machine.unserializer()
def _restore(self, blob):
return blob["is-it-on"]

@classmethod
def from_blob(cls, blob):
self = cls()
self._restore(blob)
return self


if __name__ == "__main__":
l = LightSwitch()
print(l.query_power())

0 comments on commit fc6eccb

Please sign in to comment.