A wrapper class for generators and custom iterable classes that returns a new iterator to the start of the sequence when :meth:`IterWrapper.__iter__` is called. If the wrapped object is a generator, a copy of the generator is constructed and returned when :meth:`IterWrapper.__iter__
| 323 | |
| 324 | |
| 325 | class IterWrapper(Iterable): |
| 326 | """A wrapper class for generators and custom iterable classes that returns |
| 327 | a new iterator to the start of the sequence when |
| 328 | :meth:`IterWrapper.__iter__` is called. |
| 329 | |
| 330 | If the wrapped object is a generator, a copy of the generator is |
| 331 | constructed and returned when :meth:`IterWrapper.__iter__` is called. |
| 332 | If the wrapped object is a custom type, then the :meth:`copy.copy` is |
| 333 | called and a new instance is returned. In both cases, the original |
| 334 | iterable object is unchanged. |
| 335 | |
| 336 | Note: |
| 337 | |
| 338 | Do not increment the wrapped iterable outside of this wrapper. |
| 339 | |
| 340 | """ |
| 341 | def __init__(self, wrapped): |
| 342 | """Initialize an :class:`wrf.IterWrapper` object. |
| 343 | |
| 344 | Args: |
| 345 | |
| 346 | wrapped (an iterable object): Any iterable object that contains the |
| 347 | *__iter__* method. |
| 348 | |
| 349 | """ |
| 350 | self._wrapped = wrapped |
| 351 | |
| 352 | def __iter__(self): |
| 353 | """Return an iterator to the start of the sequence. |
| 354 | |
| 355 | Returns: |
| 356 | |
| 357 | An iterator to the start of the sequence. |
| 358 | |
| 359 | """ |
| 360 | if isinstance(self._wrapped, GeneratorType): |
| 361 | |
| 362 | gen_copy = _generator_copy(self._wrapped) |
| 363 | # If a tuple comes back, then this is a generator expression, |
| 364 | # so store the first tee'd item, then return the other |
| 365 | if isinstance(gen_copy, tuple): |
| 366 | self._wrapped = gen_copy[0] |
| 367 | return gen_copy[1] |
| 368 | |
| 369 | return gen_copy |
| 370 | else: |
| 371 | obj_copy = copy(self._wrapped) |
| 372 | return obj_copy.__iter__() |
| 373 | |
| 374 | |
| 375 | def get_iterable(wrfseq): |