Traffic Light Control
I want to make a Traffic Light that changes color to control traffic.
preview
These are the tests I have at the end of the chapter
1import src.traffic_light
2import unittest
3
4
5GREEN, YELLOW, RED = 'GREEN', 'YELLOW', 'RED'
6
7
8class TestTrafficLight(unittest.TestCase):
9
10 def test_parallel_green_cross_red_timer_not_done(self):
11 self.assertEqual(
12 src.traffic_light.control(
13 red_phase='cross',
14 current_parallel=GREEN,
15 current_cross=RED,
16 timer_done=False,
17 ),
18 (GREEN, RED)
19 )
20
21 def test_parallel_green_cross_red_timer_done(self):
22 self.assertEqual(
23 src.traffic_light.control(
24 red_phase='cross',
25 current_parallel=GREEN,
26 current_cross=RED,
27 timer_done=True,
28 ),
29 (YELLOW, RED)
30 )
32 def test_parallel_yellow_cross_red_timer_not_done(self):
33 self.assertEqual(
34 src.traffic_light.control(
35 red_phase='cross',
36 current_parallel=YELLOW,
37 current_cross=RED,
38 timer_done=False,
39 ),
40 (YELLOW, RED)
41 )
42
43 def test_parallel_yellow_cross_red_timer_done(self):
44 self.assertEqual(
45 src.traffic_light.control(
46 red_phase='cross',
47 current_parallel=YELLOW,
48 current_cross=RED,
49 timer_done=True,
50 ),
51 (RED, RED)
52 )
54 def test_parallel_red_cross_red_timer_not_done(self):
55 self.assertEqual(
56 src.traffic_light.control(
57 red_phase='cross',
58 current_parallel=RED,
59 current_cross=RED,
60 timer_done=False,
61 ),
62 (RED, RED)
63 )
64
65 def test_parallel_red_cross_red_timer_done(self):
66 self.assertEqual(
67 src.traffic_light.control(
68 red_phase='cross',
69 current_parallel=RED,
70 current_cross=RED,
71 timer_done=True,
72 ),
73 (RED, GREEN)
74 )
76 def test_cross_green_parallel_red_timer_not_done(self):
77 self.assertEqual(
78 src.traffic_light.control(
79 red_phase='parallel',
80 current_parallel=RED,
81 current_cross=GREEN,
82 timer_done=False,
83 ),
84 (RED, GREEN)
85 )
86
87 def test_cross_green_parallel_red_timer_done(self):
88 self.assertEqual(
89 src.traffic_light.control(
90 red_phase='parallel',
91 current_parallel=RED,
92 current_cross=GREEN,
93 timer_done=True,
94 ),
95 (RED, YELLOW)
96 )
98 def test_cross_yellow_parallel_red_timer_not_done(self):
99 self.assertEqual(
100 src.traffic_light.control(
101 red_phase='parallel',
102 current_parallel=RED,
103 current_cross=YELLOW,
104 timer_done=False,
105 ),
106 (RED, YELLOW)
107 )
108
109 def test_cross_yellow_parallel_red_timer_done(self):
110 self.assertEqual(
111 src.traffic_light.control(
112 red_phase='parallel',
113 current_parallel=RED,
114 current_cross=YELLOW,
115 timer_done=True,
116 ),
117 (RED, RED)
118 )
120 def test_cross_red_parallel_red_timer_not_done(self):
121 self.assertEqual(
122 src.traffic_light.control(
123 red_phase='parallel',
124 current_parallel=RED,
125 current_cross=RED,
126 timer_done=False,
127 ),
128 (RED, RED)
129 )
130
131 def test_cross_red_parallel_red_timer_done(self):
132 self.assertEqual(
133 src.traffic_light.control(
134 red_phase='parallel',
135 current_parallel=RED,
136 current_cross=RED,
137 timer_done=True,
138 ),
139 (GREEN, RED)
140 )
142 def test_traffic_light_failsafe(self):
143 self.assertEqual(
144 src.traffic_light.control(
145 red_phase='cross',
146 current_parallel='BAP',
147 current_cross=RED,
148 timer_done=False,
149 ),
150 (RED, RED)
151 )
152 self.assertEqual(
153 src.traffic_light.control(
154 red_phase='parallel',
155 current_parallel=RED,
156 current_cross='POW',
157 timer_done=False,
158 ),
159 (RED, RED)
160 )
161 self.assertEqual(
162 src.traffic_light.control(
163 red_phase='BOOM',
164 current_parallel=GREEN,
165 current_cross=GREEN,
166 timer_done=False,
167 ),
168 (RED, RED)
169 )
170 self.assertEqual(
171 src.traffic_light.control(
172 red_phase='BOOM',
173 current_parallel=GREEN,
174 current_cross=YELLOW,
175 timer_done=False,
176 ),
177 (RED, RED)
178 )
179 self.assertEqual(
180 src.traffic_light.control(
181 red_phase='BOOM',
182 current_parallel=YELLOW,
183 current_cross=GREEN,
184 timer_done=False,
185 ),
186 (RED, RED)
187 )
188 self.assertEqual(
189 src.traffic_light.control(
190 red_phase='BOOM',
191 current_parallel=YELLOW,
192 current_cross=YELLOW,
193 timer_done=False,
194 ),
195 (RED, RED)
196 )
197
198
199# Exceptions seen
200# AssertionError
201# NameError
202# AttributeError
203# TypeError
start the project
I open a terminal
I open
makePythonTdd.shI change the name of the project to
traffic_lightinmakePythonTdd.shI run
makePythonTdd.shin the terminal to make thetraffic_lightproject./makePythonTdd.sh
I open
makePythonTdd.ps1I change the name of the project to
traffic_lightinmakePythonTdd.ps1I run
makePythonTdd.ps1in the terminal to make thetraffic_lightproject.\makePythonTdd.ps1
the terminal is my friend, and shows AssertionError
======================== FAILURES ========================= ___________ TestTrafficLight.test_failure _________________ self = <tests.test_traffic_light.TestTrafficLight testMethod=test_failure> def test_failure(self): > self.assertFalse(True) E AssertionError: True is not false tests/test_traffic_light.py:7: AssertionError ================ short test summary info ================== FAILED tests/test_traffic_light.py::TestTrafficLight::test_failure - AssertionError: True is not false ==================== 1 failed in X.YZs ====================I open
test_traffic_light.pyfrom thetestsfolderI change assertFalse to assertTrue in
tests/test_traffic_light.pythe test passes.
I open a new terminal then change directory to
traffic_lightI add a git commit message
I want the Traffic Light to change color based on a timer. If the inputs are
what color is the light now?
is the timer done?
then I get this truth table
current light |
timer |
output |
|---|---|---|
GREEN |
NOT done |
GREEN |
GREEN |
done |
YELLOW |
YELLOW |
NOT done |
YELLOW |
YELLOW |
done |
RED |
RED |
NOT done |
RED |
RED |
done |
GREEN |
test_green_light_timer_not_done
RED: make it fail
I go back to the terminal where the tests are running
I change test_failure to test_green_light_timer_not_done, then add an assertion for if the light is GREEN AND the timer is NOT done
current light
timer
output
GREEN
NOT done
GREEN
the terminal is my friend, and shows NameError
NameError: name 'src' is not definedbecause I do not have a definition for
srcin this file
GREEN: make it pass
I add NameError to the list of Exceptions seen
I add an import statement at the top of the file
1import src.traffic_light 2import unittest 3 4 5class TestTrafficLight(unittest.TestCase):the terminal is my friend, and shows AttributeError
AttributeError: module 'src.traffic_light' has no attribute 'control'because the
traffic_lightmodule does not have anything namedcontrolin itI add AttributeError to the list of Exceptions seen
I open
__init__.pyfromtraffic_lightfolder in thesrcfolderI delete all the text in the file then add a function to
src/traffic_light/__init__.py1def control(): 2 return Nonethe terminal is my friend, and shows TypeError
TypeError: control() got an unexpected keyword argument 'timer_done'I add TypeError to the list of Exceptions seen, in
tests/test_traffic_light.pyI add
timer_doneto the function insrc/traffic_light/__init__.py1def control(timer_done): 2 return Nonethe terminal is my friend, and shows TypeError
TypeError: control() got an unexpected keyword argument 'current_light'I add
current_lightto the function signature1def control(timer_done, current_light): 2 return Nonethe terminal is my friend, and shows AssertionError
AssertionError: None != 'GREEN'I change the return statement to give the test what it expects
1def control(timer_done, current_light): 2 return 'GREEN'the test passes.
control(current_light='GREEN' , timer_done=False) -> 'GREEN'
test_green_light_timer_done
RED: make it fail
I go back to the terminal where the tests are running
I add a test with an assertion for if the light is GREEN AND the timer is done, to
tests/test_traffic_light.pycurrent light
timer
output
GREEN
done
YELLOW
the terminal is my friend, and shows AssertionError
AssertionError: 'GREEN' != 'YELLOW'
GREEN: make it pass
I add an if statement for this case, to
src/traffic_light/__init__.py1def control(timer_done, current_light): 2 if timer_done: 3 return 'YELLOW' 4 5 return 'GREEN'the test passes.
control(current_light='GREEN' , timer_done=False) -> 'GREEN' control(current_light='GREEN' , timer_done=True ) -> 'YELLOW'
REFACTOR: make it better
I add a global variable for
'GREEN'totests/test_traffic_light.py1import src.traffic_light 2import unittest 3 4 5GREEN = 'GREEN' 6 7 8class TestTrafficLight(unittest.TestCase): 9 10 def test_green_light_timer_not_done(self):I use the new variable for
'GREEN'in test_green_light_timer_not_donethe test is still green.
I remove the commented lines from test_green_light_timer_not_done
I use the new variable for
'GREEN'in test_green_light_timer_donestill green.
I remove the commented lines from test_green_light_timer_done
test_yellow_light_timer_not_done
RED: make it fail
I go back to the terminal where the tests are running
I add a test with an assertion for if the Traffic Light is YELLOW AND the timer is NOT done, to
tests/test_traffic_light.pycurrent light
timer
output
YELLOW
NOT done
YELLOW
the terminal is my friend, and shows AssertionError
AssertionError: 'GREEN' != 'YELLOW'
GREEN: make it pass
I add an if statement for if the current light is YELLOW to
src/traffic_light/__init__.py1def control(timer_done, current_light): 2 if timer_done: 3 return 'YELLOW' 4 5 if current_light == 'YELLOW': 6 return current_light 7 8 return 'GREEN'the test passes.
control(current_light='GREEN' , timer_done=False) -> 'GREEN' control(current_light='GREEN' , timer_done=True ) -> 'YELLOW' control(current_light='YELLOW', timer_done=False) -> 'YELLOW'
REFACTOR: make it better
I add a global variable for
'YELLOW'totests/test_traffic_light.py1import src.traffic_light 2import unittest 3 4 5GREEN, YELLOW = 'GREEN', 'YELLOW' 6 7 8class TestTrafficLight(unittest.TestCase):I use the new variable for
'YELLOW'in test_green_light_timer_donethe test is still green.
I remove the commented line from test_green_light_timer_done
I use the new variable for
'YELLOW'in test_yellow_light_timer_not_donestill green.
I remove the commented lines from test_yellow_light_timer_not_done
test_yellow_light_timer_done
RED: make it fail
I go back to the terminal where the tests are running
I add a test with an assertion for if the light is YELLOW AND the timer is done, to
tests/test_traffic_light.pycurrent light
timer
output
YELLOW
done
RED
the terminal is my friend, and shows AssertionError
AssertionError: 'YELLOW' != 'RED'
GREEN: make it pass
I add an if statement for if the timer is done AND the light is YELLOW, in
src/traffic_light/__init__.py1def control(timer_done, current_light): 2 if timer_done: 3 if current_light == 'YELLOW': 4 return 'RED' 5 return 'YELLOW' 6 7 if current_light == 'YELLOW': 8 return current_light 9 10 return 'GREEN'the test passes.
control(current_light='GREEN' , timer_done=False) -> 'GREEN' control(current_light='GREEN' , timer_done=True ) -> 'YELLOW' control(current_light='YELLOW', timer_done=False) -> 'YELLOW' control(current_light='YELLOW', timer_done=True ) -> 'RED'
test_red_light_timer_not_done
RED: make it fail
I go back to the terminal where the tests are running
I add a test with an assertion for if the Traffic Light is RED AND the timer is NOT done, to
tests/test_traffic_light.pycurrent light
timer
output
RED
NOT done
RED
the terminal is my friend, and shows AssertionError
AssertionError: 'GREEN' != 'RED'
GREEN: make it pass
I add an if statement to src/traffic_light/__init__.py
7 if current_light == 'YELLOW':
8 return current_light
9 if current_light == 'RED':
10 return current_light
11
12 return 'GREEN'
the test passes.
control(current_light='GREEN' , timer_done=False) -> 'GREEN'
control(current_light='GREEN' , timer_done=True ) -> 'YELLOW'
control(current_light='YELLOW', timer_done=False) -> 'YELLOW'
control(current_light='YELLOW', timer_done=True ) -> 'RED'
control(current_light='RED' , timer_done=False) -> 'RED'
REFACTOR: make it better
I add a global variable for
'RED'totests/test_traffic_light.py1import src.traffic_light 2import unittest 3 4 5GREEN, YELLOW, RED = 'GREEN', 'YELLOW', 'RED' 6 7 8class TestTrafficLight(unittest.TestCase):I use the variable for
'RED'in test_yellow_light_timer_donethe test is still green.
I remove the commented line from test_yellow_light_timer_done
I use the variable for
'RED'in test_red_light_timer_not_donestill green.
I remove the commented line from test_red_light_timer_not_done
test_red_light_timer_done
RED: make it fail
I go back to the terminal where the tests are running
I add a test with an assertion for if the light is RED AND the timer is done, to
tests/test_traffic_light.pycurrent light
timer
output
RED
done
GREEN
the terminal is my friend, and shows AssertionError
AssertionError: 'YELLOW' != 'GREEN'I add an if statement for if the timer is done AND the current light is RED in
src/traffic_light/__init__.py1def control(timer_done, current_light): 2 if timer_done: 3 if current_light == 'YELLOW': 4 return 'RED' 5 if current_light == 'RED': 6 return 'GREEN' 7 return 'YELLOW' 8 9 if current_light == 'YELLOW': 10 return current_light 11 if current_light == 'RED': 12 return current_light 13 14 return 'GREEN'the test passes.
control(current_light='GREEN' , timer_done=False) -> 'GREEN' control(current_light='GREEN' , timer_done=True ) -> 'YELLOW' control(current_light='YELLOW', timer_done=False) -> 'YELLOW' control(current_light='YELLOW', timer_done=True ) -> 'RED' control(current_light='RED' , timer_done=False) -> 'RED' control(current_light='RED' , timer_done=True ) -> 'GREEN'I add a git commit message in the other terminal
git commit -am 'add test_red_light_timer_done'
refactor if statements
When the control function is called it checks if the timer is done
If the timer is done, it checks the color of the current light
If the current light is YELLOW, it returns RED
If the current light is RED, it returns GREEN
If none of the above conditions are met, it returns YELLOW
If none of the above conditions are met, it checks the color of the current light
If the current light is YELLOW, it returns YELLOW
If the current light is RED, it returns RED
If none of the above conditions are met, it returns GREEN
I go back to the terminal where the tests are running
I add an if statement for if the timer is done AND the light is GREEN, to make it clearer
1def control(timer_done, current_light): 2 if timer_done: 3 if current_light == 'GREEN': 4 return 'YELLOW' 5 if current_light == 'YELLOW': 6 return 'RED' 7 if current_light == 'RED': 8 return 'GREEN' 9 # return 'YELLOW'the tests are still green.
I add an if statement for if the timer is NOT done to be clearer
1def control(timer_done, current_light): 2 if timer_done: 3 if current_light == 'GREEN': 4 return 'YELLOW' 5 if current_light == 'YELLOW': 6 return 'RED' 7 if current_light == 'RED': 8 return 'GREEN' 9 # return 'YELLOW' 10 if not timer_done: 11 if current_light == 'YELLOW': 12 return current_light 13 if current_light == 'RED': 14 return current_light 15 16 return 'GREEN'still green.
I add an if statement for if the timer is NOT done and the light is GREEN, to make it clearer
The
controlfunction returns the current light in every case where the timer is NOT doneI add a return statement to return the current light if the timer is NOT done
green.
I remove the commented lines and the other if statements from the
if not timer_done:block because they are no longer used1def control(timer_done, current_light): 2 if timer_done: 3 if current_light == 'GREEN': 4 return 'YELLOW' 5 if current_light == 'YELLOW': 6 return 'RED' 7 if current_light == 'RED': 8 return 'GREEN' 9 if not timer_done: 10 return current_lightstill green.
I move the if statement for if the timer is NOT done to the top to make the function return a value quicker before it checks the lights
1def control(timer_done, current_light): 2 if not timer_done: 3 return current_light 4 if timer_done: 5 if current_light == 'GREEN': 6 return 'YELLOW' 7 if current_light == 'YELLOW': 8 return 'RED' 9 if current_light == 'RED': 10 return 'GREEN'I add variables for
'RED','YELLOW'and'GREEN'1def control(timer_done, current_light): 2 red, yellow, green = 'RED', 'YELLOW', 'GREEN' 3 if not timer_done:I use the new variables for
'RED','YELLOW'and'GREEN'1def control(timer_done, current_light): 2 red, yellow, green = 'RED', 'YELLOW', 'GREEN' 3 if not timer_done: 4 return current_light 5 if timer_done: 6 # if current_light == 'GREEN': 7 if current_light == green: 8 # return 'YELLOW' 9 return yellow 10 # if current_light == 'YELLOW': 11 if current_light == yellow: 12 # return 'RED' 13 return red 14 # if current_light == 'RED': 15 if current_light == red: 16 # return 'GREEN' 17 return greenstill green.
I remove the commented lines from the
controlfunction1def control(timer_done, current_light): 2 red, yellow, green = 'RED', 'YELLOW', 'GREEN' 3 if not timer_done: 4 return current_light 5 if timer_done: 6 if current_light == green: 7 return yellow 8 if current_light == yellow: 9 return red 10 if current_light == red: 11 return green
When the control function is called it checks if the timer is NOT done
If the timer is NOT done it returns the value of
current_lightsrc.traffic_light.control( current_light='GREEN', timer_done=False ) -> 'GREEN' └── src/traffic_light/__init__.py └── def control(timer_done, current_light): ├── red, yellow, green = 'RED', 'YELLOW', 'GREEN' └── if not timer_done: └── return current_light return 'GREEN' if timer_done: if current_light == green: return yellow if current_light == yellow: return red if current_light == red: return greensrc.traffic_light.control( current_light='YELLOW', timer_done=False ) -> 'YELLOW' └── src/traffic_light/__init__.py └── def control(timer_done, current_light): ├── red, yellow, green = 'RED', 'YELLOW', 'GREEN' └── if not timer_done: └── return current_light return 'YELLOW' if timer_done: if current_light == green: return yellow if current_light == yellow: return red if current_light == red: return greensrc.traffic_light.control( current_light='RED', timer_done=False ) -> 'RED' └── src/traffic_light/__init__.py └── def control(timer_done, current_light): ├── red, yellow, green = 'RED', 'YELLOW', 'GREEN' └── if not timer_done: └── return current_light return 'RED' if timer_done: if current_light == green: return yellow if current_light == yellow: return red if current_light == red: return greenIf the timer is done it checks the value of
current_lightIf the current light is GREEN it returns YELLOW
src.traffic_light.control( current_light='GREEN', timer_done=True ) -> 'YELLOW' └── src/traffic_light/__init__.py └── def control(timer_done, current_light): ├── red, yellow, green = 'RED', 'YELLOW', 'GREEN' ├── if not timer_done: │ return current_light └── if timer_done: └── if current_light == green: └── return yellow if current_light == yellow: return red if current_light == red: return greenIf the current light is YELLOW it returns RED
src.traffic_light.control( current_light='YELLOW', timer_done=True ) -> 'RED' └── src/traffic_light/__init__.py └── def control(timer_done, current_light): ├── red, yellow, green = 'RED', 'YELLOW', 'GREEN' ├── if not timer_done: │ return current_light └── if timer_done: ├── if current_light == green: │ return yellow └── if current_light == yellow: └── return red if current_light == red: return greenIf the current light is RED it returns GREEN
src.traffic_light.control( current_light='RED', timer_done=True ) -> 'GREEN' └── src/traffic_light/__init__.py └── def control(timer_done, current_light): ├── red, yellow, green = 'RED', 'YELLOW', 'GREEN' ├── if not timer_done: │ return current_light └── if timer_done: ├── if current_light == green: │ return yellow ├── if current_light == yellow: │ return red └── if current_light == red: └── return green
If none of the above conditions are met it returns None because ….
src/traffic_light/__init__.py └── def control(timer_done, current_light): ├── red, yellow, green = 'RED', 'YELLOW', 'GREEN' ├── if not timer_done: │ return current_light └── if timer_done: ├── if current_light == green: │ return yellow ├── if current_light == yellow: │ return red ├── if current_light == red: │ return green
The truth table for the Traffic Light is
current light |
timer |
output |
|---|---|---|
GREEN |
NOT done |
GREEN |
GREEN |
done |
YELLOW |
YELLOW |
NOT done |
YELLOW |
YELLOW |
done |
RED |
RED |
NOT done |
RED |
RED |
done |
GREEN |
This only shows one set of lights for traffic in one direction, which is not needed if the street has no cross traffic, it is not yet a real Traffic Light.
I want the control function to show what happens in front of me (parallel) and what happens for the traffic crossing the street if the light is RED for me, it has to consider traffic in both directions
what traffic phase is this (cross, parallel or both)?
what is the light for me (parallel)?
what is the light for them (cross)?
is the timer done?
The Traffic Light has to make sure that there is never a case where cars move through the intersection at the same time (to try and prevent accidents). The following cases must never happen
parallel |
cross |
|---|---|
GREEN |
GREEN |
GREEN |
YELLOW |
YELLOW |
YELLOW |
YELLOW |
GREEN |
The outputs will be the lights for Parallel and Cross Traffic which gives me this truth table
current |
current |
next |
next |
|
|---|---|---|---|---|
parallel |
cross |
timer |
parallel |
cross |
GREEN |
RED |
NOT done |
GREEN |
RED |
GREEN |
RED |
done |
YELLOW |
RED |
YELLOW |
RED |
NOT done |
YELLOW |
RED |
YELLOW |
RED |
done |
safety RED |
safety RED |
RED |
RED |
NOT done |
safety RED |
safety RED |
RED |
RED |
done |
RED |
GREEN |
current |
current |
next |
next |
|
|---|---|---|---|---|
parallel |
cross |
timer |
parallel |
cross |
RED |
GREEN |
NOT done |
RED |
GREEN |
RED |
GREEN |
done |
RED |
YELLOW |
RED |
YELLOW |
NOT done |
RED |
YELLOW |
RED |
YELLOW |
done |
safety RED |
safety RED |
RED |
RED |
NOT done |
safety RED |
safety RED |
RED |
RED |
done |
GREEN |
RED |
Where 'parallel' is the light in front of me, and 'cross' is the light for traffic crossing the street. The all RED row (Safety State) makes sure that there are no cars moving through the intersection at the same time.
test_cross_red_parallel_red_timer_done
RED: make it fail
I go back to the terminal where the tests are running
I add a test with an assertion for if the current parallel light is RED AND the current cross light is RED AND the timer is done
current
current
next
next
parallel
cross
timer
parallel
cross
RED
RED
done
GREEN
RED
the terminal is my friend, and shows TypeError
TypeError: control() got an unexpected keyword argument 'current_parallel'
GREEN: make it pass
I add
current_parallelto the function definition insrc/traffic_light/__init__.py1def control(timer_done, current_light, current_parallel):the terminal is my friend, and shows TypeError
FAILED ...test_cross_red_parallel_red_timer_done - TypeError: control() got an unexpected keyword argument 'current_cross' FAILED ...test_green_light_timer_done - TypeError: control() missing 1 required positional argument: 'current_parallel' FAILED ...test_green_light_timer_not_done - TypeError: control() missing 1 required positional argument: 'current_parallel' FAILED ...test_red_light_timer_done - TypeError: control() missing 1 required positional argument: 'current_parallel' FAILED ...test_red_light_timer_not_done - TypeError: control() missing 1 required positional argument: 'current_parallel' FAILED ...test_yellow_light_timer_done - TypeError: control() missing 1 required positional argument: 'current_parallel' FAILED ...test_yellow_light_timer_not_done - TypeError: control() missing 1 required positional argument: 'current_parallel'I add a default value for the
current_parallelparameter1def control( 2 timer_done, current_light, 3 current_parallel='RED', 4 ):the terminal is my friend, and shows TypeError
TypeError: control() got an unexpected keyword argument 'current_cross'I add
current_crossto the function definition1def control( 2 timer_done, current_light, 3 current_parallel='RED', current_cross='RED', 4 ):the terminal is my friend, and shows TypeError
TypeError: control() missing 1 required positional argument: 'current_light'I add a default value for
current_lightto the function signature1def control( 2 timer_done, current_light='RED', 3 current_parallel='RED', current_cross='RED', 4 ):the terminal is my friend, and shows AssertionError
AssertionError: 'GREEN' != ('GREEN', 'RED')I add an if statement to
if timer_done:for if the current parallel light is RED AND the current cross light is REDthe terminal still shows AssertionError because the function returns RED if the timer is done AND
current_lightis RED and the default value forcurrent_lightin this assertion is RED.I change the default value for
current_lightto None1def control( 2 timer_done, current_light=None, 3 current_parallel='RED', current_cross='RED', 4 ):the test passes.
control( current_parallel='RED' , current_cross='RED', timer_done=True ) -> 'GREEN', 'RED'
test_cross_red_parallel_red_timer_not_done
RED: make it fail
I go back to the terminal where the tests are running
I add a test with an assertion for if the current parallel light is RED AND the current cross light is RED AND the timer is NOT done
current
current
next
next
parallel
cross
timer
parallel
cross
RED
RED
NOT done
safety RED
safety RED
the terminal is my friend, and shows AssertionError
AssertionError: None != ('RED', 'RED')
GREEN: make it pass
I add an if statement for if the current parallel light is RED AND the current cross light is RED to
if not timer_done:insrc/traffic_light/__init__.pythe terminal is my friend, and shows AssertionError
FAILED ...test_green_light_timer_not_done - AssertionError: ('RED', 'RED') != 'GREEN' FAILED ...test_red_light_timer_not_done - AssertionError: ('RED', 'RED') != 'RED' FAILED ...test_yellow_light_timer_not_done - AssertionError: ('RED', 'RED') != 'YELLOW'because those assertions use the default values for
current_parallelandcurrent_crossI add an if statement for if the timer is NOT done and the
current_lightis grouped as Truethe test passes.
control( current_parallel='RED' , current_cross='RED', timer_done=False ) -> 'RED', 'RED' control( current_parallel='RED' , current_cross='RED', timer_done=True ) -> 'GREEN', 'RED'I remove the commented line
test_cross_yellow_parallel_red_timer_done
RED: make it fail
I go back to the terminal where the tests are running
I add a test with an assertion for if the current parallel light is RED AND the current cross light is YELLOW AND the timer is done
current
current
next
next
parallel
cross
timer
parallel
cross
RED
YELLOW
done
RED
RED
the terminal is my friend, and shows AssertionError
AssertionError: None != ('RED', 'RED')
GREEN: make it pass
I add an if statement for if the current parallel light is RED AND the current cross light is YELLOW to if timer_done: in src/traffic_light/__init__.py
12 if timer_done:
13 if current_light == green:
14 return yellow
15 if current_light == yellow:
16 return red
17 if current_light == red:
18 return green
19 if current_parallel == red and current_cross == yellow:
20 return red, red
21 if current_parallel == red and current_cross == red:
22 return green, red
the test passes.
control(
current_parallel='RED' , current_cross='YELLOW',
timer_done=True
) -> 'RED', 'RED'
control(
current_parallel='RED' , current_cross='RED',
timer_done=False
) -> 'RED', 'RED'
control(
current_parallel='RED' , current_cross='RED',
timer_done=True
) -> 'GREEN', 'RED'
REFACTOR: make it better
Two of the if statements in
if timer_done:are for if the current parallel light is RED. I change them to remove repetition ofif current_parallel == redthe tests are still green.
I remove the commented lines
test_cross_yellow_parallel_red_timer_not_done
RED: make it fail
I go back to the terminal where the tests are running
I add a test with an assertion for if the current parallel light is RED AND the current cross light is YELLOW AND the timer is NOT done
current
current
next
next
parallel
cross
timer
parallel
cross
RED
YELLOW
NOT done
RED
YELLOW
the terminal is my friend, and shows AssertionError
AssertionError: None != ('RED', 'YELLOW')
GREEN: make it pass
I add an if statement for if the current parallel light is RED AND the current cross light is YELLOW to if not timer_done: in src/traffic_light/__init__.py
6 if not timer_done:
7 if current_light:
8 return current_light
9 if current_parallel == red and current_cross == yellow:
10 return red, yellow
11 if current_parallel == red and current_cross == red:
12 return red, red
13
14 if timer_done:
the test passes.
control(
current_parallel='RED' , current_cross='YELLOW',
timer_done=False
) -> 'RED', 'YELLOW'
control(
current_parallel='RED' , current_cross='YELLOW',
timer_done=True
) -> 'RED', 'RED'
control(
current_parallel='RED' , current_cross='RED',
timer_done=False
) -> 'RED', 'RED'
control(
current_parallel='RED' , current_cross='RED',
timer_done=True
) -> 'GREEN', 'RED'
REFACTOR: make it better
Two of the if statements in
if not timer_done:are also for if the current parallel light is RED. I change them to remove repetition ofif current_parallel == redthe tests are still green.
I remove the commented lines
test_cross_green_parallel_red_timer_done
RED: make it fail
I go back to the terminal where the tests are running
I add a test with an assertion for if the current parallel light is RED AND the current cross light is GREEN AND the timer is done
current
current
next
next
parallel
cross
timer
parallel
cross
RED
GREEN
done
RED
YELLOW
the terminal is my friend, and shows AssertionError
AssertionError: None != ('RED', 'YELLOW')
GREEN: make it pass
I add an if statement for if the current parallel light is RED AND the current cross light is GREEN to if timer_done: in src/traffic_light/__init__.py
16 if timer_done:
17 if current_light == green:
18 return yellow
19 if current_light == yellow:
20 return red
21 if current_light == red:
22 return green
23
24 if current_parallel == red:
25 if current_cross == green:
26 return red, yellow
27 if current_cross == yellow:
28 return red, red
29 if current_cross == red:
30 return green, red
the test passes.
control(
current_parallel='RED' , current_cross='GREEN',
timer_done=True
) -> 'RED', 'YELLOW'
control(
current_parallel='RED' , current_cross='YELLOW',
timer_done=False
) -> 'RED', 'YELLOW'
control(
current_parallel='RED' , current_cross='YELLOW',
timer_done=True
) -> 'RED', 'RED'
control(
current_parallel='RED' , current_cross='RED',
timer_done=False
) -> 'RED', 'RED'
control(
current_parallel='RED' , current_cross='RED',
timer_done=True
) -> 'GREEN', 'RED'
test_cross_green_parallel_red_timer_not_done
RED: make it fail
I go back to the terminal where the tests are running
I add a test with an assertion for if the current parallel light is RED AND the current cross light is GREEN AND the timer is NOT done
current
current
next
next
parallel
cross
timer
parallel
cross
RED
GREEN
NOT done
RED
GREEN
the terminal is my friend, and shows AssertionError
AssertionError: None != ('RED', 'GREEN')
GREEN: make it pass
I add an if statement for if the current parallel light is RED AND the current cross light is GREEN to if not timer_done: in src/traffic_light/__init__.py
5 red, yellow, green = 'RED', 'YELLOW', 'GREEN'
6 if not timer_done:
7 if current_light:
8 return current_light
9
10 if current_parallel == red:
11 if current_cross == green:
12 return red, green
13 if current_cross == yellow:
14 return red, yellow
15 if current_cross == red:
16 return red, red
17
18 if timer_done:
the test passes.
control(
current_parallel='RED' , current_cross='GREEN',
timer_done=False
) -> 'RED', 'GREEN'
control(
current_parallel='RED' , current_cross='GREEN',
timer_done=True
) -> 'RED', 'YELLOW'
control(
current_parallel='RED' , current_cross='YELLOW',
timer_done=False
) -> 'RED', 'YELLOW'
control(
current_parallel='RED' , current_cross='YELLOW',
timer_done=True
) -> 'RED', 'RED'
control(
current_parallel='RED' , current_cross='RED',
timer_done=False
) -> 'RED', 'RED'
control(
current_parallel='RED' , current_cross='RED',
timer_done=True
) -> 'GREEN', 'RED'
REFACTOR: make it better
The three if statements in the
if not timer_done:block for if the current parallel light is RED all return the current parallel light and the current cross light. I write one return statement for all of themthe tests are still green.
I remove the if statements for the other cases since they are no longer used
still green.
test_parallel_red_cross_red_timer_done
RED: make it fail
I go back to the terminal where the tests are running
I change the assertion in test_red_light_timer_done for if the current parallel light is RED AND the current cross light is RED AND the timer is done
current
current
next
next
parallel
cross
timer
parallel
cross
RED
RED
done
RED
GREEN
the terminal is my friend, and shows AssertionError
AssertionError: Tuples differ: ('GREEN', 'RED') != ('RED', 'GREEN')because the
controlfunction cannot tell the difference betweenif the current parallel light is RED AND the cross light is RED AND the timer is done which returns the next parallel light as GREEN and the next cross light as RED, and
if the current parallel light is RED AND the cross light is RED AND the timer is done which returns the next parallel light as RED and the next cross light as GREEN
the current lights are the same in both cases but the output is different
current
current
next
next
parallel
cross
timer
parallel
cross
RED
RED
done
GREEN
RED
RED
RED
done
RED
GREEN
I need a way for the function to know the difference. When I look at the overall table I see that the phases that come right before these two states are the safety state which is also the same for both
current
current
next
next
parallel
cross
timer
parallel
cross
RED
RED
NOT done
safety RED
safety RED
RED
RED
done
GREEN
RED
RED
RED
NOT done
safety RED
safety RED
RED
RED
done
RED
GREEN
The difference between them is in the phase before the safety phase
current
current
next
next
parallel
cross
timer
parallel
cross
YELLOW
RED
done
RED
RED
RED
RED
NOT done
safety RED
safety RED
RED
RED
done
GREEN
RED
current
current
next
next
parallel
cross
timer
parallel
cross
RED
YELLOW
done
RED
RED
RED
RED
NOT done
safety RED
safety RED
RED
RED
done
RED
GREEN
The
controlfunction needs to know that it is a sequence based on whether the parallel or cross traffic is in a RED stateif it is in the RED state for cross traffic AND the current parallel light is RED AND the cross light is RED AND the timer is done it should keep the parallel light RED and turn the next cross light GREEN
if it is in the RED state for parallel traffic AND the current parallel light is RED AND the cross light is RED AND the timer is done it should turn the next parallel light GREEN and keep the next cross light RED
GREEN: make it pass
I add
red_phaseto the call to thecontrolfunction in the assertion in test_red_light_timer_donethe terminal is my friend, and shows TypeError
TypeError: control() got an unexpected keyword argument 'red_phase'I add
red_phasewith a default value for the other tests, to the function definition insrc/traffic_light/__init__.py1def control( 2 timer_done, current_light=None, 3 current_parallel='RED', current_cross='RED', 4 red_phase='parallel', 5 ):the terminal still shows AssertionError
I add an if statement to the
if timer_done:block for if the Traffic Light is currently in the RED phase for cross traffic AND the current parallel light is RED AND the current cross light is REDthe test passes.
control( current_parallel='RED' , current_cross='RED', timer_done=True, red_phase='cross' ) -> 'RED', 'GREEN'
REFACTOR: make it better
I remove the commented lines from test_red_light_timer_done in
tests/test_traffic_light.pyI change the name of the test from test_red_light_timer_done to test_parallel_red_cross_red_timer_done
test_parallel_red_cross_red_timer_not_done
RED: make it fail
I go back to the terminal where the tests are running
I add values for
current_parallel,current_crossandred_phaseto the assertion in test_red_light_timer_not_done for if'cross'traffic is in the RED phase AND the current parallel light is RED AND the current cross light is RED AND the timer is NOT donered
current
current
next
next
phase
parallel
cross
timer
parallel
cross
‘cross’
RED
RED
NOT done
safety RED
safety RED
the test is still green.
control( current_parallel='RED' , current_cross='RED', timer_done=False, red_phase='cross' ) -> 'RED', 'RED' control( current_parallel='RED' , current_cross='RED', timer_done=True, red_phase='cross' ) -> 'RED', 'GREEN'
GREEN: make it pass
I remove the commented lines from test_red_light_timer_not_done
I change the name of the test from test_red_light_timer_not_done to test_parallel_red_cross_red_timer_not_done
test_parallel_yellow_cross_red_timer_done
RED: make it fail
I go back to the terminal where the tests are running
I add values for
current_parallel,current_crossandred_phaseto the assertion in test_yellow_light_timer_done for if'cross'traffic is in the RED phase AND the current parallel light is YELLOW AND the current cross light is RED AND the timer is donered
current
current
next
next
phase
parallel
cross
timer
parallel
cross
‘cross’
YELLOW
RED
done
RED
RED
the terminal is my friend, and shows AssertionError
AssertionError: None != ('RED', 'RED')
GREEN: make it pass
I add an if statement for if the current parallel light is YELLOW AND the current cross light is RED to if timer_done: in src/traffic_light/__init__.py
14 if timer_done:
15 if current_light == green:
16 return yellow
17 if current_light == yellow:
18 return red
19 if current_light == red:
20 return green
21
22 if red_phase == 'cross':
23 if current_parallel == red and current_cross == red:
24 return red, green
25
26 if current_parallel == yellow and current_cross == red:
27 return red, red
28
29 if current_parallel == red:
the test passes.
control(
current_parallel='YELLOW' , current_cross='RED',
timer_done=True, red_phase='cross'
) -> 'RED', 'RED'
control(
current_parallel='RED' , current_cross='RED',
timer_done=False, red_phase='cross'
) -> 'RED', 'RED'
control(
current_parallel='RED' , current_cross='RED',
timer_done=True, red_phase='cross'
) -> 'RED', 'GREEN'
REFACTOR: make it better
Two of the if statements in the
if timer_done:block are for if the current cross light is RED which is when'cross'traffic is in the RED phase. I change them to remove repetition ofif current_cross == redthe tests are still green.
I remove the commented lines
I remove the commented lines from test_yellow_light_timer_done
I change the name of the test from test_yellow_light_timer_done to test_parallel_yellow_cross_red_timer_done
test_parallel_yellow_cross_red_timer_not_done
RED: make it fail
I go back to the terminal where the tests are running
I add values for
current_parallel,current_crossandred_phaseto the assertion in test_yellow_light_timer_not_done for if'cross'traffic is in the RED phase AND the current parallel light is YELLOW AND the current cross light is RED AND the timer is NOT donered
current
current
next
next
phase
parallel
cross
timer
parallel
cross
‘cross’
YELLOW
RED
NOT done
YELLOW
RED
the terminal is my friend, and shows AssertionError
AssertionError: None != ('YELLOW', 'RED')
GREEN: make it pass
I add an if statement for if the current parallel light is YELLOW AND the current cross light is RED to if not timer_done: in src/traffic_light/__init__.py
6 red, yellow, green = 'RED', 'YELLOW', 'GREEN'
7 if not timer_done:
8 if current_light:
9 return current_light
10
11 if current_parallel == yellow and current_cross == red:
12 return yellow, red
13 if current_parallel == red:
14 return current_parallel, current_cross
15
16 if timer_done:
the test passes.
control(
current_parallel='YELLOW' , current_cross='RED',
timer_done=False, red_phase='cross'
) -> 'YELLOW', 'RED'
control(
current_parallel='YELLOW' , current_cross='RED',
timer_done=True, red_phase='cross'
) -> 'RED', 'RED'
control(
current_parallel='RED' , current_cross='RED',
timer_done=False, red_phase='cross'
) -> 'RED', 'RED'
control(
current_parallel='RED' , current_cross='RED',
timer_done=True, red_phase='cross'
) -> 'RED', 'GREEN'
REFACTOR: make it better
I remove the commented lines from test_yellow_light_timer_not_done in
tests/test_traffic_light.pyI change the name of the test from test_yellow_light_timer_not_done to test_parallel_yellow_cross_red_timer_not_done
test_parallel_green_cross_red_timer_done
RED: make it fail
I go back to the terminal where the tests are running
I add values for
current_parallel,current_crossandred_phaseto the assertion in test_green_light_timer_done for if'cross'traffic is in the RED phase AND the current parallel light is GREEN AND the current cross light is RED AND the timer is donered
current
current
next
next
phase
parallel
cross
timer
parallel
cross
‘cross’
GREEN
RED
done
YELLOW
RED
the terminal is my friend and shows AssertionError
AssertionError: None != ('YELLOW', 'RED')
GREEN: make it pass
I add an if statement to if red_phase == 'cross': in the if timer_done: block for if 'cross' traffic is in the RED phase AND the current parallel light is GREEN AND the current cross light is RED AND the timer is done
16 if timer_done:
17 if current_light == green:
18 return yellow
19 if current_light == yellow:
20 return red
21 if current_light == red:
22 return green
23
24 if red_phase == 'cross':
25 if current_parallel == green:
26 return yellow, red
27 if current_parallel == yellow:
28 return red, red
29 if current_parallel == red:
30 return red, green
31
32 if current_parallel == red:
the test passes.
control(
current_parallel='GREEN' , current_cross='RED',
timer_done=True, red_phase='cross'
) -> 'YELLOW', 'RED'
control(
current_parallel='YELLOW' , current_cross='RED',
timer_done=False, red_phase='cross'
) -> 'YELLOW', 'RED'
control(
current_parallel='YELLOW' , current_cross='RED',
timer_done=True, red_phase='cross'
) -> 'RED', 'RED'
control(
current_parallel='RED' , current_cross='RED',
timer_done=False, red_phase='cross'
) -> 'RED', 'RED'
control(
current_parallel='RED' , current_cross='RED',
timer_done=True, red_phase='cross'
) -> 'RED', 'GREEN'
REFACTOR: make it better
I remove the commented lines from test_green_light_timer_done
I change the name of the test from test_green_light_timer_done to test_parallel_green_cross_red_timer_done
test_parallel_green_cross_red_timer_not_done
RED: make it fail
I go back to the terminal where the tests are running
I add values for
current_parallel,current_crossandred_phaseto the assertion in test_green_light_timer_not_done for if'cross'traffic is in the RED phase AND the current parallel light is GREEN AND the current cross light is RED AND the timer is NOT donered
current
current
next
next
phase
parallel
cross
timer
parallel
cross
‘cross’
GREEN
RED
NOT done
GREEN
RED
the terminal is my friend, and shows AssertionError
AssertionError: None != ('GREEN', 'RED')
GREEN: make it pass
I add an if statement for if the current parallel light is GREEN AND the current cross light is RED to if not timer_done: in src/traffic_light/__init__.py
6 red, yellow, green = 'RED', 'YELLOW', 'GREEN'
7 if not timer_done:
8 if current_light:
9 return current_light
10
11 if current_parallel == green and current_cross == red:
12 return green, red
13 if current_parallel == yellow and current_cross == red:
14 return yellow, red
15 if current_parallel == red:
16 return current_parallel, current_cross
17
18 if timer_done:
the test passes.
control(
current_parallel='GREEN' , current_cross='RED',
timer_done=False, red_phase='cross'
) -> 'GREEN', 'RED'
control(
current_parallel='GREEN' , current_cross='RED',
timer_done=True, red_phase='cross'
) -> 'YELLOW', 'RED'
control(
current_parallel='YELLOW' , current_cross='RED',
timer_done=False, red_phase='cross'
) -> 'YELLOW', 'RED'
control(
current_parallel='YELLOW' , current_cross='RED',
timer_done=True, red_phase='cross'
) -> 'RED', 'RED'
control(
current_parallel='RED' , current_cross='RED',
timer_done=False, red_phase='cross'
) -> 'RED', 'RED'
control(
current_parallel='RED' , current_cross='RED',
timer_done=True, red_phase='cross'
) -> 'RED', 'GREEN'
REFACTOR: make it better
I remove the commented lines from test_green_light_timer_not_done
I change the name of the test from test_green_light_timer_not_done to test_parallel_green_cross_red_timer_not_done
I remove the if statements for the
current_lightparameter since they are no longer used, insrc/traffic_light/__init__.pythe tests are still green.
I remove the
current_lightparameter from the parentheses of the function definition1def control( 2 timer_done, red_phase='parallel', 3 current_parallel='RED', current_cross='RED', 4 ): 5 red, yellow, green = 'RED', 'YELLOW', 'GREEN' 6 if not timer_done:The three if statements in
if not timer_done:all return the current parallel light and the current cross light. I write one return statement for all of themthe tests are still green.
I change
if current_parallel == redtoif red_phase == 'parallel':for if the timer is done AND the current parallel light is RED since it is for when'parallel'traffic is in the RED phasegreen.
I remove the if statements from
if not timer_done:and the commented line since they are no longer used1def control( 2 timer_done, red_phase='parallel', 3 current_parallel='RED', current_cross='RED', 4 ): 5 red, yellow, green = 'RED', 'YELLOW', 'GREEN' 6 7 if not timer_done: 8 return current_parallel, current_cross 9 10 if timer_done: 11 if red_phase == 'cross': 12 if current_parallel == green: 13 return yellow, red 14 if current_parallel == yellow: 15 return red, red 16 if current_parallel == red: 17 return red, green 18 if red_phase == 'parallel': 19 if current_cross == green: 20 return red, yellow 21 if current_cross == yellow: 22 return red, red 23 if current_cross == red: 24 return green, redI add a
red_phase=parallel,to test_cross_green_parallel_red_timer_not_done to make it clearerthe test is still green.
I do the same thing to test_cross_green_parallel_red_timer_done
still green.
then test_cross_yellow_parallel_red_timer_not_done
green.
I add
red_phase='parallel'to test_cross_yellow_parallel_red_timer_donestill green.
I add it to test_cross_red_parallel_red_timer_not_done
the test is still green.
I add it to test_cross_red_parallel_red_timer_done
still green.
When the control function is called it checks if the timer is NOT done
If the timer is NOT done, it returns the values of
current_parallelandcurrent_cross, which means it does not change the parallel or cross lights, it keeps them the samesrc.traffic_light.control( current_parallel='RED', current_cross='GREEN', timer_done=False, red_phase='parallel' ) -> 'RED', 'GREEN' └── src/traffic_light/__init__.py └── def control( timer_done, red_phase='parallel', current_parallel='RED', current_cross='RED', ): ├── red, yellow, green = 'RED', 'YELLOW', 'GREEN' └── if not timer_done: └── return current_parallel, current_cross return 'RED' , 'GREEN' if timer_done:src.traffic_light.control( current_parallel='RED', current_cross='YELLOW', timer_done=False, red_phase='parallel' ) -> 'RED', 'YELLOW' └── src/traffic_light/__init__.py └── def control( timer_done, red_phase='parallel', current_parallel='RED', current_cross='RED', ): ├── red, yellow, green = 'RED', 'YELLOW', 'GREEN' └── if not timer_done: └── return current_parallel, current_cross return 'RED' , 'YELLOW' if timer_done:src.traffic_light.control( current_parallel='RED', current_cross='RED', timer_done=False, red_phase='parallel' ) -> 'RED', 'RED' └── src/traffic_light/__init__.py └── def control( timer_done, red_phase='parallel', current_parallel='RED', current_cross='RED', ): ├── red, yellow, green = 'RED', 'YELLOW', 'GREEN' └── if not timer_done: └── return current_parallel, current_cross return 'RED' , 'RED' if timer_done:src.traffic_light.control( current_parallel='GREEN', current_cross='RED', timer_done=False, red_phase='cross' ) -> 'GREEN', 'RED' └── src/traffic_light/__init__.py └── def control( timer_done, red_phase='parallel', current_parallel='RED', current_cross='RED', ): ├── red, yellow, green = 'RED', 'YELLOW', 'GREEN' └── if not timer_done: └── return current_parallel, current_cross return 'GREEN' , 'RED' if timer_done:src.traffic_light.control( current_parallel='YELLOW', current_cross='RED', timer_done=False, red_phase='cross' ) -> 'YELLOW', 'RED' └── src/traffic_light/__init__.py └── def control( timer_done, red_phase='parallel', current_parallel='RED', current_cross='RED', ): ├── red, yellow, green = 'RED', 'YELLOW', 'GREEN' └── if not timer_done: └── return current_parallel, current_cross return 'YELLOW' , 'RED' if timer_done:src.traffic_light.control( current_parallel='RED', current_cross='RED', timer_done=False, red_phase='cross' ) -> 'RED', 'RED' └── src/traffic_light/__init__.py └── def control( timer_done, red_phase='parallel', current_parallel='RED', current_cross='RED', ): ├── red, yellow, green = 'RED', 'YELLOW', 'GREEN' └── if not timer_done: └── return current_parallel, current_cross return 'RED' , 'RED' if timer_done:If the timer is done, it checks if
'cross'traffic is in the RED phaseIf
'cross'traffic is in the RED phase, it checks the value ofcurrent_parallelIf the current parallel light is GREEN, it returns
YELLOW, RED, which means the next parallel light will be YELLOW and the cross light will remain RED because it is still in the RED phasesrc.traffic_light.control( current_parallel='GREEN', current_cross='RED', timer_done=True, red_phase='cross' ) -> 'YELLOW', 'RED' └── src/traffic_light/__init__.py └── def control( timer_done, red_phase='parallel', current_parallel='RED', current_cross='RED', ): ├── red, yellow, green = 'RED', 'YELLOW', 'GREEN' ├── if not timer_done: │ return current_parallel, current_cross └── if timer_done: └── if red_phase == 'cross': └── if current_parallel == green: └── return yellow, red if current_parallel == yellow: return red, red if current_parallel == red: return red, green if red_phase == 'parallel':If the current parallel light is YELLOW, it returns
RED, RED, which means there will be no traffic in the intersection, the parallel and cross lights will both be REDsrc.traffic_light.control( current_parallel='YELLOW', current_cross='RED', timer_done=True, red_phase='cross' ) -> 'RED', 'RED' └── src/traffic_light/__init__.py └── def control( timer_done, red_phase='parallel', current_parallel='RED', current_cross='RED', ): ├── red, yellow, green = 'RED', 'YELLOW', 'GREEN' ├── if not timer_done: │ return current_parallel, current_cross └── if timer_done: └── if red_phase == 'cross': ├── if current_parallel == green: │ return yellow, red └── if current_parallel == yellow: └── return red, red if current_parallel == red: return red, green if red_phase == 'parallel':If the current parallel light is RED, it returns
RED, GREEN, which means the parallel light will stay RED since it is now in the RED phase and the next cross light will be GREENsrc.traffic_light.control( current_parallel='RED', current_cross='RED', timer_done=True, red_phase='cross' ) -> 'RED', 'GREEN' └── src/traffic_light/__init__.py └── def control( timer_done, red_phase='parallel', current_parallel='RED', current_cross='RED', ): ├── red, yellow, green = 'RED', 'YELLOW', 'GREEN' ├── if not timer_done: │ return current_parallel, current_cross └── if timer_done: └── if red_phase == 'cross': ├── if current_parallel == green: │ return yellow, red ├── if current_parallel == yellow: │ return red, red └── if current_parallel == red: └── return red, green if red_phase == 'parallel':If
'cross'traffic is not in the RED phase, it checks if'parallel'traffic is in the RED phase
If
'parallel'traffic is in the RED phase, it checks the value ofcurrent_crossIf the current cross light is GREEN, it returns
RED, YELLOW, which means the parallel light will remain RED because it is still in the RED phase and the next cross light will be YELLOWsrc.traffic_light.control( current_parallel='RED', current_cross='GREEN', timer_done=True, red_phase='parallel' ) -> 'RED', 'YELLOW' └── src/traffic_light/__init__.py └── def control( timer_done, red_phase='parallel', current_parallel='RED', current_cross='RED', ): ├── red, yellow, green = 'RED', 'YELLOW', 'GREEN' ├── if not timer_done: │ return current_parallel, current_cross └── if timer_done: ├── if red_phase == 'cross': │ ... └── if red_phase == 'parallel': └── if current_cross == green: └── return red, yellow if current_cross == yellow: return red, red if current_cross == red: return green, redIf the current parallel light is YELLOW, it returns
RED, RED, which means there will be no traffic in the intersection, the parallel and cross lights will both be REDsrc.traffic_light.control( current_parallel='RED', current_cross='YELLOW', timer_done=True, red_phase='parallel' ) -> 'RED', 'RED' └── src/traffic_light/__init__.py └── def control( timer_done, red_phase='parallel', current_parallel='RED', current_cross='RED', ): ├── red, yellow, green = 'RED', 'YELLOW', 'GREEN' ├── if not timer_done: │ return current_parallel, current_cross └── if timer_done: ├── if red_phase == 'cross': │ ... └── if red_phase == 'parallel': ├── if current_cross == green: │ return red, yellow └── if current_cross == yellow: └── return red, red if current_cross == red: return green, redIf the current cross light is RED, it returns
GREEN, RED, which means the next parallel light will be GREEN and the cross light will stay RED since it is now in the RED phasesrc.traffic_light.control( current_parallel='RED', current_cross='RED', timer_done=True, red_phase='parallel' ) -> 'GREEN', 'RED' └── src/traffic_light/__init__.py └── def control( timer_done, red_phase='parallel', current_parallel='RED', current_cross='RED', ): ├── red, yellow, green = 'RED', 'YELLOW', 'GREEN' ├── if not timer_done: │ return current_parallel, current_cross └── if timer_done: ├── if red_phase == 'cross': │ ... └── if red_phase == 'parallel': ├── if current_cross == green: │ return red, yellow ├── if current_cross == yellow: │ return red, red └── if current_cross == red: └── return green, red
If none of the above conditions are met it returns None
src/traffic_light/__init__.py └── def control( timer_done, red_phase='parallel', current_parallel='RED', current_cross='RED', ): ├── red, yellow, green = 'RED', 'YELLOW', 'GREEN' ├── if not timer_done: │ return current_parallel, current_cross └── if timer_done: ├── if red_phase == 'cross': │ ... ├── if red_phase == 'parallel': │ ...
test_traffic_light_failsafe
The Traffic Light has to make sure that there is never a case where cars move through the intersection at the same time. The following cases must never happen
parallel |
cross |
|---|---|
GREEN |
GREEN |
GREEN |
YELLOW |
YELLOW |
YELLOW |
YELLOW |
GREEN |
It should be RED for both 'cross' and 'parallel' traffic for any of the above cases and for any cases that are outside the safe cases, for example a power failure.
RED: make it fail
I add a test for the safety state with an assertion for if
current_parallelis NOT GREEN or YELLOW or RED,tests/test_traffic_light.pythe terminal is my friend, and shows AssertionError
AssertionError: Tuples differ: ('BAP', 'RED') != ('RED', 'RED')
GREEN: make it pass
I add an if statement for if current_parallel is NOT GREEN or YELLOW or RED, in src/traffic_light/__init__.py
5 red, yellow, green = 'RED', 'YELLOW', 'GREEN'
6
7 if not (
8 current_parallel == green
9 or current_parallel == yellow
10 or current_parallel == red
11 ):
12 return red, red
13
14 if not timer_done:
the test passes.
control(
current_parallel='BAP', current_cross='RED',
timer_done=False, red_phase='cross'
) -> 'RED', 'RED'
REFACTOR: make it better
I add an assertion for if
current_crossis NOT GREEN or YELLOW or RED to test_traffic_light_failsafe intests/test_traffic_light.pythe terminal is my friend, and shows AssertionError
AssertionError: Tuples differ: ('RED', 'RED') != ('RED', 'POW')I add an if statement for if
current_crossis NOT GREEN or YELLOW or RED to test_traffic_light_failsafe insrc/traffic_light/__init__.pythe test passes.
control( current_parallel=RED, current_cross='POW', timer_done=False, red_phase='parallel' ) -> 'RED', 'RED' control( current_parallel='BAP', current_cross='RED', timer_done=False, red_phase='cross' ) -> 'RED', 'RED'I add an assertion to test_traffic_light_failsafe for if both lights are GREEN, in
tests/test_traffic_light.pythe terminal is my friend, and shows AssertionError
AssertionError: Tuples differ: ('GREEN', 'GREEN') != ('RED', 'RED')I add an if statement for if both lights are GREEN, in
src/traffic_light/__init__.pythe test passes.
control( current_parallel='GREEN', current_cross='GREEN', timer_done=False, red_phase='BOOM' ) -> 'RED', 'RED' control( current_parallel=RED, current_cross='POW', timer_done=False, red_phase='parallel' ) -> 'RED', 'RED' control( current_parallel='BAP', current_cross='RED', timer_done=False, red_phase='cross' ) -> 'RED', 'RED'I add an assertion to test_traffic_light_failsafe for if the parallel lights are GREEN AND the cross lights are YELLOW, in
tests/test_traffic_light.pythe terminal is my friend, and shows AssertionError
AssertionError: Tuples differ: ('GREEN', 'YELLOW') != ('RED', 'RED')I add an if statement for if the parallel lights are GREEN AND the cross lights are YELLOW, in
src/traffic_light/__init__.pythe test passes.
control( current_parallel='GREEN', current_cross='YELLOW', timer_done=False, red_phase='BOOM' ) -> 'RED', 'RED' control( current_parallel='GREEN', current_cross='GREEN', timer_done=False, red_phase='BOOM' ) -> 'RED', 'RED' control( current_parallel=RED, current_cross='POW', timer_done=False, red_phase='parallel' ) -> 'RED', 'RED' control( current_parallel='BAP', current_cross='RED', timer_done=False, red_phase='cross' ) -> 'RED', 'RED'I add an assertion to test_traffic_light_failsafe for if the parallel lights are YELLOW AND the cross lights are GREEN, in
tests/test_traffic_light.pythe terminal is my friend, and shows AssertionError
AssertionError: Tuples differ: ('YELLOW', 'GREEN') != ('RED', 'RED')I add an if statement for if the parallel lights are YELLOW AND the cross lights are GREEN, in
src/traffic_light/__init__.pythe test passes.
control( current_parallel='YELLOW', current_cross='GREEN', timer_done=False, red_phase='BOOM' ) -> 'RED', 'RED' control( current_parallel='GREEN', current_cross='YELLOW', timer_done=False, red_phase='BOOM' ) -> 'RED', 'RED' control( current_parallel='GREEN', current_cross='GREEN', timer_done=False, red_phase='BOOM' ) -> 'RED', 'RED' control( current_parallel=RED, current_cross='POW', timer_done=False, red_phase='parallel' ) -> 'RED', 'RED' control( current_parallel='BAP', current_cross='RED', timer_done=False, red_phase='cross' ) -> 'RED', 'RED'I add an assertion to test_traffic_light_failsafe for if both lights are YELLOW, in
tests/test_traffic_light.pythe terminal is my friend, and shows AssertionError
AssertionError: Tuples differ: ('YELLOW', 'YELLOW') != ('RED', 'RED')I add an if statement for if both lights are YELLOW, in
src/traffic_light/__init__.pythe test passes.
control( current_parallel='YELLOW', current_cross='YELLOW', timer_done=False, red_phase='BOOM' ) -> 'RED', 'RED' control( current_parallel='YELLOW', current_cross='GREEN', timer_done=False, red_phase='BOOM' ) -> 'RED', 'RED' control( current_parallel='GREEN', current_cross='YELLOW', timer_done=False, red_phase='BOOM' ) -> 'RED', 'RED' control( current_parallel='GREEN', current_cross='GREEN', timer_done=False, red_phase='BOOM' ) -> 'RED', 'RED' control( current_parallel=RED, current_cross='POW', timer_done=False, red_phase='parallel' ) -> 'RED', 'RED' control( current_parallel='BAP', current_cross='RED', timer_done=False, red_phase='cross' ) -> 'RED', 'RED'
I add an if statement for if the parallel lights and cross lights are the same AND are not equal to RED
the tests are still green.
I remove the commented lines
I add a return statement with the safety state as what the
controlfunction returns by default. This way if none of its conditions are met, it puts the lights in the safety statestill green.
I remove
if current_cross == yellow:fromif red_phase == 'parallel':since it returns the safety stategreen.
I remove
if current_parallel == yellow:fromif red_phase == 'cross':since it returns the safety statestill green.
extract is_not_safe function
I add a function to check for when both lights allow traffic at the same time
1def is_not_safe(parallel, cross): 2 red, yellow, green = 'RED', 'YELLOW', 'GREEN' 3 if parallel == cross != red: 4 return True 5 if parallel == green and cross == yellow: 6 return True 7 if parallel == yellow and cross == green: 8 return True 9 10 11def control( 12 timer_done, red_phase='parallel', 13 current_parallel='RED', current_cross='RED', 14 ): 15 red, yellow, green = 'RED', 'YELLOW', 'GREEN'I add a call to the is_not_safe function for the if statements that check if the lights allow traffic both ways
the tests are still green.
I remove the other if statements that check if the lights allow traffic both ways, because they are no longer needed
I write a conditional expression for the if statements in the is_not_safe function
1def is_not_safe(parallel, cross): 2 red, yellow, green = 'RED', 'YELLOW', 'GREEN' 3 return ( 4 (parallel == cross != red) 5 or (parallel == green and cross == yellow) 6 or (parallel == yellow and cross == green) 7 ) 8 if parallel == cross != red: 9 return True 10 if parallel == green and cross == yellow: 11 return True 12 if parallel == yellow and cross == green: 13 return True 14 15 16 17def control( 18 timer_done, red_phase='parallel', 19 current_parallel='RED', current_cross='RED', 20 ):still green.
I remove the other if statements from the is_not_safe function because they are no longer used
1def is_not_safe(parallel, cross): 2 red, yellow, green = 'RED', 'YELLOW', 'GREEN' 3 return ( 4 (parallel == cross != red) 5 or (parallel == green and cross == yellow) 6 or (parallel == yellow and cross == green) 7 ) 8 9 10 11def control( 12 timer_done, red_phase='parallel', 13 current_parallel='RED', current_cross='RED', 14 ):
extract is_not_light function
I add a function to check if the value of a light is GREEN or YELLOW or RED
1def is_not_safe(parallel, cross): 2 red, yellow, green = 'RED', 'YELLOW', 'GREEN' 3 return ( 4 (parallel == cross != red) 5 or (parallel == green and cross == yellow) 6 or (parallel == yellow and cross == green) 7 ) 8 9 10def is_not_light(light): 11 red, yellow, green = 'RED', 'YELLOW', 'GREEN' 12 if not ( 13 light == green or light == yellow or light == red 14 ): 15 return True 16 17 18def control( 19 timer_done, red_phase='parallel', 20 current_parallel='RED', current_cross='RED', 21 ):I add a call to the is_not_light function for the if statements that check if
current_parallelis GREEN or YELLOW or REDgreen.
I add a call to the is_not_light function for the if statements that check if
current_crossis GREEN or YELLOW or REDgreen.
I remove the commented lines from the
controlfunctionI add a conditional expression to the is_not_light function
still green.
I remove the if statement from the is_not_light function
extract triggers_failsafe function
extract global variables for lights
I add global variables for GREEN, YELLOW and RED
1GREEN, YELLOW, RED = 'GREEN', 'YELLOW', 'RED' 2 3 4def is_not_safe(parallel, cross):I use the variables for GREEN, YELLOW and RED in the is_not_safe function
still green.
I remove the commented lines from the is_not_safe function
I use the variables for GREEN, YELLOW and RED in the is_not_light function
green.
I remove the commented lines from the is_not_light function
I use the variables for GREEN, YELLOW and RED in the
controlfunctionstill green.
I remove the commented lines from the
controlfunction
extract next_light function
I add a function for when the timer is done and none of the fail safes are triggered
I add a call to the next_light function from
if timer_done:in thecontrolfunctionthe tests are still green.
I remove the other statements in the
controlfunction
When the control function is called it calls the triggers_failsafe function to check if the values of current_parallel or current_cross trigger the failsafe. The triggers_failsafe function calls the is_not_light function or is_not_safe function to check the values of current_parallel and current_cross
If the failsafe is triggered, it turns the parallel and cross lights RED
src.traffic_light.control( current_parallel='RED', current_cross='POW', timer_done=False, red_phase='BOOM' ) -> 'RED', 'RED' └── src/traffic_light/__init__.py └── def control( timer_done, red_phase='parallel', current_parallel=RED, current_cross=RED, ): └── if triggers_failsafe(current_parallel, current_cross): ├── def triggers_failsafe(parallel, cross): │ └── return ( │ │ is_not_light(parallel) │ └── or is_not_light(cross) │ or is_not_safe(parallel, cross) │ ) │ └── def is_not_light(light): │ └── return not ( │ light == GREEN or light == YELLOW │ or light == RED │ ) │ return True └── return RED, RED if not timer_done:src.traffic_light.control( current_parallel='BAP', current_cross='RED', timer_done=False, red_phase='BOOM' ) -> 'RED', 'RED' └── src/traffic_light/__init__.py └── def control( timer_done, red_phase='parallel', current_parallel=RED, current_cross=RED, ): └── if triggers_failsafe(current_parallel, current_cross): ├── def triggers_failsafe(parallel, cross): │ └── return ( │ └── is_not_light(parallel) │ or is_not_light(cross) │ or is_not_safe(parallel, cross) │ ) │ └── def is_not_light(light): │ └── return not ( │ light == GREEN or light == YELLOW │ or light == RED │ ) │ return True └── return RED, RED if not timer_done:src.traffic_light.control( current_parallel='GREEN', current_cross='GREEN', timer_done=False, red_phase='BOOM' ) -> 'RED', 'RED' └── src/traffic_light/__init__.py └── def control( timer_done, red_phase='parallel', current_parallel=RED, current_cross=RED, ): └── if triggers_failsafe(current_parallel, current_cross): ├── def triggers_failsafe(parallel, cross): │ └── return ( │ │ is_not_light(parallel) or is_not_light(cross) │ └── or is_not_safe(parallel, cross) │ ) │ └── def is_not_safe(parallel, cross): │ └── return ( │ └── (parallel == cross != RED) │ or (parallel == GREEN and cross == YELLOW) │ or (parallel == YELLOW and cross == GREEN) │ ) │ return True └── return RED, RED if not timer_done:src.traffic_light.control( current_parallel='GREEN', current_cross='YELLOW', timer_done=False, red_phase='BOOM' ) -> 'RED', 'RED' └── src/traffic_light/__init__.py └── def control( timer_done, red_phase='parallel', current_parallel=RED, current_cross=RED, ): └── if triggers_failsafe(current_parallel, current_cross): ├── def triggers_failsafe(parallel, cross): │ └── return ( │ │ is_not_light(parallel) or is_not_light(cross) │ └── or is_not_safe(parallel, cross) │ ) │ └── def is_not_safe(parallel, cross): │ └── return ( │ │ (parallel == cross != RED) │ └── or (parallel == GREEN and cross == YELLOW) │ or (parallel == YELLOW and cross == GREEN) │ ) │ return True └── return RED, RED if not timer_done:src.traffic_light.control( current_parallel='YELLOW', current_cross='GREEN', timer_done=False, red_phase='BOOM' ) -> 'RED', 'RED' └── src/traffic_light/__init__.py └── def control( timer_done, red_phase='parallel', current_parallel=RED, current_cross=RED, ): └── if triggers_failsafe(current_parallel, current_cross): ├── def triggers_failsafe(parallel, cross): │ └── return ( │ │ is_not_light(parallel) or is_not_light(cross) │ └── or is_not_safe(parallel, cross) │ ) │ └── def is_not_safe(parallel, cross): │ └── return ( │ │ (parallel == cross != RED) │ │ or (parallel == GREEN and cross == YELLOW) │ └── or (parallel == YELLOW and cross == GREEN) │ ) │ return True └── return RED, RED if not timer_done:src.traffic_light.control( current_parallel='YELLOW', current_cross='YELLOW', timer_done=False, red_phase='BOOM' ) -> 'RED', 'RED' └── src/traffic_light/__init__.py └── def control( timer_done, red_phase='parallel', current_parallel=RED, current_cross=RED, ): └── if triggers_failsafe(current_parallel, current_cross): ├── def triggers_failsafe(parallel, cross): │ └── return ( │ │ is_not_light(parallel) or is_not_light(cross) │ └── or is_not_safe(parallel, cross) │ ) │ └── def is_not_safe(parallel, cross): │ └── return ( │ └── (parallel == cross != RED) │ or (parallel == GREEN and cross == YELLOW) │ or (parallel == YELLOW and cross == GREEN) │ ) │ return True └── return RED, RED if not timer_done:If the failsafe is NOT triggered, it checks if the timer is NOT done
If the timer is NOT done, it returns the values of
current_parallelandcurrent_cross, which means it does not change the parallel or cross lights, it keeps them the samesrc.traffic_light.control( current_parallel='RED', current_cross='GREEN', timer_done=False, red_phase='parallel' ) -> 'RED', 'GREEN' └── src/traffic_light/__init__.py └── def control( timer_done, red_phase='parallel', current_parallel=RED, current_cross=RED, ): ├── if triggers_failsafe(current_parallel, current_cross): │ return RED, RED └── if not timer_done: └── return current_parallel, current_cross return 'RED' , 'GREEN' if timer_done:src.traffic_light.control( current_parallel='RED', current_cross='YELLOW', timer_done=False, red_phase='parallel' ) -> 'RED', 'YELLOW' └── src/traffic_light/__init__.py └── def control( timer_done, red_phase='parallel', current_parallel='RED', current_cross='RED', ): ├── if triggers_failsafe(current_parallel, current_cross): │ return RED, RED └── if not timer_done: └── return current_parallel, current_cross return 'RED' , 'YELLOW' if timer_done:src.traffic_light.control( current_parallel='RED', current_cross='RED', timer_done=False, red_phase='parallel' ) -> 'RED', 'RED' └── src/traffic_light/__init__.py └── def control( timer_done, red_phase='parallel', current_parallel='RED', current_cross='RED', ): ├── if triggers_failsafe(current_parallel, current_cross): │ return RED, RED └── if not timer_done: └── return current_parallel, current_cross return 'RED' , 'RED' if timer_done:src.traffic_light.control( current_parallel='GREEN', current_cross='RED', timer_done=False, red_phase='cross' ) -> 'GREEN', 'RED' └── src/traffic_light/__init__.py └── def control( timer_done, red_phase='parallel', current_parallel='RED', current_cross='RED', ): ├── if triggers_failsafe(current_parallel, current_cross): │ return RED, RED └── if not timer_done: └── return current_parallel, current_cross return 'GREEN' , 'RED' if timer_done:src.traffic_light.control( current_parallel='YELLOW', current_cross='RED', timer_done=False, red_phase='cross' ) -> 'YELLOW', 'RED' └── src/traffic_light/__init__.py └── def control( timer_done, red_phase='parallel', current_parallel='RED', current_cross='RED', ): ├── if triggers_failsafe(current_parallel, current_cross): │ return RED, RED └── if not timer_done: └── return current_parallel, current_cross return 'YELLOW' , 'RED' if timer_done:src.traffic_light.control( current_parallel='RED', current_cross='RED', timer_done=False, red_phase='cross' ) -> 'RED', 'RED' └── src/traffic_light/__init__.py └── def control( timer_done, red_phase='parallel', current_parallel='RED', current_cross='RED', ): ├── if triggers_failsafe(current_parallel, current_cross): │ return RED, RED └── if not timer_done: └── return current_parallel, current_cross return 'RED' , 'RED' if timer_done:If the timer is done, it calls the next_light function which checks if
'cross'traffic is in the RED phaseIf
'cross'traffic is in the RED phase, it checks the value ofcurrent_parallelIf the current parallel light is GREEN, it returns
YELLOW, RED, which means the next parallel light will be YELLOW and the cross light will remain RED because it is still in the RED phasesrc.traffic_light.control( current_parallel='GREEN', current_cross='RED', timer_done=True, red_phase='cross' ) -> 'YELLOW', 'RED' └── src/traffic_light/__init__.py └── def control( timer_done, red_phase='parallel', current_parallel='RED', current_cross='RED', ): ├── if triggers_failsafe(current_parallel, current_cross): │ return RED, RED ├── if not timer_done: │ return current_parallel, current_cross └── if timer_done: └── return next_light( red_phase, current_parallel, current_cross ) └── def next_light(red_phase, parallel, cross): └── if red_phase == 'cross': └── if parallel == GREEN: └── return YELLOW, RED if parallel == RED:If the current parallel light is RED, it returns
RED, GREEN, which means the parallel light will stay RED since it is now in the RED phase and the next cross light will be GREENsrc.traffic_light.control( current_parallel='RED', current_cross='RED', timer_done=True, red_phase='cross' ) -> 'RED', 'GREEN' └── src/traffic_light/__init__.py └── def control( timer_done, red_phase='parallel', current_parallel='RED', current_cross='RED', ): ├── if triggers_failsafe(current_parallel, current_cross): │ return RED, RED ├── if not timer_done: │ return current_parallel, current_cross └── if timer_done: └── return next_light( red_phase, current_parallel, current_cross ) └── def next_light(red_phase, parallel, cross): └── if red_phase == 'cross': ├── if parallel == GREEN: │ return YELLOW, RED └── if parallel == RED: └── return RED, GREEN if red_phase == 'parallel':If
'cross'traffic is not in the RED phase, it checks if'parallel'traffic is in the RED phase
If
'parallel'traffic is in the RED phase, it checks the value ofcurrent_crossIf the current cross light is GREEN, it returns
RED, YELLOW, which means the parallel light will remain RED because it is still in the RED phase and the next cross light will be YELLOWsrc.traffic_light.control( current_parallel='RED', current_cross='GREEN', timer_done=True, red_phase='parallel' ) -> 'RED', 'YELLOW' └── src/traffic_light/__init__.py └── def control( timer_done, red_phase='parallel', current_parallel='RED', current_cross='RED', ): ├── if triggers_failsafe(current_parallel, current_cross): │ return RED, RED ├── if not timer_done: │ return current_parallel, current_cross └── if timer_done: └── return next_light( red_phase, current_parallel, current_cross ) └── def next_light(red_phase, parallel, cross): ├── if red_phase == 'cross': │ ... └── if red_phase == 'parallel': └── if cross == GREEN: └── return RED, YELLOW if cross == RED:If the current cross light is RED, it returns
GREEN, RED, which means the next parallel light will be GREEN and the cross light will stay RED since it is now in the RED phasesrc.traffic_light.control( current_parallel='RED', current_cross='RED', timer_done=True, red_phase='parallel' ) -> 'GREEN', 'RED' └── src/traffic_light/__init__.py └── def control( timer_done, red_phase='parallel', current_parallel='RED', current_cross='RED', ): ├── if triggers_failsafe(current_parallel, current_cross): │ return RED, RED ├── if not timer_done: │ return current_parallel, current_cross └── if timer_done: └── return next_light( red_phase, current_parallel, current_cross ) └── def next_light(red_phase, parallel, cross): ├── if red_phase == 'cross': │ ... └── if red_phase == 'parallel': ├── if cross == GREEN: │ return RED, YELLOW └── if cross == RED: └── return GREEN, RED return RED, REDIf none of the above conditions are met, it returns
RED, RED, which means there will be no traffic in the intersection, the parallel and cross lights will both be REDsrc.traffic_light.control( current_parallel='YELLOW', current_cross='RED', timer_done=True, red_phase='cross' ) -> 'RED', 'RED' └── src/traffic_light/__init__.py └── def control( timer_done, red_phase='parallel', current_parallel='RED', current_cross='RED', ): ├── if triggers_failsafe(current_parallel, current_cross): │ return RED, RED ├── if not timer_done: │ return current_parallel, current_cross └── if timer_done: └── return next_light( red_phase, current_parallel, current_cross ) └── def next_light(red_phase, parallel, cross): └── if red_phase == 'cross': ├── if parallel == GREEN: │ return YELLOW, RED ┌───┴── if parallel == RED: │ return RED, GREEN │ if red_phase == 'parallel': │ ... └── return RED, REDsrc.traffic_light.control( current_parallel='RED', current_cross='YELLOW', timer_done=True, red_phase='parallel' ) -> 'RED', 'RED' └── src/traffic_light/__init__.py └── def control( timer_done, red_phase='parallel', current_parallel='RED', current_cross='RED', ): ├── if triggers_failsafe(current_parallel, current_cross): │ return RED, RED ├── if not timer_done: │ return current_parallel, current_cross └── if timer_done: └── return next_light( red_phase, current_parallel, current_cross ) └── def next_light(red_phase, parallel, cross): ├── if red_phase == 'cross': │ ... └── if red_phase == 'parallel': ├── if cross == GREEN: │ return RED, YELLOW ┌───┴── if cross == RED: │ return GREEN, RED └── return RED, RED
If none of the above conditions are met, it returns
RED, RED, which means there will be no traffic in the intersection, the parallel and cross lights will both be REDsrc/traffic_light/__init__.py └── def control( timer_done, red_phase='parallel', current_parallel='RED', current_cross='RED', ): ├── if triggers_failsafe(current_parallel, current_cross): │ return RED, RED ├── if not timer_done: │ return current_parallel, current_cross ├── if timer_done: │ return next_light( │ red_phase, current_parallel, current_cross │ ) └── return RED, RED
close the project
review
I ran tests for a Traffic Light that changes color based on what RED phase the traffic is in ('cross' or 'parallel') AND if a timer is done or NOT done
The outputs are the next lights for Parallel and Cross Traffic which gave me this truth table
red |
current |
current |
next |
next |
|
|---|---|---|---|---|---|
phase |
parallel |
cross |
timer |
parallel |
cross |
‘cross’ |
GREEN |
RED |
NOT done |
GREEN |
RED |
‘cross’ |
GREEN |
RED |
done |
YELLOW |
RED |
‘cross’ |
YELLOW |
RED |
NOT done |
YELLOW |
RED |
‘cross’ |
YELLOW |
RED |
done |
safety RED |
safety RED |
‘cross’ |
RED |
RED |
NOT done |
safety RED |
safety RED |
‘cross’ |
RED |
RED |
done |
RED |
GREEN |
red |
current |
current |
next |
next |
|
|---|---|---|---|---|---|
phase |
parallel |
cross |
timer |
parallel |
cross |
‘parallel’ |
RED |
GREEN |
NOT done |
RED |
GREEN |
‘parallel’ |
RED |
GREEN |
done |
RED |
YELLOW |
‘parallel’ |
RED |
YELLOW |
NOT done |
RED |
YELLOW |
‘parallel’ |
RED |
YELLOW |
done |
safety RED |
safety RED |
‘parallel’ |
RED |
RED |
NOT done |
safety RED |
safety RED |
‘parallel’ |
RED |
RED |
done |
GREEN |
RED |
It also makes sure that there is never a case where the lights allow cars to move through the intersection at the same time (to try to avoid accidents).
What if the Traffic Light has a walk button and I push it? What if the Traffic Light changes based on if there is an emergency vehicle? What would the inputs be and what truth table do I get?
code from the chapter
what is next?
I know how to make a Python Test Driven Development environment manually.
I know how to make a Python Test Driven Development environment automatically
I changed the name of the project in a few places every time I ran makePythonTdd, that is too many steps, would you like to see a better way to make a Python Test Driven Development environment automatically?
rate pumping python
If this has been a 7 star experience for you, please CLICK HERE to leave a 5 star review of pumping python. It helps other people get into the book too.