跳到主要内容

is_sequence_of

函数源码

flag: TypeGuard[[T]] = is_sequence_of(
data: Any,
validator: type[T] | (Any) => TypeGuard[T],
)

检查数据data是否是Sequence[T],其中T由验证器确认。

别名

该函数可以按以下方式之一获取

import dash_picture_annotation as dpa


dpa.is_sequence_of
dpa.typehints.is_sequence_of

参数

输入

参数类型必选
说明
dataAny要验证的值。
validatortype[T] | (Any) => TypeGuard[T]验证器。其可以是一个数据类型,或者一个用来检查某值是否是某类型的函数。

输出

参数类型
说明
flagbool若该值为True,表示data是由validator所确认的类型T的实例构成的序列,反之亦然。

范例

检查某对象是否是int的序列

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))

检查某对象是否是某定制类型的序列

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))

检查某对象是否是标记数据项的序列

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))