MCPcopy Create free account
hub / github.com/celery/celery / firstmethod

Function firstmethod

celery/utils/functional.py:89–112  ·  view source on GitHub ↗

Multiple dispatch. Return a function that with a list of instances, finds the first instance that gives a value for the given method. The list can also contain lazy instances (:class:`~kombu.utils.functional.lazy`.)

(method, on_call=None)

Source from the content-addressed store, hash-verified

87
88
89def firstmethod(method, on_call=None):
90 """Multiple dispatch.
91
92 Return a function that with a list of instances,
93 finds the first instance that gives a value for the given method.
94
95 The list can also contain lazy instances
96 (:class:`~kombu.utils.functional.lazy`.)
97 """
98
99 def _matcher(it, *args, **kwargs):
100 for obj in it:
101 obj = maybe_evaluate(obj)
102 try:
103 meth = getattr(obj, method)
104 except AttributeError:
105 continue
106
107 reply = (on_call(meth, *args, **kwargs) if on_call
108 else meth(*args, **kwargs))
109 if reply is not None:
110 return reply
111
112 return _matcher
113
114
115def chunks(it, n):

Callers 4

test_AttributeErrorMethod · 0.90
test_handles_lazyMethod · 0.90
annotations.pyFile · 0.90

Calls

no outgoing calls

Tested by 3

test_AttributeErrorMethod · 0.72
test_handles_lazyMethod · 0.72