dictutils.ops.deep_setΒΆ
- dictutils.ops.deep_set(obj, path, value, *, create_missing=True, create_mapping=<class 'dict'>)[source]ΒΆ
Set value in nested object using dotted path notation.
- Parameters:
obj (
Any) β The object to modifypath (
Union[str,Sequence[Union[str,int]]]) β Path as string (βa.b.cβ) or sequence [βaβ, βbβ, βcβ]value (
Any) β Value to set at the pathcreate_missing (
bool) β Whether to create missing intermediate pathscreate_mapping (
Callable[[],Any]) β Factory function for creating new mappings
- Return type:
- Returns:
The modified object
Example
>>> import json >>> data = {} >>> deep_set(data, "user.profile.name", "Alice") >>> print(json.dumps(data, indent=4)) { "user": { "profile": { "name": "Alice" } } }
>>> deep_set(data, "user.profile.tags[0]", "admin") >>> print(json.dumps(data, indent=4)) { "user": { "profile": { "name": "Alice", "tags": [ "admin" ] } } }