blob: d0f0a35e977150fc886e012fe6fbc5252f7722a8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
#
# trafficlightstate.pystate
#
# state machine model of the states and associated behaviors and properties for each
# different state of a traffic light
# define state machine with transitions
# (states will be implemented as Python classes, so use name case appropriate for class names)
statemachine TrafficLightState:
Red -> Green
Green -> Yellow
Yellow -> Red
# statemachine only defines the state->state transitions - actual behavior and properties
# must be added separately
# define some class level constants
Red.carsCanGo = False
Yellow.carsCanGo = True
Green.carsCanGo = True
# setup some class level methods
def flashCrosswalk(s):
def flash():
print("%s...%s...%s" % (s, s, s))
return flash
Red.crossingSignal = staticmethod(flashCrosswalk("WALK"))
Yellow.crossingSignal = staticmethod(flashCrosswalk("DONT WALK"))
Green.crossingSignal = staticmethod(flashCrosswalk("DONT WALK"))
# setup some instance methods
def wait(nSeconds):
def waitFn(self):
print("<wait %d seconds>" % nSeconds)
return waitFn
Red.delay = wait(20)
Yellow.delay = wait(3)
Green.delay = wait(15)
|