is_sequence_of
FunctionSource
flag: TypeGuard[[T]] = is_sequence_of(
data: Any,
validator: type[T] | (Any) => TypeGuard[T],
)
Check whether data
is Sequence[T]
, where T
is specified by validator
.
Aliases
This function can be acquired by
import dash_picture_annotation as dpa
dpa.is_sequence_of
dpa.typehints.is_sequence_of
Arguments
Requires
Argument | Type | Required | |
---|---|---|---|
data | Any | The value to be verified. | |
validator | type[T] | (Any) => TypeGuard[T] | The validator. It can be a type or a function validating whether a value is of a specific type. |
Returns
Argument | Type | |
---|---|---|
flag | bool | If this value is True , data is a sequence of the instance of T determined by validator , vice versa. |
Examples
Check whether an object is a list of int
- Codes
- Results
check_is_sequence_of_int.py
import dash_picture_annotation as dpa
print(dpa.is_sequence_of([], int))
print(dpa.is_sequence_of((1,), int))
print(dpa.is_sequence_of([1, 2, 3], int))
print(dpa.is_sequence_of([1, 2, 3, 4.0], int))
True
True
True
False
Check whether an object is a list of a customized type
- Codes
- Results
check_is_sequence_of_hascopy.py
from typing import Any
from typing_extensions import TypeGuard, Protocol, Self
import dash_picture_annotation as dpa
class HasCopy(Protocol):
def copy(self: Self) -> Self: ...
def is_has_copy(val: Any) -> TypeGuard[HasCopy]:
return callable(getattr(val, "copy", None))
print(dpa.is_sequence_of([[], [], []], is_has_copy))
print(dpa.is_sequence_of([tuple(), [], []], is_has_copy))
print(dpa.is_sequence_of([dict(),], is_has_copy))
print(dpa.is_sequence_of(["", "", ""], is_has_copy))
print(dpa.is_sequence_of([[], dict()], is_has_copy))
print(dpa.is_sequence_of([[], dict(), tuple()], is_has_copy))
True
False
True
False
True
False
Check whether an object is a list of annotation items
- Codes
- Results
check_is_sequence_of_annoitem.py
import dash_picture_annotation as dpa
example_empty = []
example_one_item = dpa.AnnoItem(
id="test", mark=dpa.AnnoMark(x=0, y=0, width=0, height=0, type="RECT")
)
example_item_single_tuple = (example_one_item,)
example_items = [example_one_item, example_one_item, example_one_item]
example_items_bad = [example_one_item, dict(id="test", mark={"x": 1})]
print(dpa.is_sequence_of(example_empty, dpa.is_anno_item))
print(dpa.is_sequence_of(example_one_item, dpa.is_anno_item))
print(dpa.is_sequence_of(example_item_single_tuple, dpa.is_anno_item))
print(dpa.is_sequence_of(example_items, dpa.is_anno_item))
print(dpa.is_sequence_of(example_items_bad, dpa.is_anno_item))
True
False
True
True
False