| 933 | |
| 934 | |
| 935 | class 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): |
no outgoing calls
searching dependent graphs…