Set a value in a dict. If `key` contains a '.', it is assumed be a path (i.e. dot-delimited string) to the value's location. :: >>> d = {} >>> set_value(d, 'foo.bar', 42) >>> d {'foo': {'bar': 42}}
(dct: dict[str, typing.Any], key: str, value: typing.Any)
| 129 | |
| 130 | |
| 131 | def set_value(dct: dict[str, typing.Any], key: str, value: typing.Any): |
| 132 | """Set a value in a dict. If `key` contains a '.', it is assumed |
| 133 | be a path (i.e. dot-delimited string) to the value's location. |
| 134 | |
| 135 | :: |
| 136 | |
| 137 | >>> d = {} |
| 138 | >>> set_value(d, 'foo.bar', 42) |
| 139 | >>> d |
| 140 | {'foo': {'bar': 42}} |
| 141 | """ |
| 142 | if "." in key: |
| 143 | head, rest = key.split(".", 1) |
| 144 | target = dct.setdefault(head, {}) |
| 145 | if not isinstance(target, dict): |
| 146 | raise ValueError( |
| 147 | f"Cannot set {key} in {head} due to existing value: {target}" |
| 148 | ) |
| 149 | set_value(target, rest, value) |
| 150 | else: |
| 151 | dct[key] = value |
| 152 | |
| 153 | |
| 154 | def callable_or_raise(obj): |
no outgoing calls
no test coverage detected
searching dependent graphs…