summaryrefslogtreecommitdiff
path: root/examples/statemachine/trafficlight.pystate
blob: 3de7b7f1a3a02d1ee72e3d8f8b7a262fee1b0cbd (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
# define state machine
statemachine TrafficLight:
    Red -> Green
    Green -> Yellow
    Yellow -> Red


# 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)