MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / check_key_duplicates

Function check_key_duplicates

monai/utils/misc.py:744–766  ·  view source on GitHub ↗

Checks if there is a duplicated key in the sequence of `ordered_pairs`. If there is - it will log a warning or raise ValueError (if configured by environmental var `MONAI_FAIL_ON_DUPLICATE_CONFIG==1`) Otherwise, it returns the dict made from this sequence. Satisfies a format f

(ordered_pairs: Sequence[tuple[Any, Any]])

Source from the content-addressed store, hash-verified

742
743
744def check_key_duplicates(ordered_pairs: Sequence[tuple[Any, Any]]) -> dict[Any, Any]:
745 """
746 Checks if there is a duplicated key in the sequence of `ordered_pairs`.
747 If there is - it will log a warning or raise ValueError
748 (if configured by environmental var `MONAI_FAIL_ON_DUPLICATE_CONFIG==1`)
749
750 Otherwise, it returns the dict made from this sequence.
751
752 Satisfies a format for an `object_pairs_hook` in `json.load`
753
754 Args:
755 ordered_pairs: sequence of (key, value)
756 """
757 keys = set()
758 for k, _ in ordered_pairs:
759 if k in keys:
760 if os.environ.get("MONAI_FAIL_ON_DUPLICATE_CONFIG", "0") == "1":
761 raise ValueError(f"Duplicate key: `{k}`")
762 else:
763 warnings.warn(f"Duplicate key: `{k}`")
764 else:
765 keys.add(k)
766 return dict(ordered_pairs)
767
768
769class CheckKeyDuplicatesYamlLoader(SafeLoader):

Callers

nothing calls this directly

Calls 2

getMethod · 0.80
addMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…