Flatten config while preserving specified nested structures
(config, no_flatten_keys=None)
| 22 | |
| 23 | |
| 24 | def flatten_config(config, no_flatten_keys=None): |
| 25 | """Flatten config while preserving specified nested structures""" |
| 26 | flattened = {} |
| 27 | no_flatten = set(no_flatten_keys or []) |
| 28 | |
| 29 | for key, value in config.items(): |
| 30 | if key in no_flatten: |
| 31 | flattened[key] = value |
| 32 | continue |
| 33 | |
| 34 | if isinstance(value, dict): |
| 35 | nested_flat = flatten_config(value, no_flatten_keys) |
| 36 | for nested_key, nested_value in nested_flat.items(): |
| 37 | if nested_key in flattened: |
| 38 | raise ValueError(f"Key conflict: '{nested_key}'") |
| 39 | flattened[nested_key] = nested_value |
| 40 | else: |
| 41 | if key in flattened: |
| 42 | raise ValueError(f"Key conflict: '{key}'") |
| 43 | flattened[key] = value |
| 44 | print("FLATTENED") |
| 45 | print(flattened) |
| 46 | return flattened |
| 47 | |
| 48 | |
| 49 | try: |
no outgoing calls
no test coverage detected