Last active
August 29, 2015 14:21
-
-
Save willstott101/10454087f7ba0b5b370b to your computer and use it in GitHub Desktop.
This file contains 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
from itertools import chain | |
class DeepPartial(object): | |
def __init__(self, func, *args, **kwargs): | |
self.func = func | |
self.args = args | |
self.kwargs = kwargs | |
def _call(self, callee): | |
if isinstance(callee, self.__class__): | |
return callee() | |
else: | |
return callee | |
def _call_dpartials(self, args, kwargs): | |
called = [self._call(a) for a in args] | |
called_kw = {n: self._call(v) for n, v in kwargs.items()} | |
return called, called_kw | |
def _call_children(self): | |
return self._call_dpartials(self.args, self.kwargs) | |
def __call__(self, *iargs, **ikwargs): | |
args, kwargs = self._call_children() | |
kwargs.update(ikwargs) | |
return self.func(*chain(args, iargs), **kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment