Skip to main content
Version: 0.4.3

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

ArgumentTypeRequired
Description
dataAnyThe whole data to be indexed.
indexAnyThe 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].
valueAnyThe new value used for updating the member of data located by index.

Examples

Set a part of the whole data

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)