Given an object that can be a nested structure of dict-like or array-like objects. What is the pythonic way to set a value on that object given a dotted path as a string?
Example:
obj = [
{'a': [1, 2]},
{'b': {
'c': [3, 4],
}},
]
path = '1.b.c.0'
# operation that sets a value at the given path, e.g.
obj[path] = 5
# or
set_value(obj, path, 5)
The above call/assignment should replace the 3
in the example with a 5
.
Note: The path can contain list indices as well as keys. Every level of the object can be an array
or a dict
or something that behaves like that.
The solution should be roughly like what the npm package object-path
does in javascript.