使用 dacite.from_dict 动态添加数据类字段
我正在使用英安岩将 Python 字典转换为数据类。有没有办法动态地将字段添加到数据类中?就像下面的示例一样,数据类“Parameters”仅定义了一个时间序列“timeseriesA”,但可能还有其他无法声明的时间序列(通过字典提供)。
from dataclasses import asdict, dataclass
from typing import Dict, List, Optional
from dacite import from_dict
@dataclass(frozen = True)
class TimeSeries:
name: str
unit: str
data: Optional[List[float]]
@dataclass(frozen = True)
class Parameters:
timeseriesA: TimeSeries
@dataclass(frozen = True)
class Data:
parameters: Parameters
@classmethod
def fromDict(cls, data: Dict) -> 'Data':
return from_dict(cls, data)
@classmethod
def toDict(cls) -> Dict:
return asdict(cls)
def main() -> None:
d: Dict = {
'parameters': {
'timeseriesA': {
'name': 'nameA',
'unit': 'USD',
'data': [10, 20, 30, 40]
},
'timeseriesB': {
'name': 'nameB',
'unit': 'EUR',
'data': [60, 30, 40, 50]
}
}
}
data: Data = Data.fromDict(d)
if __name__ == '__main__':
main()
在此示例中,“timeseriesB”将被英安岩忽略,但应添加为“Parameters”数据类的字段。
I am using dacite to transform a Python dictionary into a dataclass. Is there a way to dynamically add fields to a dataclass? Like in the example below, where the dataclass "Parameters" has defined only one timeseries "timeseriesA", but there might be additional ones (provided through the dictionary) that cannot be declared.
from dataclasses import asdict, dataclass
from typing import Dict, List, Optional
from dacite import from_dict
@dataclass(frozen = True)
class TimeSeries:
name: str
unit: str
data: Optional[List[float]]
@dataclass(frozen = True)
class Parameters:
timeseriesA: TimeSeries
@dataclass(frozen = True)
class Data:
parameters: Parameters
@classmethod
def fromDict(cls, data: Dict) -> 'Data':
return from_dict(cls, data)
@classmethod
def toDict(cls) -> Dict:
return asdict(cls)
def main() -> None:
d: Dict = {
'parameters': {
'timeseriesA': {
'name': 'nameA',
'unit': 'USD',
'data': [10, 20, 30, 40]
},
'timeseriesB': {
'name': 'nameB',
'unit': 'EUR',
'data': [60, 30, 40, 50]
}
}
}
data: Data = Data.fromDict(d)
if __name__ == '__main__':
main()
In this example, "timeseriesB" will be ignored by dacite, but should be added as field for the "Parameters" dataclass.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一般来说,在定义类之后向数据类动态添加字段并不是一个好的做法。然而,由于源
dict
对象中字段的动态性质,这确实为在数据类中使用dict
提供了一个很好的用例。下面是一个使用
dict
字段来处理源对象中键的动态映射的简单示例,使用dataclass-wizard
这也是一个类似的 JSON 序列化库。下面概述的方法处理 dict 对象中的无关数据,例如timeseriesB
。诚然,
dataclass-wizard
并不像dacite
那样执行严格类型检查,而是执行隐式类型强制,如果可能的话,就像str
到带注释的int
一样。也许因此,它总体上要快得多;另一个好处是序列化甚至比内置dataclasses.asdict
还要快:-)以下是一些快速测试:
结果,在我的 PC (Windows) 上:
In general, dynamically adding fields to a dataclass, after the class is defined, is not good practice. However, this does present a good use case for using a
dict
within a dataclass, due to the dynamic nature of fields in the sourcedict
object.Here is a straightforward example of using a
dict
field to handle a dynamic mapping of keys in the source object, using thedataclass-wizard
which is also a similar JSON serialization library. The approach outlined below handles extraneous data in the dict object liketimeseriesB
for instance.The
dataclass-wizard
admittedly doesn't perform strict type checking likedacite
, but instead performs implicit type coercion, likestr
to annotatedint
, where possible. Perhaps as a result, it's overall much faster; the other nice thing is serialization is even slightly faster than builtindataclasses.asdict
too :-)Here are some quick tests:
Results, on my PC (Windows):