The itertools module from the standard library contains nice features. In this particular case, the loop function is good.
help loop says:
loop (iterable) β loop object
Returns items from an iterable until it is exhausted. then repeat the sequence indefinitely.
Using a loop in your solution:
import itertools def g_user(): while True: yield read_user_input() def g_socket(): while True: yield read_socket_input() def g_combine(*generators): for generator in itertools.cycle(generators): yield generator.next() g_combined = g_combine(g_user(), g_socket())
Considerations for this code:
g_combine ends with the first generator that calls StopIteration
g_combine accepts N generators
How to correct exceptions? Think about it.
Rodrigo AraΓΊjo
source share