set_item_of_object
FunctionPrivateSource
set_item_of_object(data: Any, index: Any, value: Any)
Run data[index] = value
supposing that data
is abitrary and index
can be a
one-value sequence.
Raise an ValueError
if the item of data
cannot be set.
Raise an IndexError
if the item of data
is a sequence, while value
cannot
be broadcast to this item.
warning
Note that index
is not a route
. If it is a sequence, it should only
contain one element.
Arguments
Requires
Argument | Type | Required | |
---|---|---|---|
data | Any | The whole data to be indexed. | |
index | Any | The index to be used for locating the part to be modified. It can be a number, or keyword, or a one-value sequence like [0] . | |
value | Any | The new value used for updating the member of data located by index . |
Examples
Set a part of the whole data
- Codes
- Results
set_a_part_of_data.py
import pprint
from dash_json_grid.mixins import set_item_of_object
data = {
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 1111.55,
"batters": {
"batter": [
{"id": "1001", "type": "Regular"},
{"id": "1002", "type": "Chocolate"},
{"id": "1003", "type": "Blueberry"},
{"id": "1004", "type": "Devil's Food"},
]
},
"topping": [
{"id": "5001", "type": "None"},
{"id": "5002", "type": "Glazed"},
{"id": "5005", "type": "Sugar"},
{"id": "5007", "type": "Powdered Sugar"},
{"id": "5006", "type": "Chocolate with Sprinkles"},
{"id": "5003", "type": "Chocolate"},
{"id": "5004", "type": "Maple"},
],
}
route = "batters"
set_item_of_object(data, route, 0)
pprint.pprint(data)
{'batters': 0,
'id': '0001',
'name': 'Cake',
'ppu': 1111.55,
'topping': [{'id': '5001', 'type': 'None'},
{'id': '5002', 'type': 'Glazed'},
{'id': '5005', 'type': 'Sugar'},
{'id': '5007', 'type': 'Powdered Sugar'},
{'id': '5006', 'type': 'Chocolate with Sprinkles'},
{'id': '5003', 'type': 'Chocolate'},
{'id': '5004', 'type': 'Maple'}],
'type': 'donut'}