Skip to main content
Version: 0.2.1

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​

ArgumentTypeRequired
Description
dataAnnotations | [AnnoItem]The annotation data that is queried.
comment_regexstr | Pattern | None

The regular expression used for matching the comment of the annoatations. All comments matching the regex can be returned.

If using None, will return the annotations without comments.

nint | None

Limit the maximal number of the items to be found. If not provided, will try to return all annotation items the comment matched.

Specifying a non-positive value will cause this number to be ignored.

Returns​

ArgumentType
Description
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​

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,}"))