Perform a substitution on a task Examples -------- >>> def inc(x): ... return x + 1 >>> subs((inc, 'x'), 'x', 1) # doctest: +ELLIPSIS ( , 1)
(task, key, val)
| 321 | |
| 322 | |
| 323 | def subs(task, key, val): |
| 324 | """Perform a substitution on a task |
| 325 | |
| 326 | Examples |
| 327 | -------- |
| 328 | >>> def inc(x): |
| 329 | ... return x + 1 |
| 330 | |
| 331 | >>> subs((inc, 'x'), 'x', 1) # doctest: +ELLIPSIS |
| 332 | (<function inc at ...>, 1) |
| 333 | """ |
| 334 | type_task = type(task) |
| 335 | if not (type_task is tuple and task and callable(task[0])): # istask(task): |
| 336 | try: |
| 337 | if type_task is type(key) and task == key: |
| 338 | return val |
| 339 | except Exception: |
| 340 | pass |
| 341 | if type_task is list: |
| 342 | return [subs(x, key, val) for x in task] |
| 343 | return task |
| 344 | newargs = [] |
| 345 | hash_key = {key} |
| 346 | for arg in task[1:]: |
| 347 | type_arg = type(arg) |
| 348 | if type_arg is tuple and arg and callable(arg[0]): # istask(task): |
| 349 | arg = subs(arg, key, val) |
| 350 | elif type_arg is list: |
| 351 | arg = [subs(x, key, val) for x in arg] |
| 352 | else: |
| 353 | try: |
| 354 | if arg in hash_key: # Hash and equality match |
| 355 | arg = val |
| 356 | except TypeError: # not hashable |
| 357 | pass |
| 358 | newargs.append(arg) |
| 359 | return task[:1] + tuple(newargs) |
| 360 | |
| 361 | |
| 362 | def _toposort(dsk, keys=None, returncycle=False, dependencies=None): |
no outgoing calls
searching dependent graphs…