-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeneratorGenerator.py
39 lines (27 loc) · 1.12 KB
/
GeneratorGenerator.py
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
import sys
class GeneratorInterface(object):
def Generate(self):
raise NotImplementedError("Should have implemented Generate")
class GeneratorGenerator(GeneratorInterface):
def __init__(self, some_yielder_class, yielder_init_args=None):
self.generatee = some_yielder_class
self.yielder_init_args = yielder_init_args
def Generate(self, iterations):
for x in range(0, iterations):
if self.yielder_init_args is not None:
gen = self.generatee(self.yielder_init_args)
else:
gen = self.generatee()
yield gen
class ExampleGenerator(GeneratorInterface):
def Generate(self, iterations):
for x in range(0, iterations):
yield 'This is example number ' + str(x)
if __name__ == "__main__":
gen = GeneratorGenerator(ExampleGenerator)
counter = 0
for generator in gen.Generate(3):
counter += 1
print('This is generator number ' + str(counter))
for x in generator.Generate(3):
print(str(x) + ' of generator number ' + str(counter))