get_data_items_by_regex
FunctionSource
items: [AnnoItem] = get_data_items_by_regex(
    data: Annotations | [AnnoItem],
    comment_regex: str | Pattern | None = None,
    n: int | None = None,
)
Get one or several annotation items by matching the comment with a regular expression.
The regex is done by re.match.
Note that this method will not validate the format of each returned annotation items. Not well-formatted items will STILL be returned.
Aliases
This function can be acquired by
import dash_picture_annotation as dpa
dpa.get_data_items_by_regex
dpa.utilities.get_data_items_by_regex
Arguments
Requires
| Argument | Type | Required | |
|---|---|---|---|
data | Annotations | [AnnoItem] | The annotation data that is queried. | |
comment_regex | str | Pattern | None | The regular expression used for matching the comment of the annoatations. All comments matching the regex can be returned. If using   | |
n | int | None | Limit the maximal number of the items to be found. If not provided, will try to return all annotation items the  Specifying a non-positive value will cause this number to be ignored.  | 
Returns
| Argument | Type | |
|---|---|---|
items | [AnnoItem] | The located annotation items. Modifying the content of these items will cause the data to be modified. | 
Examples
Get an annotation item by quering the comment with an regular expression
- Codes
 - Results
 
get_anno_item_by_comment_with_regex.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_by_regex(data))
pprint.pprint(dpa.get_data_items_by_regex(data_wrapped))
pprint.pprint(dpa.get_data_items_by_regex(data, r"type-\d"))
pprint.pprint(dpa.get_data_items_by_regex(data_wrapped, r"type-\d"))
pprint.pprint(dpa.get_data_items_by_regex(data, r"type-\d", 1))
pprint.pprint(dpa.get_data_items_by_regex(data_wrapped, r"type-\d", 1))
pprint.pprint(dpa.get_data_items_by_regex(data, r"type-\d{2,}"))
({'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-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-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-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}},)
()