MCPcopy Create free account
hub / github.com/dask/dask / _substitute

Method _substitute

dask/_expr.py:622–673  ·  view source on GitHub ↗
(self, old, new, _seen)

Source from the content-addressed store, hash-verified

620 return self._substitute(old, new, _seen=set())
621
622 def _substitute(self, old, new, _seen):
623 if self._name in _seen:
624 return self
625 # Check if we are replacing a literal
626 if isinstance(old, Expr):
627 substitute_literal = False
628 if self._name == old._name:
629 return new
630 else:
631 substitute_literal = True
632 if isinstance(old, bool):
633 raise TypeError("Arguments to `substitute` cannot be bool.")
634
635 new_exprs = []
636 update = False
637 for operand in self.operands:
638 if isinstance(operand, Expr):
639 val = operand._substitute(old, new, _seen)
640 if operand._name != val._name:
641 update = True
642 new_exprs.append(val)
643 elif (
644 "Fused" in type(self).__name__
645 and isinstance(operand, list)
646 and all(isinstance(op, Expr) for op in operand)
647 ):
648 # Special handling for `Fused`.
649 # We make no promise to dive through a
650 # list operand in general, but NEED to
651 # do so for the `Fused.exprs` operand.
652 val = []
653 for op in operand:
654 val.append(op._substitute(old, new, _seen))
655 if val[-1]._name != op._name:
656 update = True
657 new_exprs.append(val)
658 elif (
659 substitute_literal
660 and not isinstance(operand, bool)
661 and isinstance(operand, type(old))
662 and operand == old
663 ):
664 new_exprs.append(new)
665 update = True
666 else:
667 new_exprs.append(operand)
668
669 if update: # Only recreate if something changed
670 return type(self)(*new_exprs)
671 else:
672 _seen.add(self._name)
673 return self
674
675 def substitute_parameters(self, substitutions: dict) -> Expr:
676 """Substitute specific `Expr` parameters

Callers 1

substituteMethod · 0.95

Calls 2

allFunction · 0.85
addMethod · 0.45

Tested by

no test coverage detected