MCPcopy Create free account
hub / github.com/ActiveState/code / Curry

Class Curry

recipes/Python/52564_Curried_functions/recipe-52564.py:4–33  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2CO_VARKEYWORDS = 0x0008
3
4class Curry:
5 def __init__(self, f):
6 self.hasv = f.func_code.co_flags & CO_VARARGS
7 self.hask = f.func_code.co_flags & CO_VARKEYWORDS
8 self.defaults = f.func_defaults or ()
9 self.defnum = len(self.defaults)
10 self.f = f
11 self.argnum = f.func_code.co_argcount
12 self._reset()
13 def __call__(self, *a, **k):
14 if k and not self.hask:
15 raise TypeError, "%s got unexpected keyword argument '%s'" %\
16 (self.f.__name__, k.popitem()[0])
17 kargs = self.kargs
18 args = self.args
19 kargs.update(k)
20 totlen = len(args) + len(a)
21 if totlen > self.argnum:
22 if not self.hasv:
23 raise TypeError, "%s takes exactly %d argument%c (%d given)" % (self.f.__name__, self.argnum, ['s',''][self.argnum==1], totlen)
24 args += a
25 self._reset()
26 return self.f(*args, **kargs)
27 if totlen >= self.argnum - self.defnum:
28 num_defaults = totlen - defnum
29 args += a + self.defaults[defnum-num_defaults:]
30 self._reset()
31 return self.f(*args, **kargs)
32 self.args += a
33 return self
34 def _reset(self):
35 self.args, self.kargs = (), {}

Callers

nothing calls this directly

Calls 3

updateMethod · 0.45
_resetMethod · 0.45
fMethod · 0.45

Tested by

no test coverage detected