MCPcopy
hub / github.com/marshmallow-code/marshmallow / set_value

Function set_value

src/marshmallow/utils.py:131–151  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

129
130
131def 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
154def callable_or_raise(obj):

Callers 1

_deserializeMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…