N-dim 数值列表类型
N-dim numerical list type
如何在 Python 3.7 中为 N-dimensional 数字列表(张量)定义类型?这将用作 Pydantic BaseModel
.
的道具之一
我想要这样的东西
from typing import List, Union
NumericalList = Union[
int, float,
List[int], List[float],
List[List[int]], List[List[float]],
...
]
n1: NumericalList = [0]
n3: NumericalList = [ [ [0, 1, 2], [1, 1, 2] ],
[ [1, 2, 3], [0, 1, 3] ],
]
我知道在 类 上可以编写字符串文字来指示 children 属于同一类型。 (或者只是添加 from __future__ import annotations
。)我想要一个 iterable/sliceable,而不是通过道具访问。
认为递归定义可能会起作用,但在 typing
模块的 deepcopy
的许多级别之后,AttributeError: __forward_arg__
失败了。
NumericalList = Union[int, float, List["NumericalList"]] # AttributeError: __forward_arg__
但是请注意,在使用 Pydantic 时失败,而不是在 Python 的 IDE 中。这是 Pydantic 特有的东西还是错误的方法?
虽然我希望 pydantic 支持本机递归类型,但您可以使用 pydantic custome root types models with pydantic strict types 来确保 float 值不会变成 int
from __future__ import annotations
from typing import Union, List
from pydantic import BaseModel, StrictInt, StrictFloat
class NumericalList(BaseModel):
__root__: Union[StrictInt, StrictFloat, List[NumericalList]]
NumericalList.update_forward_refs()
n1: NumericalList = NumericalList.parse_obj([0])
"""
NumericalList(__root__=[NumericalList(__root__=0)])
"""
n2: NumericalList = NumericalList.parse_obj(
[ [ [0, 1, 2], [1, 1, 2] ],
[ [1, 2, 3], [0, 1, 3] ],
]
)
"""
NumericalList(__root__=[
NumericalList(__root__=[
NumericalList(__root__=[NumericalList(__root__=0), NumericalList(__root__=1), NumericalList(__root__=2)]),
NumericalList(__root__=[NumericalList(__root__=1), NumericalList(__root__=1), NumericalList(__root__=2)])
]),
NumericalList(__root__=[
NumericalList(__root__=[NumericalList(__root__=1), NumericalList(__root__=2), NumericalList(__root__=3)]),
NumericalList(__root__=[NumericalList(__root__=0), NumericalList(__root__=1), NumericalList(__root__=3)])
])
])
"""
n1.dict()
"""
{"__root__": [0]}
"""
n2.dict()
"""
{"__root__": [
[ [0, 1, 2], [1, 1, 2] ],
[ [1, 2, 3], [0, 1, 3] ],
]}
"""
您可以扩展 class 以涵盖列表函数,但这是可选的
class NumericalList(BaseModel):
__root__: Union[StrictInt, StrictFloat, List[NumericalList]]
def __iter__(self):
return iter(self.__root__)
def __getitem__(self, index):
return self.__root__[index]
def __setitem__(self, index, value):
self.__root__[index] = value
NumericalList.update_forward_refs()
n3: NumericalList = NumericalList.parse_obj([0, 5, [1]])
for i in n3:
print(i)
"""
__root__=0
__root__=5
__root__=[NumericalList(__root__=1)]
"""
如果你想获得你可以做的原生类型
n3.dict()
"""
{'__root__': [0, 5, [1]]}
"""
如何在 Python 3.7 中为 N-dimensional 数字列表(张量)定义类型?这将用作 Pydantic BaseModel
.
我想要这样的东西
from typing import List, Union
NumericalList = Union[
int, float,
List[int], List[float],
List[List[int]], List[List[float]],
...
]
n1: NumericalList = [0]
n3: NumericalList = [ [ [0, 1, 2], [1, 1, 2] ],
[ [1, 2, 3], [0, 1, 3] ],
]
我知道在 类 上可以编写字符串文字来指示 children 属于同一类型。 (或者只是添加 from __future__ import annotations
。)我想要一个 iterable/sliceable,而不是通过道具访问。
认为递归定义可能会起作用,但在 typing
模块的 deepcopy
的许多级别之后,AttributeError: __forward_arg__
失败了。
NumericalList = Union[int, float, List["NumericalList"]] # AttributeError: __forward_arg__
但是请注意,在使用 Pydantic 时失败,而不是在 Python 的 IDE 中。这是 Pydantic 特有的东西还是错误的方法?
虽然我希望 pydantic 支持本机递归类型,但您可以使用 pydantic custome root types models with pydantic strict types 来确保 float 值不会变成 int
from __future__ import annotations
from typing import Union, List
from pydantic import BaseModel, StrictInt, StrictFloat
class NumericalList(BaseModel):
__root__: Union[StrictInt, StrictFloat, List[NumericalList]]
NumericalList.update_forward_refs()
n1: NumericalList = NumericalList.parse_obj([0])
"""
NumericalList(__root__=[NumericalList(__root__=0)])
"""
n2: NumericalList = NumericalList.parse_obj(
[ [ [0, 1, 2], [1, 1, 2] ],
[ [1, 2, 3], [0, 1, 3] ],
]
)
"""
NumericalList(__root__=[
NumericalList(__root__=[
NumericalList(__root__=[NumericalList(__root__=0), NumericalList(__root__=1), NumericalList(__root__=2)]),
NumericalList(__root__=[NumericalList(__root__=1), NumericalList(__root__=1), NumericalList(__root__=2)])
]),
NumericalList(__root__=[
NumericalList(__root__=[NumericalList(__root__=1), NumericalList(__root__=2), NumericalList(__root__=3)]),
NumericalList(__root__=[NumericalList(__root__=0), NumericalList(__root__=1), NumericalList(__root__=3)])
])
])
"""
n1.dict()
"""
{"__root__": [0]}
"""
n2.dict()
"""
{"__root__": [
[ [0, 1, 2], [1, 1, 2] ],
[ [1, 2, 3], [0, 1, 3] ],
]}
"""
您可以扩展 class 以涵盖列表函数,但这是可选的
class NumericalList(BaseModel):
__root__: Union[StrictInt, StrictFloat, List[NumericalList]]
def __iter__(self):
return iter(self.__root__)
def __getitem__(self, index):
return self.__root__[index]
def __setitem__(self, index, value):
self.__root__[index] = value
NumericalList.update_forward_refs()
n3: NumericalList = NumericalList.parse_obj([0, 5, [1]])
for i in n3:
print(i)
"""
__root__=0
__root__=5
__root__=[NumericalList(__root__=1)]
"""
如果你想获得你可以做的原生类型
n3.dict()
"""
{'__root__': [0, 5, [1]]}
"""