summaryrefslogtreecommitdiff
path: root/examples/statemachine/trafficLightDemo.py
diff options
context:
space:
mode:
authorPaul McGuire <ptmcg@austin.rr.com>2019-04-15 18:53:05 -0500
committerPaul McGuire <ptmcg@austin.rr.com>2019-04-15 18:53:05 -0500
commit6c447c4d663d0badddd84976033504c855325ce5 (patch)
tree81c7f74073236b499c3656f82a8179033c02f8b9 /examples/statemachine/trafficLightDemo.py
parentadf8dd00b736ba1934914e1db78528af02662b65 (diff)
downloadpyparsing-git-6c447c4d663d0badddd84976033504c855325ce5.tar.gz
Refactor generated State code to use overridden transition methods instead of overriding getattr; add generation of state-managing mixin class to delegate to _state instance variable, and reworked demos to use mixin instead of replicating state code
Diffstat (limited to 'examples/statemachine/trafficLightDemo.py')
-rw-r--r--examples/statemachine/trafficLightDemo.py12
1 files changed, 2 insertions, 10 deletions
diff --git a/examples/statemachine/trafficLightDemo.py b/examples/statemachine/trafficLightDemo.py
index 4d70541..8358750 100644
--- a/examples/statemachine/trafficLightDemo.py
+++ b/examples/statemachine/trafficLightDemo.py
@@ -8,21 +8,13 @@ import statemachine
import trafficlightstate
-class TrafficLight:
+class TrafficLight(trafficlightstate.TrafficLightStateMixin):
def __init__(self):
- # start light in Red state
- self._state = trafficlightstate.Red()
+ self.initialize_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 "{0}: {1}".format(self.__class__.__name__, self._state)
-
light = TrafficLight()
for i in range(10):