Count the number of tasks in the given signature recursively. Descend into the signature object and return the amount of tasks it contains.
(cls, sig_obj)
| 2168 | |
| 2169 | @classmethod |
| 2170 | def _descend(cls, sig_obj): |
| 2171 | """Count the number of tasks in the given signature recursively. |
| 2172 | |
| 2173 | Descend into the signature object and return the amount of tasks it contains. |
| 2174 | """ |
| 2175 | # Sometimes serialized signatures might make their way here |
| 2176 | if not isinstance(sig_obj, Signature) and isinstance(sig_obj, dict): |
| 2177 | sig_obj = Signature.from_dict(sig_obj) |
| 2178 | if isinstance(sig_obj, group): |
| 2179 | # Each task in a group counts toward this chord |
| 2180 | subtasks = getattr(sig_obj.tasks, "tasks", sig_obj.tasks) |
| 2181 | return sum(cls._descend(task) for task in subtasks) |
| 2182 | elif isinstance(sig_obj, _chain): |
| 2183 | # The last non-empty element in a chain counts toward this chord |
| 2184 | for child_sig in sig_obj.tasks[-1::-1]: |
| 2185 | child_size = cls._descend(child_sig) |
| 2186 | if child_size > 0: |
| 2187 | return child_size |
| 2188 | # We have to just hope this chain is part of some encapsulating |
| 2189 | # signature which is valid and can fire the chord body |
| 2190 | return 0 |
| 2191 | elif isinstance(sig_obj, chord): |
| 2192 | # The child chord's body counts toward this chord |
| 2193 | return cls._descend(sig_obj.body) |
| 2194 | elif isinstance(sig_obj, Signature): |
| 2195 | # Each simple signature counts as 1 completion for this chord |
| 2196 | return 1 |
| 2197 | # Any other types are assumed to be iterables of simple signatures |
| 2198 | return len(sig_obj) |
| 2199 | |
| 2200 | def __length_hint__(self): |
| 2201 | """Return the number of tasks in this chord's header (recursively).""" |
no test coverage detected