GH avatar Bite 63. Use an infinite iterator to simulate a traffic light

Complete traffic_light using itertools.cycle to return an infinite iterator to similate a traffic light.

When called it should return a State namedtuple for each new state, here is how an islice looks:

from pprint import pprint as pp
pp(list(islice(traffic_light(), 10)))

This captures the first 10 iterated values of traffic_light into a list and pretty prints them:

[State(color='red', command='Stop', timeout=2),
 State(color='green', command='Go', timeout=2),
 State(color='amber', command='Caution', timeout=0.5),
 State(color='red', command='Stop', timeout=2),
 State(color='green', command='Go', timeout=2),
 State(color='amber', command='Caution', timeout=0.5),
 State(color='red', command='Stop', timeout=2),
 State(color='green', command='Go', timeout=2),
 State(color='amber', command='Caution', timeout=0.5),
 State(color='red', command='Stop', timeout=2)]

Good luck and have fun!

Will you be Pythonista #34 to crack this Bite?
It takes an average of ~20 minutes to solve this Bite (submissions 5-240 min).
Pythonistas rate this Bite 1.5 on a 1-10 difficulty scale.
» You can do it! 😌

Login and get coding
We use Python 3.7