get_data_items
函数源码
items: [AnnoItem] = get_data_items(
data: Annotations | [AnnoItem],
comment: str | None = None,
n: int | None = None,
)
按照精确匹配文字标注,以获取一个或多个标记数据项。
注意,该方法不会验证 所返回的标记数据项是否格式正确。即使数据项格式不妥当,依然会返回之。
别名
该函数可以按以下方式之一获取
import dash_picture_annotation as dpa
dpa.get_data_items
dpa.utilities.get_data_items
参数
输入
参数 | 类型 | 必选 | |
---|---|---|---|
data | Annotations | [AnnoItem] | 要检索的标记数据。 | |
comment | str | None | 用来选中标记数据项的文字标注。该值会用来精确匹配。亦即,这种匹配对空白字符、和大小写均敏感。 若此处使用 | |
n | int | None | 限制要检索到的数据的最大数目。若不提供此值,则会试图返回所有具备文字标注 若提供了一个非正值,则该值会被忽略掉。 |
输出
参数 | 类型 | |
---|---|---|
items | [AnnoItem] | 所定位到的标记数据项。任何针对这些数据项内容的修改,都是对data 的原处修改。 |
范例
按照文字标注检索一个标记数据项
- 代码
- 结果
get_anno_items_by_comment.py
import pprint
import dash_picture_annotation as dpa
mark = lambda pos: {"x": pos, "y": pos, "width": 0, "height": 0, "type": "RECT"}
data = [
{"id": "a", "mark": mark(0)},
{"id": "b", "mark": mark(1)},
{"id": "c", "mark": mark(2), "comment": "type-1"},
{"id": "d", "mark": mark(3), "comment": "type-2"},
{"id": "d", "mark": mark(4), "comment": "type-2"},
]
data_wrapped = {"timestamp": 0, "data": data}
pprint.pprint(dpa.get_data_items(data))
pprint.pprint(dpa.get_data_items(data_wrapped))
pprint.pprint(dpa.get_data_items(data, "type-1"))
pprint.pprint(dpa.get_data_items(data_wrapped, "type-1"))
pprint.pprint(dpa.get_data_items(data, "type-2"))
pprint.pprint(dpa.get_data_items(data_wrapped, "type-2"))
pprint.pprint(dpa.get_data_items(data, "type-2", 1))
pprint.pprint(dpa.get_data_items(data_wrapped, "type-2", 1))
try:
pprint.pprint(dpa.get_data_item(data, "type-3"))
except KeyError as e:
print(e)
({'id': 'a', 'mark': {'height': 0, 'type': 'RECT', 'width': 0, 'x': 0, 'y': 0}},
{'id': 'b', 'mark': {'height': 0, 'type': 'RECT', 'width': 0, 'x': 1, 'y': 1}})
({'id': 'a', 'mark': {'height': 0, 'type': 'RECT', 'width': 0, 'x': 0, 'y': 0}},
{'id': 'b', 'mark': {'height': 0, 'type': 'RECT', 'width': 0, 'x': 1, 'y': 1}})
({'comment': 'type-1',
'id': 'c',
'mark': {'height': 0, 'type': 'RECT', 'width': 0, 'x': 2, 'y': 2}},)
({'comment': 'type-1',
'id': 'c',
'mark': {'height': 0, 'type': 'RECT', 'width': 0, 'x': 2, 'y': 2}},)
({'comment': 'type-2',
'id': 'd',
'mark': {'height': 0, 'type': 'RECT', 'width': 0, 'x': 3, 'y': 3}},
{'comment': 'type-2',
'id': 'd',
'mark': {'height': 0, 'type': 'RECT', 'width': 0, 'x': 4, 'y': 4}})
({'comment': 'type-2',
'id': 'd',
'mark': {'height': 0, 'type': 'RECT', 'width': 0, 'x': 3, 'y': 3}},
{'comment': 'type-2',
'id': 'd',
'mark': {'height': 0, 'type': 'RECT', 'width': 0, 'x': 4, 'y': 4}})
({'comment': 'type-2',
'id': 'd',
'mark': {'height': 0, 'type': 'RECT', 'width': 0, 'x': 3, 'y': 3}},)
({'comment': 'type-2',
'id': 'd',
'mark': {'height': 0, 'type': 'RECT', 'width': 0, 'x': 3, 'y': 3}},)
'The ID "type-3" is not found in the data.'