concept of treepathmap¶
The basic task of treepathmap
is to create a map of nested collections
and support selection of items via the path (parts) or attached meta attributes.
nested_sample_data = {
"shelf": [
{"banana": {"color": "red", "weight": 123}},
{"banana": {"color": "blue", "weight": 113}},
],
"table": [
{"apple": {"color": "green", "weight": 80}},
{"banana": {"color": "green", "weight": 113}},
{"apple": {"color": "red", "weight": 81}},
]
}
sample_map = a_map_method(nested_sample_data)
# selection of items
bananas = sample_map.select("banana")
# read access to of attributes
banana_weights = bananas.select("weight")
total_weight_of_all_bananas = numpy.sum(banana_weights.to_list())
print("Total weight of bananas: {}g".format(total_weight_of_all_bananas))
# write access of attributes
bananas.tree_items["color"] = "yellow"
pretty_print(nested_sample_data)
Total weight of bananas 349g.
{
'shelf': [
{'banana': {'color': 'yellow', 'weight': 123}},
{'banana': {'color': 'yellow', 'weight': 113}},
],
'table': [
{'apple': {'color': 'green', 'weight': 80}},
{'banana': {'color': 'yellow', 'weight': 113}},
{'apple': {'color': 'red', 'weight': 81}},
]
}
The PathMapItem
points at a specific item within a collection. It attributes
are
-
PathMapItem.parent_container -> Collection
The parent collection of the item, the PathMapItem points to.
-
PathMapItem.real_key -> Hashable
The index of a Sequence or hashable key of a Mapping of the item this PathMapItem points to.
-
PathMapItem.prime_value : Any
This is the value of the item
-
PathMapItem.real_path : str
The sequence of indexes and keys pointing at the item’s location within the nested root collection as a path-like string.
-
PathMapItem.
meta_attributes
¶ Attributes associated with the item.