我想为数据类创建字典,该字典包含作为属性的数据类列表
以下是我要实现的目标的一个示例:
from typing import List
from dataclasses import dataclass
@dataclass
class level2:
key21: int
key22: int
@nested_dataclass
class level1:
key1: int
key2: List[level2]
data = {
'key1': value1,
'key2': [{
'key21': value21,
'key22': value22,
}]
}
my_object = level1(**data)
print(my_object.key2[0].key21) #should print value21
我发现的最近的装饰器是这个装饰器,但是它不适用于数据类列表: Creating nested dataclass objects in Python
def is_dataclass(obj):
"""Returns True if obj is a dataclass or an instance of a
dataclass."""
_FIELDS = '__dataclass_fields__'
return hasattr(obj, _FIELDS)
def nested_dataclass(*args, **kwargs):
def wrapper(cls):
cls = dataclass(cls, **kwargs)
original_init = cls.__init__
def __init__(self, *args, **kwargs):
for name, value in kwargs.items():
field_type = cls.__annotations__.get(name, None)
if is_dataclass(field_type) and isinstance(value, dict):
new_obj = field_type(**value)
kwargs[name] = new_obj
original_init(self, *args, **kwargs)
cls.__init__ = __init__
return cls
return wrapper(args[0]) if args else wrapper
您将如何修改此装饰器或创建可以完成此装饰的装饰器? (我在建筑装饰方面的经验为零)
非常感谢任何注释/代码。谢谢
答案 0 :(得分:1)
pip install validated-dc
ValidatedDC:https://github.com/EvgeniyBurdin/validated_dc
from dataclasses import dataclass
from typing import List, Union
from validated_dc import ValidatedDC
@dataclass
class Level2(ValidatedDC):
key21: int
key22: int
@dataclass
class Level1(ValidatedDC):
key1: int
key2: List[Level2]
data = {
'key1': 1,
'key2': [{
'key21': 21,
'key22': 22,
}]
}
my_object = Level1(**data)
assert my_object.key2[0].key21 == 21
# ----------------------------------
@dataclass
class Level1(ValidatedDC):
key1: int
key2: Union[Level2, List[Level2]]
my_object = Level1(**data) # key2 - list
assert my_object.key2[0].key21 == 21
data['key2'] = {
'key21': 21,
'key22': 22,
}
my_object = Level1(**data) # key2 - dict
assert my_object.key2.key21 == 21
答案 1 :(得分:0)
这没有提供如何更改装饰器的信息,如果您不希望使用任何第三方包装,请忽略此答案。但我认为pydantic
可以为您做点什么想。我建议的唯一原因是因为它不允许您在将key2
声明为列表时错误地将其作为字典。
from typing import List
from pydantic import BaseModel
class level2(BaseModel):
key21: int
key22: int
class level1(BaseModel):
key1: int
key2: List[level2]
data = {
'key1': 1,
'key2': [{
'key21': 21,
'key22': 22,
}]
}
my_object = level1(**data)
print(my_object.key2[0].key21) # prints 21
如果您实际上想直接从key21
访问key2
,那么
class level1(BaseModel):
key1: int
key2: level2 # Not a list
data = {
'key1': 1,
'key2': {
'key21': 21,
'key22': 22,
}
}
my_object = level1(**data)
print(my_object.key2.key21) # prints 21
如果您的目标是成功使装饰器工作,请再次忽略此操作。否则,安装pydantic不会造成伤害:)
答案 2 :(得分:0)
好的,因此我对装饰器进行了一些更改,但是它非常特定于此处提供的示例。主要问题是您的List[level2]
字段不是dataclass
。因此,为了解决这个问题,我玩了一会儿,发现有一个 args 属性可以告诉您列表中的嵌套类型。我以前从未使用过数据类(除了pydantic以外),所以也许在那里会有更好的答案
def nested_dataclass(*args, **kwargs):
def wrapper(cls):
cls = dataclass(cls, **kwargs)
original_init = cls.__init__
def __init__(self, *args, **kwargs):
for name, value in kwargs.items():
field_type = cls.__annotations__.get(name, None)
if hasattr(field_type, '__args__'):
inner_type = field_type.__args__[0]
if is_dataclass(inner_type):
new_obj = [inner_type(**dict_) for dict_ in value]
kwargs[name] = new_obj
original_init(self, *args, **kwargs)
cls.__init__ = __init__
return cls
return wrapper(args[0]) if args else wrapper
@dataclass
class level2:
key21: int
key22: int
@nested_dataclass
class level1:
key1: int
key2: List[level2]
data = {
'key1': 1,
'key2': [{
'key21': 21,
'key22': 22,
},
{
'key21': 23,
'key22': 24
}]
}
my_object = level1(**data)
print(my_object.key2[0].key21) #should print 21
print(my_object.key2[1].key21) #should print 23
@nested_dataclass
class random:
key1: int
key2: List[int]
random_object = random(**{'key1': 1, 'key2': [1,2,3]})
print(random_object.key2) # prints [1,2,3]
进一步嵌套
@nested_dataclass
class level3:
key3: List[level1]
level3(**{'key3': [data]})
输出:
level3(key3=[level1(key1=1, key2=[level2(key21=21, key22=22), level2(key21=23, key22=24)])])