from gaffer import DictPath data = { 'items': [ {'name': 'apple', 'price': 0.49}, {'name': 'banana', 'price': 0.29}, {'name': 'orange', 'price': 0.59} ] } dp = DictPath('items.1.name') name = dp.get(data) print(name) # prints 'banana'
from gaffer import DictPath data = {'name': 'Alice', 'address': {'city': 'New York', 'state': 'NY'}} dp = DictPath('address.city') dp.set(data, 'Chicago') print(data) # prints {'name': 'Alice', 'address': {'city': 'Chicago', 'state': 'NY'}}In this case, the DictPath object `dp` specifies the path `address.city`, which tells it to navigate to the dictionary at `'address'` and set the value associated with the key `'city'` to `'Chicago'`. The `set()` method is then called on `dp` with the `data` dictionary as its first argument and the new value as its second argument, which modifies the dictionary in place. The package library used in these examples is `gaffer`.