-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcomposable.py
More file actions
30 lines (23 loc) · 1.02 KB
/
composable.py
File metadata and controls
30 lines (23 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
"""A composable decorator"""
from functools import wraps
class composable:
"""Decorator that allows chaining of simple functions"""
def __init__(self, func: callable):
self.func = func
wraps(func)(self)
def __call__(self, *args, **kwargs):
"""Called when the function is called directly"""
return self.func(*args, **kwargs)
def __or__(self, on_the_right):
"""Called when a chain is being stored for later use"""
if callable(on_the_right):
return composable(lambda value: on_the_right(self.func(value)))
return composable(lambda: self.func(on_the_right))
def __ror__(self, on_the_left):
"""Called when static values are piped into the function"""
return self.func(on_the_left)
# Note:
# Under normal circumstances, when you call `a | b`, Python will effectively
# attempt to call `a.__or__(b)`. If a doesn't implement `__or__`, or if a
# `NotImplemented` is returned, Python will then attempt to call `b.__ror__(a)`
# as a fallback.