MCPcopy
hub / github.com/dask/dask / Dict

Class Dict

dask/_task_spec.py:935–1003  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

933
934
935class Dict(NestedContainer, Mapping):
936 klass = dict
937
938 def __init__(self, /, *args: Any, **kwargs: Any):
939 if args:
940 assert not kwargs
941 if len(args) == 1:
942 args = args[0]
943 if isinstance(args, dict): # type: ignore[unreachable]
944 args = tuple(itertools.chain(*args.items())) # type: ignore[unreachable]
945 elif isinstance(args, (list, tuple)):
946 if all(
947 len(el) == 2 if isinstance(el, (list, tuple)) else False
948 for el in args
949 ):
950 args = tuple(itertools.chain(*args))
951 else:
952 raise ValueError("Invalid argument provided")
953
954 if len(args) % 2 != 0:
955 raise ValueError("Invalid number of arguments provided")
956
957 elif kwargs:
958 assert not args
959 args = tuple(itertools.chain(*kwargs.items()))
960
961 super().__init__(*args)
962
963 def __repr__(self):
964 values = ", ".join(f"{k}: {v}" for k, v in batched(self.args, 2, strict=True))
965 return f"Dict({values})"
966
967 def substitute(
968 self, subs: dict[KeyType, KeyType | GraphNode], key: KeyType | None = None
969 ) -> Dict:
970 subs_filtered = {
971 # subs can be as large as the whole graph; do not iterate over it!
972 k: v
973 for k in (self.dependencies & subs.keys())
974 if (v := subs[k]) != k
975 }
976 if not subs_filtered:
977 return self
978
979 new_args = []
980 for arg in self.args:
981 new_arg = (
982 arg.substitute(subs_filtered)
983 if isinstance(arg, (GraphNode, TaskRef))
984 else arg
985 )
986 new_args.append(new_arg)
987 return type(self)(new_args)
988
989 def __iter__(self):
990 yield from self.args[::2]
991
992 def __len__(self):

Callers 9

unpack_collectionsFunction · 0.90
_unpackFunction · 0.90
map_partitionsFunction · 0.90
test_pickleFunction · 0.90
test_dict_classFunction · 0.90
test_substitute_nestedFunction · 0.90
parse_inputFunction · 0.85
_wrap_namedtuple_taskFunction · 0.85
convert_legacy_taskFunction · 0.85

Calls

no outgoing calls

Tested by 3

test_pickleFunction · 0.72
test_dict_classFunction · 0.72
test_substitute_nestedFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…