functoolz.py 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  1. from functools import reduce, partial
  2. import inspect
  3. import sys
  4. from operator import attrgetter, not_
  5. from importlib import import_module
  6. from types import MethodType
  7. from .utils import no_default
  8. PYPY = hasattr(sys, 'pypy_version_info') and sys.version_info[0] > 2
  9. __all__ = ('identity', 'apply', 'thread_first', 'thread_last', 'memoize',
  10. 'compose', 'compose_left', 'pipe', 'complement', 'juxt', 'do',
  11. 'curry', 'flip', 'excepts')
  12. PYPY = hasattr(sys, 'pypy_version_info')
  13. def identity(x):
  14. """ Identity function. Return x
  15. >>> identity(3)
  16. 3
  17. """
  18. return x
  19. def apply(*func_and_args, **kwargs):
  20. """ Applies a function and returns the results
  21. >>> def double(x): return 2*x
  22. >>> def inc(x): return x + 1
  23. >>> apply(double, 5)
  24. 10
  25. >>> tuple(map(apply, [double, inc, double], [10, 500, 8000]))
  26. (20, 501, 16000)
  27. """
  28. if not func_and_args:
  29. raise TypeError('func argument is required')
  30. func, args = func_and_args[0], func_and_args[1:]
  31. return func(*args, **kwargs)
  32. def thread_first(val, *forms):
  33. """ Thread value through a sequence of functions/forms
  34. >>> def double(x): return 2*x
  35. >>> def inc(x): return x + 1
  36. >>> thread_first(1, inc, double)
  37. 4
  38. If the function expects more than one input you can specify those inputs
  39. in a tuple. The value is used as the first input.
  40. >>> def add(x, y): return x + y
  41. >>> def pow(x, y): return x**y
  42. >>> thread_first(1, (add, 4), (pow, 2)) # pow(add(1, 4), 2)
  43. 25
  44. So in general
  45. thread_first(x, f, (g, y, z))
  46. expands to
  47. g(f(x), y, z)
  48. See Also:
  49. thread_last
  50. """
  51. def evalform_front(val, form):
  52. if callable(form):
  53. return form(val)
  54. if isinstance(form, tuple):
  55. func, args = form[0], form[1:]
  56. args = (val,) + args
  57. return func(*args)
  58. return reduce(evalform_front, forms, val)
  59. def thread_last(val, *forms):
  60. """ Thread value through a sequence of functions/forms
  61. >>> def double(x): return 2*x
  62. >>> def inc(x): return x + 1
  63. >>> thread_last(1, inc, double)
  64. 4
  65. If the function expects more than one input you can specify those inputs
  66. in a tuple. The value is used as the last input.
  67. >>> def add(x, y): return x + y
  68. >>> def pow(x, y): return x**y
  69. >>> thread_last(1, (add, 4), (pow, 2)) # pow(2, add(4, 1))
  70. 32
  71. So in general
  72. thread_last(x, f, (g, y, z))
  73. expands to
  74. g(y, z, f(x))
  75. >>> def iseven(x):
  76. ... return x % 2 == 0
  77. >>> list(thread_last([1, 2, 3], (map, inc), (filter, iseven)))
  78. [2, 4]
  79. See Also:
  80. thread_first
  81. """
  82. def evalform_back(val, form):
  83. if callable(form):
  84. return form(val)
  85. if isinstance(form, tuple):
  86. func, args = form[0], form[1:]
  87. args = args + (val,)
  88. return func(*args)
  89. return reduce(evalform_back, forms, val)
  90. def instanceproperty(fget=None, fset=None, fdel=None, doc=None, classval=None):
  91. """ Like @property, but returns ``classval`` when used as a class attribute
  92. >>> class MyClass(object):
  93. ... '''The class docstring'''
  94. ... @instanceproperty(classval=__doc__)
  95. ... def __doc__(self):
  96. ... return 'An object docstring'
  97. ... @instanceproperty
  98. ... def val(self):
  99. ... return 42
  100. ...
  101. >>> MyClass.__doc__
  102. 'The class docstring'
  103. >>> MyClass.val is None
  104. True
  105. >>> obj = MyClass()
  106. >>> obj.__doc__
  107. 'An object docstring'
  108. >>> obj.val
  109. 42
  110. """
  111. if fget is None:
  112. return partial(instanceproperty, fset=fset, fdel=fdel, doc=doc,
  113. classval=classval)
  114. return InstanceProperty(fget=fget, fset=fset, fdel=fdel, doc=doc,
  115. classval=classval)
  116. class InstanceProperty(property):
  117. """ Like @property, but returns ``classval`` when used as a class attribute
  118. Should not be used directly. Use ``instanceproperty`` instead.
  119. """
  120. def __init__(self, fget=None, fset=None, fdel=None, doc=None,
  121. classval=None):
  122. self.classval = classval
  123. property.__init__(self, fget=fget, fset=fset, fdel=fdel, doc=doc)
  124. def __get__(self, obj, type=None):
  125. if obj is None:
  126. return self.classval
  127. return property.__get__(self, obj, type)
  128. def __reduce__(self):
  129. state = (self.fget, self.fset, self.fdel, self.__doc__, self.classval)
  130. return InstanceProperty, state
  131. class curry(object):
  132. """ Curry a callable function
  133. Enables partial application of arguments through calling a function with an
  134. incomplete set of arguments.
  135. >>> def mul(x, y):
  136. ... return x * y
  137. >>> mul = curry(mul)
  138. >>> double = mul(2)
  139. >>> double(10)
  140. 20
  141. Also supports keyword arguments
  142. >>> @curry # Can use curry as a decorator
  143. ... def f(x, y, a=10):
  144. ... return a * (x + y)
  145. >>> add = f(a=1)
  146. >>> add(2, 3)
  147. 5
  148. See Also:
  149. toolz.curried - namespace of curried functions
  150. https://toolz.readthedocs.io/en/latest/curry.html
  151. """
  152. def __init__(self, *args, **kwargs):
  153. if not args:
  154. raise TypeError('__init__() takes at least 2 arguments (1 given)')
  155. func, args = args[0], args[1:]
  156. if not callable(func):
  157. raise TypeError("Input must be callable")
  158. # curry- or functools.partial-like object? Unpack and merge arguments
  159. if (
  160. hasattr(func, 'func')
  161. and hasattr(func, 'args')
  162. and hasattr(func, 'keywords')
  163. and isinstance(func.args, tuple)
  164. ):
  165. _kwargs = {}
  166. if func.keywords:
  167. _kwargs.update(func.keywords)
  168. _kwargs.update(kwargs)
  169. kwargs = _kwargs
  170. args = func.args + args
  171. func = func.func
  172. if kwargs:
  173. self._partial = partial(func, *args, **kwargs)
  174. else:
  175. self._partial = partial(func, *args)
  176. self.__doc__ = getattr(func, '__doc__', None)
  177. self.__name__ = getattr(func, '__name__', '<curry>')
  178. self.__module__ = getattr(func, '__module__', None)
  179. self.__qualname__ = getattr(func, '__qualname__', None)
  180. self._sigspec = None
  181. self._has_unknown_args = None
  182. @instanceproperty
  183. def func(self):
  184. return self._partial.func
  185. @instanceproperty
  186. def __signature__(self):
  187. sig = inspect.signature(self.func)
  188. args = self.args or ()
  189. keywords = self.keywords or {}
  190. if is_partial_args(self.func, args, keywords, sig) is False:
  191. raise TypeError('curry object has incorrect arguments')
  192. params = list(sig.parameters.values())
  193. skip = 0
  194. for param in params[:len(args)]:
  195. if param.kind == param.VAR_POSITIONAL:
  196. break
  197. skip += 1
  198. kwonly = False
  199. newparams = []
  200. for param in params[skip:]:
  201. kind = param.kind
  202. default = param.default
  203. if kind == param.VAR_KEYWORD:
  204. pass
  205. elif kind == param.VAR_POSITIONAL:
  206. if kwonly:
  207. continue
  208. elif param.name in keywords:
  209. default = keywords[param.name]
  210. kind = param.KEYWORD_ONLY
  211. kwonly = True
  212. else:
  213. if kwonly:
  214. kind = param.KEYWORD_ONLY
  215. if default is param.empty:
  216. default = no_default
  217. newparams.append(param.replace(default=default, kind=kind))
  218. return sig.replace(parameters=newparams)
  219. @instanceproperty
  220. def args(self):
  221. return self._partial.args
  222. @instanceproperty
  223. def keywords(self):
  224. return self._partial.keywords
  225. @instanceproperty
  226. def func_name(self):
  227. return self.__name__
  228. def __str__(self):
  229. return str(self.func)
  230. def __repr__(self):
  231. return repr(self.func)
  232. def __hash__(self):
  233. return hash((self.func, self.args,
  234. frozenset(self.keywords.items()) if self.keywords
  235. else None))
  236. def __eq__(self, other):
  237. return (isinstance(other, curry) and self.func == other.func and
  238. self.args == other.args and self.keywords == other.keywords)
  239. def __ne__(self, other):
  240. return not self.__eq__(other)
  241. def __call__(self, *args, **kwargs):
  242. try:
  243. return self._partial(*args, **kwargs)
  244. except TypeError as exc:
  245. if self._should_curry(args, kwargs, exc):
  246. return self.bind(*args, **kwargs)
  247. raise
  248. def _should_curry(self, args, kwargs, exc=None):
  249. func = self.func
  250. args = self.args + args
  251. if self.keywords:
  252. kwargs = dict(self.keywords, **kwargs)
  253. if self._sigspec is None:
  254. sigspec = self._sigspec = _sigs.signature_or_spec(func)
  255. self._has_unknown_args = has_varargs(func, sigspec) is not False
  256. else:
  257. sigspec = self._sigspec
  258. if is_partial_args(func, args, kwargs, sigspec) is False:
  259. # Nothing can make the call valid
  260. return False
  261. elif self._has_unknown_args:
  262. # The call may be valid and raised a TypeError, but we curry
  263. # anyway because the function may have `*args`. This is useful
  264. # for decorators with signature `func(*args, **kwargs)`.
  265. return True
  266. elif not is_valid_args(func, args, kwargs, sigspec):
  267. # Adding more arguments may make the call valid
  268. return True
  269. else:
  270. # There was a genuine TypeError
  271. return False
  272. def bind(self, *args, **kwargs):
  273. return type(self)(self, *args, **kwargs)
  274. def call(self, *args, **kwargs):
  275. return self._partial(*args, **kwargs)
  276. def __get__(self, instance, owner):
  277. if instance is None:
  278. return self
  279. return curry(self, instance)
  280. def __reduce__(self):
  281. func = self.func
  282. modname = getattr(func, '__module__', None)
  283. qualname = getattr(func, '__qualname__', None)
  284. if qualname is None: # pragma: no cover
  285. qualname = getattr(func, '__name__', None)
  286. is_decorated = None
  287. if modname and qualname:
  288. attrs = []
  289. obj = import_module(modname)
  290. for attr in qualname.split('.'):
  291. if isinstance(obj, curry):
  292. attrs.append('func')
  293. obj = obj.func
  294. obj = getattr(obj, attr, None)
  295. if obj is None:
  296. break
  297. attrs.append(attr)
  298. if isinstance(obj, curry) and obj.func is func:
  299. is_decorated = obj is self
  300. qualname = '.'.join(attrs)
  301. func = '%s:%s' % (modname, qualname)
  302. # functools.partial objects can't be pickled
  303. userdict = tuple((k, v) for k, v in self.__dict__.items()
  304. if k not in ('_partial', '_sigspec'))
  305. state = (type(self), func, self.args, self.keywords, userdict,
  306. is_decorated)
  307. return _restore_curry, state
  308. def _restore_curry(cls, func, args, kwargs, userdict, is_decorated):
  309. if isinstance(func, str):
  310. modname, qualname = func.rsplit(':', 1)
  311. obj = import_module(modname)
  312. for attr in qualname.split('.'):
  313. obj = getattr(obj, attr)
  314. if is_decorated:
  315. return obj
  316. func = obj.func
  317. obj = cls(func, *args, **(kwargs or {}))
  318. obj.__dict__.update(userdict)
  319. return obj
  320. @curry
  321. def memoize(func, cache=None, key=None):
  322. """ Cache a function's result for speedy future evaluation
  323. Considerations:
  324. Trades memory for speed.
  325. Only use on pure functions.
  326. >>> def add(x, y): return x + y
  327. >>> add = memoize(add)
  328. Or use as a decorator
  329. >>> @memoize
  330. ... def add(x, y):
  331. ... return x + y
  332. Use the ``cache`` keyword to provide a dict-like object as an initial cache
  333. >>> @memoize(cache={(1, 2): 3})
  334. ... def add(x, y):
  335. ... return x + y
  336. Note that the above works as a decorator because ``memoize`` is curried.
  337. It is also possible to provide a ``key(args, kwargs)`` function that
  338. calculates keys used for the cache, which receives an ``args`` tuple and
  339. ``kwargs`` dict as input, and must return a hashable value. However,
  340. the default key function should be sufficient most of the time.
  341. >>> # Use key function that ignores extraneous keyword arguments
  342. >>> @memoize(key=lambda args, kwargs: args)
  343. ... def add(x, y, verbose=False):
  344. ... if verbose:
  345. ... print('Calculating %s + %s' % (x, y))
  346. ... return x + y
  347. """
  348. if cache is None:
  349. cache = {}
  350. try:
  351. may_have_kwargs = has_keywords(func) is not False
  352. # Is unary function (single arg, no variadic argument or keywords)?
  353. is_unary = is_arity(1, func)
  354. except TypeError: # pragma: no cover
  355. may_have_kwargs = True
  356. is_unary = False
  357. if key is None:
  358. if is_unary:
  359. def key(args, kwargs):
  360. return args[0]
  361. elif may_have_kwargs:
  362. def key(args, kwargs):
  363. return (
  364. args or None,
  365. frozenset(kwargs.items()) if kwargs else None,
  366. )
  367. else:
  368. def key(args, kwargs):
  369. return args
  370. def memof(*args, **kwargs):
  371. k = key(args, kwargs)
  372. try:
  373. return cache[k]
  374. except TypeError:
  375. raise TypeError("Arguments to memoized function must be hashable")
  376. except KeyError:
  377. cache[k] = result = func(*args, **kwargs)
  378. return result
  379. try:
  380. memof.__name__ = func.__name__
  381. except AttributeError:
  382. pass
  383. memof.__doc__ = func.__doc__
  384. memof.__wrapped__ = func
  385. return memof
  386. class Compose(object):
  387. """ A composition of functions
  388. See Also:
  389. compose
  390. """
  391. __slots__ = 'first', 'funcs'
  392. def __init__(self, funcs):
  393. funcs = tuple(reversed(funcs))
  394. self.first = funcs[0]
  395. self.funcs = funcs[1:]
  396. def __call__(self, *args, **kwargs):
  397. ret = self.first(*args, **kwargs)
  398. for f in self.funcs:
  399. ret = f(ret)
  400. return ret
  401. def __getstate__(self):
  402. return self.first, self.funcs
  403. def __setstate__(self, state):
  404. self.first, self.funcs = state
  405. @instanceproperty(classval=__doc__)
  406. def __doc__(self):
  407. def composed_doc(*fs):
  408. """Generate a docstring for the composition of fs.
  409. """
  410. if not fs:
  411. # Argument name for the docstring.
  412. return '*args, **kwargs'
  413. return '{f}({g})'.format(f=fs[0].__name__, g=composed_doc(*fs[1:]))
  414. try:
  415. return (
  416. 'lambda *args, **kwargs: ' +
  417. composed_doc(*reversed((self.first,) + self.funcs))
  418. )
  419. except AttributeError:
  420. # One of our callables does not have a `__name__`, whatever.
  421. return 'A composition of functions'
  422. @property
  423. def __name__(self):
  424. try:
  425. return '_of_'.join(
  426. (f.__name__ for f in reversed((self.first,) + self.funcs))
  427. )
  428. except AttributeError:
  429. return type(self).__name__
  430. def __repr__(self):
  431. return '{.__class__.__name__}{!r}'.format(
  432. self, tuple(reversed((self.first, ) + self.funcs)))
  433. def __eq__(self, other):
  434. if isinstance(other, Compose):
  435. return other.first == self.first and other.funcs == self.funcs
  436. return NotImplemented
  437. def __ne__(self, other):
  438. equality = self.__eq__(other)
  439. return NotImplemented if equality is NotImplemented else not equality
  440. def __hash__(self):
  441. return hash(self.first) ^ hash(self.funcs)
  442. # Mimic the descriptor behavior of python functions.
  443. # i.e. let Compose be called as a method when bound to a class.
  444. # adapted from
  445. # docs.python.org/3/howto/descriptor.html#functions-and-methods
  446. def __get__(self, obj, objtype=None):
  447. return self if obj is None else MethodType(self, obj)
  448. # introspection with Signature is only possible from py3.3+
  449. @instanceproperty
  450. def __signature__(self):
  451. base = inspect.signature(self.first)
  452. last = inspect.signature(self.funcs[-1])
  453. return base.replace(return_annotation=last.return_annotation)
  454. __wrapped__ = instanceproperty(attrgetter('first'))
  455. def compose(*funcs):
  456. """ Compose functions to operate in series.
  457. Returns a function that applies other functions in sequence.
  458. Functions are applied from right to left so that
  459. ``compose(f, g, h)(x, y)`` is the same as ``f(g(h(x, y)))``.
  460. If no arguments are provided, the identity function (f(x) = x) is returned.
  461. >>> inc = lambda i: i + 1
  462. >>> compose(str, inc)(3)
  463. '4'
  464. See Also:
  465. compose_left
  466. pipe
  467. """
  468. if not funcs:
  469. return identity
  470. if len(funcs) == 1:
  471. return funcs[0]
  472. else:
  473. return Compose(funcs)
  474. def compose_left(*funcs):
  475. """ Compose functions to operate in series.
  476. Returns a function that applies other functions in sequence.
  477. Functions are applied from left to right so that
  478. ``compose_left(f, g, h)(x, y)`` is the same as ``h(g(f(x, y)))``.
  479. If no arguments are provided, the identity function (f(x) = x) is returned.
  480. >>> inc = lambda i: i + 1
  481. >>> compose_left(inc, str)(3)
  482. '4'
  483. See Also:
  484. compose
  485. pipe
  486. """
  487. return compose(*reversed(funcs))
  488. def pipe(data, *funcs):
  489. """ Pipe a value through a sequence of functions
  490. I.e. ``pipe(data, f, g, h)`` is equivalent to ``h(g(f(data)))``
  491. We think of the value as progressing through a pipe of several
  492. transformations, much like pipes in UNIX
  493. ``$ cat data | f | g | h``
  494. >>> double = lambda i: 2 * i
  495. >>> pipe(3, double, str)
  496. '6'
  497. See Also:
  498. compose
  499. compose_left
  500. thread_first
  501. thread_last
  502. """
  503. for func in funcs:
  504. data = func(data)
  505. return data
  506. def complement(func):
  507. """ Convert a predicate function to its logical complement.
  508. In other words, return a function that, for inputs that normally
  509. yield True, yields False, and vice-versa.
  510. >>> def iseven(n): return n % 2 == 0
  511. >>> isodd = complement(iseven)
  512. >>> iseven(2)
  513. True
  514. >>> isodd(2)
  515. False
  516. """
  517. return compose(not_, func)
  518. class juxt(object):
  519. """ Creates a function that calls several functions with the same arguments
  520. Takes several functions and returns a function that applies its arguments
  521. to each of those functions then returns a tuple of the results.
  522. Name comes from juxtaposition: the fact of two things being seen or placed
  523. close together with contrasting effect.
  524. >>> inc = lambda x: x + 1
  525. >>> double = lambda x: x * 2
  526. >>> juxt(inc, double)(10)
  527. (11, 20)
  528. >>> juxt([inc, double])(10)
  529. (11, 20)
  530. """
  531. __slots__ = ['funcs']
  532. def __init__(self, *funcs):
  533. if len(funcs) == 1 and not callable(funcs[0]):
  534. funcs = funcs[0]
  535. self.funcs = tuple(funcs)
  536. def __call__(self, *args, **kwargs):
  537. return tuple(func(*args, **kwargs) for func in self.funcs)
  538. def __getstate__(self):
  539. return self.funcs
  540. def __setstate__(self, state):
  541. self.funcs = state
  542. def do(func, x):
  543. """ Runs ``func`` on ``x``, returns ``x``
  544. Because the results of ``func`` are not returned, only the side
  545. effects of ``func`` are relevant.
  546. Logging functions can be made by composing ``do`` with a storage function
  547. like ``list.append`` or ``file.write``
  548. >>> from toolz import compose
  549. >>> from toolz.curried import do
  550. >>> log = []
  551. >>> inc = lambda x: x + 1
  552. >>> inc = compose(inc, do(log.append))
  553. >>> inc(1)
  554. 2
  555. >>> inc(11)
  556. 12
  557. >>> log
  558. [1, 11]
  559. """
  560. func(x)
  561. return x
  562. @curry
  563. def flip(func, a, b):
  564. """ Call the function call with the arguments flipped
  565. This function is curried.
  566. >>> def div(a, b):
  567. ... return a // b
  568. ...
  569. >>> flip(div, 2, 6)
  570. 3
  571. >>> div_by_two = flip(div, 2)
  572. >>> div_by_two(4)
  573. 2
  574. This is particularly useful for built in functions and functions defined
  575. in C extensions that accept positional only arguments. For example:
  576. isinstance, issubclass.
  577. >>> data = [1, 'a', 'b', 2, 1.5, object(), 3]
  578. >>> only_ints = list(filter(flip(isinstance, int), data))
  579. >>> only_ints
  580. [1, 2, 3]
  581. """
  582. return func(b, a)
  583. def return_none(exc):
  584. """ Returns None.
  585. """
  586. return None
  587. class excepts(object):
  588. """A wrapper around a function to catch exceptions and
  589. dispatch to a handler.
  590. This is like a functional try/except block, in the same way that
  591. ifexprs are functional if/else blocks.
  592. Examples
  593. --------
  594. >>> excepting = excepts(
  595. ... ValueError,
  596. ... lambda a: [1, 2].index(a),
  597. ... lambda _: -1,
  598. ... )
  599. >>> excepting(1)
  600. 0
  601. >>> excepting(3)
  602. -1
  603. Multiple exceptions and default except clause.
  604. >>> excepting = excepts((IndexError, KeyError), lambda a: a[0])
  605. >>> excepting([])
  606. >>> excepting([1])
  607. 1
  608. >>> excepting({})
  609. >>> excepting({0: 1})
  610. 1
  611. """
  612. def __init__(self, exc, func, handler=return_none):
  613. self.exc = exc
  614. self.func = func
  615. self.handler = handler
  616. def __call__(self, *args, **kwargs):
  617. try:
  618. return self.func(*args, **kwargs)
  619. except self.exc as e:
  620. return self.handler(e)
  621. @instanceproperty(classval=__doc__)
  622. def __doc__(self):
  623. from textwrap import dedent
  624. exc = self.exc
  625. try:
  626. if isinstance(exc, tuple):
  627. exc_name = '(%s)' % ', '.join(
  628. map(attrgetter('__name__'), exc),
  629. )
  630. else:
  631. exc_name = exc.__name__
  632. return dedent(
  633. """\
  634. A wrapper around {inst.func.__name__!r} that will except:
  635. {exc}
  636. and handle any exceptions with {inst.handler.__name__!r}.
  637. Docs for {inst.func.__name__!r}:
  638. {inst.func.__doc__}
  639. Docs for {inst.handler.__name__!r}:
  640. {inst.handler.__doc__}
  641. """
  642. ).format(
  643. inst=self,
  644. exc=exc_name,
  645. )
  646. except AttributeError:
  647. return type(self).__doc__
  648. @property
  649. def __name__(self):
  650. exc = self.exc
  651. try:
  652. if isinstance(exc, tuple):
  653. exc_name = '_or_'.join(map(attrgetter('__name__'), exc))
  654. else:
  655. exc_name = exc.__name__
  656. return '%s_excepting_%s' % (self.func.__name__, exc_name)
  657. except AttributeError:
  658. return 'excepting'
  659. def _check_sigspec(sigspec, func, builtin_func, *builtin_args):
  660. if sigspec is None:
  661. try:
  662. sigspec = inspect.signature(func)
  663. except (ValueError, TypeError) as e:
  664. sigspec = e
  665. if isinstance(sigspec, ValueError):
  666. return None, builtin_func(*builtin_args)
  667. elif not isinstance(sigspec, inspect.Signature):
  668. if (
  669. func in _sigs.signatures
  670. and ((
  671. hasattr(func, '__signature__')
  672. and hasattr(func.__signature__, '__get__')
  673. ))
  674. ):
  675. val = builtin_func(*builtin_args)
  676. return None, val
  677. return None, False
  678. return sigspec, None
  679. if PYPY: # pragma: no cover
  680. _check_sigspec_orig = _check_sigspec
  681. def _check_sigspec(sigspec, func, builtin_func, *builtin_args):
  682. # PyPy may lie, so use our registry for builtins instead
  683. if func in _sigs.signatures:
  684. val = builtin_func(*builtin_args)
  685. return None, val
  686. return _check_sigspec_orig(sigspec, func, builtin_func, *builtin_args)
  687. _check_sigspec.__doc__ = """ \
  688. Private function to aid in introspection compatibly across Python versions.
  689. If a callable doesn't have a signature (Python 3) or an argspec (Python 2),
  690. the signature registry in toolz._signatures is used.
  691. """
  692. def num_required_args(func, sigspec=None):
  693. sigspec, rv = _check_sigspec(sigspec, func, _sigs._num_required_args,
  694. func)
  695. if sigspec is None:
  696. return rv
  697. return sum(1 for p in sigspec.parameters.values()
  698. if p.default is p.empty
  699. and p.kind in (p.POSITIONAL_OR_KEYWORD, p.POSITIONAL_ONLY))
  700. def has_varargs(func, sigspec=None):
  701. sigspec, rv = _check_sigspec(sigspec, func, _sigs._has_varargs, func)
  702. if sigspec is None:
  703. return rv
  704. return any(p.kind == p.VAR_POSITIONAL
  705. for p in sigspec.parameters.values())
  706. def has_keywords(func, sigspec=None):
  707. sigspec, rv = _check_sigspec(sigspec, func, _sigs._has_keywords, func)
  708. if sigspec is None:
  709. return rv
  710. return any(p.default is not p.empty
  711. or p.kind in (p.KEYWORD_ONLY, p.VAR_KEYWORD)
  712. for p in sigspec.parameters.values())
  713. def is_valid_args(func, args, kwargs, sigspec=None):
  714. sigspec, rv = _check_sigspec(sigspec, func, _sigs._is_valid_args,
  715. func, args, kwargs)
  716. if sigspec is None:
  717. return rv
  718. try:
  719. sigspec.bind(*args, **kwargs)
  720. except TypeError:
  721. return False
  722. return True
  723. def is_partial_args(func, args, kwargs, sigspec=None):
  724. sigspec, rv = _check_sigspec(sigspec, func, _sigs._is_partial_args,
  725. func, args, kwargs)
  726. if sigspec is None:
  727. return rv
  728. try:
  729. sigspec.bind_partial(*args, **kwargs)
  730. except TypeError:
  731. return False
  732. return True
  733. def is_arity(n, func, sigspec=None):
  734. """ Does a function have only n positional arguments?
  735. This function relies on introspection and does not call the function.
  736. Returns None if validity can't be determined.
  737. >>> def f(x):
  738. ... return x
  739. >>> is_arity(1, f)
  740. True
  741. >>> def g(x, y=1):
  742. ... return x + y
  743. >>> is_arity(1, g)
  744. False
  745. """
  746. sigspec, rv = _check_sigspec(sigspec, func, _sigs._is_arity, n, func)
  747. if sigspec is None:
  748. return rv
  749. num = num_required_args(func, sigspec)
  750. if num is not None:
  751. num = num == n
  752. if not num:
  753. return False
  754. varargs = has_varargs(func, sigspec)
  755. if varargs:
  756. return False
  757. keywords = has_keywords(func, sigspec)
  758. if keywords:
  759. return False
  760. if num is None or varargs is None or keywords is None: # pragma: no cover
  761. return None
  762. return True
  763. num_required_args.__doc__ = """ \
  764. Number of required positional arguments
  765. This function relies on introspection and does not call the function.
  766. Returns None if validity can't be determined.
  767. >>> def f(x, y, z=3):
  768. ... return x + y + z
  769. >>> num_required_args(f)
  770. 2
  771. >>> def g(*args, **kwargs):
  772. ... pass
  773. >>> num_required_args(g)
  774. 0
  775. """
  776. has_varargs.__doc__ = """ \
  777. Does a function have variadic positional arguments?
  778. This function relies on introspection and does not call the function.
  779. Returns None if validity can't be determined.
  780. >>> def f(*args):
  781. ... return args
  782. >>> has_varargs(f)
  783. True
  784. >>> def g(**kwargs):
  785. ... return kwargs
  786. >>> has_varargs(g)
  787. False
  788. """
  789. has_keywords.__doc__ = """ \
  790. Does a function have keyword arguments?
  791. This function relies on introspection and does not call the function.
  792. Returns None if validity can't be determined.
  793. >>> def f(x, y=0):
  794. ... return x + y
  795. >>> has_keywords(f)
  796. True
  797. """
  798. is_valid_args.__doc__ = """ \
  799. Is ``func(*args, **kwargs)`` a valid function call?
  800. This function relies on introspection and does not call the function.
  801. Returns None if validity can't be determined.
  802. >>> def add(x, y):
  803. ... return x + y
  804. >>> is_valid_args(add, (1,), {})
  805. False
  806. >>> is_valid_args(add, (1, 2), {})
  807. True
  808. >>> is_valid_args(map, (), {})
  809. False
  810. **Implementation notes**
  811. Python 2 relies on ``inspect.getargspec``, which only works for
  812. user-defined functions. Python 3 uses ``inspect.signature``, which
  813. works for many more types of callables.
  814. Many builtins in the standard library are also supported.
  815. """
  816. is_partial_args.__doc__ = """ \
  817. Can partial(func, *args, **kwargs)(*args2, **kwargs2) be a valid call?
  818. Returns True *only* if the call is valid or if it is possible for the
  819. call to become valid by adding more positional or keyword arguments.
  820. This function relies on introspection and does not call the function.
  821. Returns None if validity can't be determined.
  822. >>> def add(x, y):
  823. ... return x + y
  824. >>> is_partial_args(add, (1,), {})
  825. True
  826. >>> is_partial_args(add, (1, 2), {})
  827. True
  828. >>> is_partial_args(add, (1, 2, 3), {})
  829. False
  830. >>> is_partial_args(map, (), {})
  831. True
  832. **Implementation notes**
  833. Python 2 relies on ``inspect.getargspec``, which only works for
  834. user-defined functions. Python 3 uses ``inspect.signature``, which
  835. works for many more types of callables.
  836. Many builtins in the standard library are also supported.
  837. """
  838. from . import _signatures as _sigs