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)
| 87 | |
| 88 | |
| 89 | def 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 | |
| 115 | def chunks(it, n): |
no outgoing calls