Sequence is a wrapper around any type of sequence which provides access to common functional transformations and reductions in a data pipeline style
| 27 | |
| 28 | |
| 29 | class Sequence(object): |
| 30 | """ |
| 31 | Sequence is a wrapper around any type of sequence which provides access to common |
| 32 | functional transformations and reductions in a data pipeline style |
| 33 | """ |
| 34 | |
| 35 | def __init__(self, sequence, transform=None, engine=None, max_repr_items=None): |
| 36 | # pylint: disable=protected-access |
| 37 | """ |
| 38 | Takes a Sequence, list, tuple. or iterable sequence and wraps it around a Sequence object. |
| 39 | If the sequence is already an instance of Sequence, it will in total be wrapped exactly |
| 40 | once. A TypeError is raised if sequence is none of these. |
| 41 | |
| 42 | :param sequence: sequence of items to wrap in a Sequence |
| 43 | :param transform: transformation to apply |
| 44 | :param engine: execution engine |
| 45 | :param max_repr_items: maximum number of items to print with repr |
| 46 | :return: sequence wrapped in a Sequence |
| 47 | """ |
| 48 | self.engine = engine or ExecutionEngine() |
| 49 | if isinstance(sequence, Sequence): |
| 50 | self._max_repr_items = max_repr_items or sequence._max_repr_items |
| 51 | self._base_sequence = sequence._base_sequence |
| 52 | self._lineage = Lineage(prior_lineage=sequence._lineage, engine=engine) |
| 53 | elif isinstance(sequence, (list, tuple)) or is_iterable(sequence): |
| 54 | self._max_repr_items = max_repr_items |
| 55 | self._base_sequence = sequence |
| 56 | self._lineage = Lineage(engine=engine) |
| 57 | else: |
| 58 | raise TypeError("Given sequence must be an iterable value") |
| 59 | if transform is not None: |
| 60 | self._lineage.apply(transform) |
| 61 | |
| 62 | def __iter__(self): |
| 63 | """ |
| 64 | Return iterator of sequence. |
| 65 | |
| 66 | :return: iterator of sequence |
| 67 | """ |
| 68 | return self._evaluate() |
| 69 | |
| 70 | def __eq__(self, other): |
| 71 | """ |
| 72 | Checks for equality with the sequence's equality operator. |
| 73 | |
| 74 | :param other: object to compare to |
| 75 | :return: true if the underlying sequence is equal to other |
| 76 | """ |
| 77 | return self.sequence == other |
| 78 | |
| 79 | def __ne__(self, other): |
| 80 | """ |
| 81 | Checks for inequality with the sequence's inequality operator. |
| 82 | |
| 83 | :param other: object to compare to |
| 84 | :return: true if the underlying sequence is not equal to other |
| 85 | """ |
| 86 | return self.sequence != other |
no outgoing calls