File: trafficlightstate.pystate

package info (click to toggle)
pyparsing 3.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 12,200 kB
  • sloc: python: 30,867; ansic: 422; sh: 112; makefile: 24
file content (47 lines) | stat: -rw-r--r-- 1,141 bytes parent folder | download | duplicates (8)
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.cars_can_go = False
Yellow.cars_can_go = True
Green.cars_can_go = True


# setup some class level methods
def flash_crosswalk(s):
    def flash():
        print(f"{s}...{s}...{s}")

    return flash

Red.crossing_signal = staticmethod(flash_crosswalk("WALK"))
Yellow.crossing_signal = staticmethod(flash_crosswalk("DONT WALK"))
Green.crossing_signal = staticmethod(flash_crosswalk("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)