File size: 1,686 Bytes
6e18947
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
48
49
50
51
52
53
54
55
56
57
58
import inspect
import itertools


class ReusableGenerator:
    def __init__(self, generator, gen_argv=[], gen_kwargs={}):
        self._generator = generator
        self._gen_kwargs = gen_kwargs
        self._gen_argv = gen_argv

    def get_generator(self):
        return self._generator

    def get_gen_kwargs(self):
        return self._gen_kwargs

    def construct(self):
        return self._generator(*self._gen_argv, **self._gen_kwargs)

    def __iter__(self):
        return iter(self.construct())

    def __call__(self):
        yield from self.construct()

    def __repr__(self):
        return f"{self.__class__.__name__}({self._generator.__name__}, gen_argv={self._gen_argv}, gen_kwargs={self._gen_kwargs})"


if __name__ == "__main__":
    from itertools import chain, islice

    # Creating objects of MyIterable
    iterable1 = ReusableGenerator(range, gen_argv=[1, 4])
    iterable2 = ReusableGenerator(range, gen_argv=[4, 7])

    # Using itertools.chain
    chained = list(chain(iterable1, iterable2))
    print(chained)  # Prints: [1, 2, 3, 4, 5, 6]

    # Using itertools.islice
    sliced = list(islice(ReusableGenerator(range, gen_argv=[1, 7]), 1, 4))
    print(sliced)  # Prints: [2, 3, 4]

    # now same test with generators
    def generator(start, end):
        for i in range(start, end):
            yield i

    iterable1 = ReusableGenerator(generator, gen_argv=[1, 4])
    iterable2 = ReusableGenerator(generator, gen_argv=[4, 7])

    chained = list(chain(iterable1, iterable2))
    print(chained)  # Prints: [1, 2, 3, 4, 5, 6]

    sliced = list(islice(ReusableGenerator(generator, gen_argv=[1, 7]), 1, 4))
    print(sliced)  # Prints: [2, 3, 4]