summaryrefslogtreecommitdiff
path: root/examples/statemachine/trafficLightDemo.py
diff options
context:
space:
mode:
authorPaul McGuire <ptmcg@austin.rr.com>2018-12-31 01:49:22 -0600
committerPaul McGuire <ptmcg@austin.rr.com>2018-12-31 01:49:22 -0600
commit5132a91c470a8b6c34c7f0525c0bf41b9365e817 (patch)
treeb9d36e4a41dc08c5db03f3c99bd2113305ff1a1f /examples/statemachine/trafficLightDemo.py
parentb5bdb59d39c9202899bb6302095028a979089ba8 (diff)
downloadpyparsing-git-5132a91c470a8b6c34c7f0525c0bf41b9365e817.tar.gz
Update statemachine demo code to Py3
Diffstat (limited to 'examples/statemachine/trafficLightDemo.py')
-rw-r--r--examples/statemachine/trafficLightDemo.py37
1 files changed, 30 insertions, 7 deletions
diff --git a/examples/statemachine/trafficLightDemo.py b/examples/statemachine/trafficLightDemo.py
index 30fe934..ea42180 100644
--- a/examples/statemachine/trafficLightDemo.py
+++ b/examples/statemachine/trafficLightDemo.py
@@ -1,12 +1,35 @@
+#
+# trafficLightDemo.py
+#
+# Example of a simple state machine modeling the state of a traffic light
+#
+
import statemachine
-import trafficlight
+import trafficlightstate
+
+
+class TrafficLight:
+ def __init__(self):
+ # start light in Red state
+ self._state = trafficlightstate.Red()
+
+ def change(self):
+ self._state = self._state.next_state()
+
+ # get light behavior/properties from current state
+ def __getattr__(self, attrname):
+ return getattr(self._state, attrname)
+
+ def __str__(self):
+ return "{}: {}".format(self.__class__.__name__, self._state)
+
-tl = trafficLight.Red()
+light = TrafficLight()
for i in range(10):
- print(tl, end='')
- print(("STOP", "GO")[tl.carsCanGo])
- tl.crossingSignal()
- tl.delay()
+ print(light, end=' ')
+ print(("STOP", "GO")[light.carsCanGo])
+ light.crossingSignal()
+ light.delay()
print()
- tl = tl.nextState()
+ light.change()