Coverage for pipable/__init__.py: 100%
13 statements
« prev ^ index » next coverage.py v7.1.0, created at 2023-02-10 02:26 +0800
« prev ^ index » next coverage.py v7.1.0, created at 2023-02-10 02:26 +0800
1"""This package try to mimic pipe operation by overriding the bitwise-or operator,
2turn it into an infix function that take the output of previous expression as the first argument of the current function.
3"""
5from typing import Callable, Any, Iterable
6from functools import partial
9class Pipe(object):
10 """This class create the `Pipe` object that mimic pipe operation:
12 - instatiate by creating partial of existing function
13 - turn the bitwise-or operator `|` into an infix function that accept the output of previous expression.
14 ie. pipe operator
15 """
17 def __init__(self, func: Callable, /, *args, **kwargs) -> None:
18 """create pipable partial for the target func
20 Args:
21 func (Callable): func to be pipable
22 args: partial's positional args
23 kwargs: partial's keyword args
24 """
25 self.pipe = partial(func, *args, **kwargs)
27 def __ror__(self, precedent: Any):
28 """override the builit-in `|` operator, turn it into pipe"""
29 # return partial(self.func, precedent)
30 return self.pipe(precedent)
32 def __rrshift__(self, precedent: Iterable):
33 """override the builit-in `>>` operator, pass precedent as destructured iterable to the pipe"""
34 return self.pipe(*precedent)
36 def __rlshift__(self, precedent: dict):
37 """override the builit-in `>>=` operator, pass as destructured dict to the pipe"""
38 return self.pipe(**precedent)
40 def __call__(self, *args, **kwargs):
41 """replace arguments of the pipable partial"""
42 return Pipe(self.pipe.func, *args, **kwargs)