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]])
| 742 | |
| 743 | |
| 744 | def 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 | |
| 769 | class CheckKeyDuplicatesYamlLoader(SafeLoader): |