Skip to content

edspdf.utils.collections

multi_tee

Makes copies of an iterable such that every iteration over it starts from 0. If the iterable is a sequence (list, tuple), just returns it since every iter() over the object restart from the beginning

Source code in edspdf/utils/collections.py
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
class multi_tee:
    """
    Makes copies of an iterable such that every iteration over it
    starts from 0. If the iterable is a sequence (list, tuple), just returns
    it since every iter() over the object restart from the beginning
    """

    def __new__(cls, iterable):
        if isinstance(iterable, Sequence):
            return iterable
        return super().__new__(cls)

    def __init__(self, iterable):
        self.main, self.copy = itertools.tee(iterable)

    def __iter__(self):
        if self.copy is not None:
            it = self.copy
            self.copy = None
            return it
        return copy.copy(self.main)