Created
August 30, 2015 23:51
-
-
Save shoyer/9a97196c8876cc66c957 to your computer and use it in GitHub Desktop.
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
class partial_by_order(object): | |
""" | |
>>> f = partial_by_order(add, [(1, 10)]) | |
>>> f(5) | |
15 | |
""" | |
def __init__(self, op, other): | |
if (not isinstance(other, list) or | |
not all(isinstance(o, tuple) and len(o) == 2 for o in other)): | |
raise ValueError('input must be list of tuples') | |
self.op = op | |
self.other = other | |
def __call__(self, *args): | |
args2 = list(args) | |
for i, arg in self.other: | |
args2.insert(i, arg) | |
return self.op(*args2) | |
@property | |
def __name__(self): | |
if len(self.other) == 1: | |
other_arg = self.other[0][1] | |
else: | |
other_arg = '...' | |
return '{0}({1})'.format(self.op.__name__, other_arg) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment