Last active
August 24, 2020 22:31
-
-
Save ivangeorgiev/a6b3d6dc458391a38e693af7aba7a99c to your computer and use it in GitHub Desktop.
Data pipelining in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools | |
class Pipe: | |
def __init__(self, seq): | |
self._seq = seq | |
def __iter__(self): | |
return iter(self._seq) | |
def map(self, func): | |
return Pipe(map(func, self)) | |
def flat_map(self, func=None): | |
maped_iter = self if func is None else map(func, self) | |
return Pipe(itertools.chain.from_iterable(maped_iter)) | |
def filter(self, func=None): | |
return Pipe(filter(func, self)) | |
def before(self, func): | |
return Pipe(before_it(func, self)) | |
def after(self, func): | |
return Pipe(after_it(func, self)) | |
@classmethod | |
def from_repeated(cls, value): | |
return Pipe(repeat(value)) | |
def repeat(value): | |
func = value if callable else lambda: value | |
while True: | |
yield func() | |
def before_it(func, iter1): | |
for it in iter1: | |
func() | |
yield it | |
def after_it(func, iter1): | |
for it in iter1: | |
yield it | |
func() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment