嵌套字典到具有默认值的对象

时间:2020-10-28 01:50:18

标签: python python-3.7 python-dataclasses

我正在使用dataclasses + dataclasses_json来实现我的Web api。

我的应用程序将解码从dict到对象的请求,我希望在不填充每个字段的情况下仍然可以生成对象,并使用默认值填充空文件。

这是我可能的代码:

from dataclasses import dataclass
from dataclasses_json import dataclass_json

@dataclass
class Glasses:
    color: str
    size: str
    prize: int

@dataclass
class Tshirt:
    color: str
    size: str
    prize: int

@dataclass_json
@dataclass
class People:
    Name : str = 'James'
    Age : int = 25
    Glasses: Glasses = Glasses('black', 'M', 2500)
    Tshirt: Tshirt = Tshirt('black', 'XL', 500)

p = {
    'Name': 'Gino',
    'Age': 20,
    'Glasses': {
        'color' : 'red',
        'size' : 'M',
        'prize' : 4000
    },
    'Tshirt': {
        'color' : 'red',
        'size' : 'M',
        'prize' : 4000
    }
}

a = People.from_dict(p)
print(a)

我会收到错误消息

Traceback (most recent call last):
  File ".\People.py", line 39, in <module>
    a = People.from_dict(p)
  File "C:\Python3.7\lib\site-packages\dataclasses_json\api.py", line 83, in from_dict
    return _decode_dataclass(cls, kvs, infer_missing)
  File "C:\Python3.7\lib\site-packages\dataclasses_json\core.py", line 133, in _decode_dataclass
    overrides = _user_overrides_or_exts(cls)
  File "C:\Python3.7\lib\site-packages\dataclasses_json\core.py", line 59, in _user_overrides_or_exts
    if field.type in encoders:
TypeError: unhashable type: 'Glasses'

我发现我可以添加(eq = True,frozen = True) 所以我将代码更改为

from dataclasses import dataclass
from dataclasses_json import dataclass_json

@dataclass(eq=True, frozen=True)
class Glasses:
    color: str
    size: str
    prize: int

@dataclass(eq=True, frozen=True)
class Tshirt:
    color: str
    size: str
    prize: int

@dataclass_json
@dataclass
class People:
    Name : str = 'James'
    Age : int = 25
    Glasses: Glasses = Glasses('black', 'M', 2500)
    Tshirt: Tshirt = Tshirt('black', 'XL', 500)

p = {
    'Name': 'Gino',
    'Age': 20,
    'Glasses': {
        'color' : 'red',
        'size' : 'M',
        'prize' : 4000
    },
    'Tshirt': {
        'color' : 'red',
        'size' : 'M',
        'prize' : 4000
    }
}

a = People.from_dict(p)
print(a)

我收到另一个错误

  File ".\People.py", line 39, in <module>
    a = People.from_dict(p)
  File "C:\Python3.7\lib\site-packages\dataclasses_json\api.py", line 83, in from_dict
    return _decode_dataclass(cls, kvs, infer_missing)
  File "C:\Python3.7\lib\site-packages\dataclasses_json\core.py", line 198, in _decode_dataclass
    infer_missing)
  File "C:\Python3.7\lib\site-packages\dataclasses_json\core.py", line 131, in _decode_dataclass
    if isinstance(kvs, cls):
TypeError: isinstance() arg 2 must be a type or tuple of types

是否有任何可能的方法可以使用内置函数来满足我的要求而无需自己编写?

1 个答案:

答案 0 :(得分:1)

您有字段名称和类型名称冲突。为了使代码正常工作,您必须具有与类名称不同的JSON字段名称,即您不能具有字段"Glasses"和类Glasses,您必须将类重命名为Glasses_或重命名您的字段名称。

另一种选择是为类类型创建别名,即创建别名GlassesTGlasses

具有类型别名的代码版本:

Try it online!

from dataclasses import dataclass
from dataclasses_json import dataclass_json

@dataclass
class Glasses:
    color: str
    size: str
    prize: int

GlassesT = Glasses

@dataclass
class Tshirt:
    color: str
    size: str
    prize: int
    
TshirtT = Tshirt
    
@dataclass_json
@dataclass
class People:
    Name : str = 'James'
    Age : int = 25
    Glasses: GlassesT = GlassesT('black', 'M', 2500)
    Tshirt: TshirtT = TshirtT('black', 'XL', 500)

p = {
    'Name': 'Gino',
    'Age': 20,
    'Glasses': {
        'color' : 'red',
        'size' : 'M',
        'prize' : 4000
    },
    'Tshirt': {
        'color' : 'red',
        'size' : 'M',
        'prize' : 4000
    }
}

a = People.from_dict(p)
print(a)

输出:

People(Name='Gino', Age=20, Glasses=Glasses(color='red', size='M', prize=4000), Tshirt=Tshirt(color='red', size='M', prize=4000))

带有重命名类的代码版本:

Try it online!

from dataclasses import dataclass
from dataclasses_json import dataclass_json

@dataclass
class Glasses_:
    color: str
    size: str
    prize: int

@dataclass
class Tshirt_:
    color: str
    size: str
    prize: int

@dataclass_json
@dataclass
class People:
    Name : str = 'James'
    Age : int = 25
    Glasses: Glasses_ = Glasses_('black', 'M', 2500)
    Tshirt: Tshirt_ = Tshirt_('black', 'XL', 500)

p = {
    'Name': 'Gino',
    'Age': 20,
    'Glasses': {
        'color' : 'red',
        'size' : 'M',
        'prize' : 4000
    },
    'Tshirt': {
        'color' : 'red',
        'size' : 'M',
        'prize' : 4000
    }
}

a = People.from_dict(p)
print(a)

输出:

People(Name='Gino', Age=20, Glasses=Glasses_(color='red', size='M', prize=4000), Tshirt=Tshirt_(color='red', size='M', prize=4000))