attrs的序列化加载项
attrs-serde的Python项目详细描述
属性序列
attrs的序列化加载项。
person_dict={"contact":{"personal":{"name":"John"},"phone":"555-112233"}}name_path=["contact","personal","name"]phone_path=["contact","phone"]@serde@attrsclassPerson(object):name=attrib(metadata={"to":name_path,"from":name_path})phone=attrib(metadata={"to":phone_path,"from":phone_path})>>>p=Person.from_dict(person_dict)Person(name=Johnphone=555-112233)>>>p.to_dict{"contact":{"personal":{"name":"John"},"phone":"555-112233"}}
快速启动
使用pip/pipenv/etc进行安装(我们建议poetry用于SANE依赖关系管理):
$ poetry add attrs-serde
用serde
装饰自动to_dict
和from_dict
。在metadata
:
from
-从中获取字段值的路径to
-将值序列化到的路径(根据需要创建嵌套字典)
示例:
fromattrs_serdeimportserdefromattrimportattrs,attrib@serde@attrsclassPerson(object):name=attrib(metadata={"to":name_path,"from":name_path})phone=attrib(metadata={"to":phone_path,"from":phone_path})
自定义from
和to
键(如果您或其他扩展使用这些键):
fromattrs_serdeimportserdefromattrimportattrs,attrib@serde(from_key="get",to_key="set")@attrsclassPerson(object):name=attrib(metadata={"get":name_path,"set":name_path})phone=attrib(metadata={"get":phone_path,"set":phone_path})
性能
attrs-serde
与cytoolz
(主要是c实现)一起工作,因此与attrs
已经做的相比,开销非常低。
反对手动对象构造:
------------------------------------------------------------------------------------- benchmark 'deserialization': 2 tests ------------------------------------------------------------------------------------
Name (time in ns) Min Max Mean StdDev Median IQR Outliers OPS (Kops/s) Rounds Iterations
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
test_deser_baseline 583.2500 (1.0) 5,037.3500 (1.0) 641.4743 (1.0) 161.4237 (1.0) 603.8500 (1.0) 33.7500 (1.0) 2315;3276 1,558.9089 (1.0) 77828 20
test_deser_serde 1,976.0000 (3.39) 88,504.0000 (17.57) 2,226.3774 (3.47) 1,195.7336 (7.41) 2,127.0000 (3.52) 110.0000 (3.26) 484;1576 449.1601 (0.29) 86806 1
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
针对属性的序列化asdict
:
-------------------------------------------------------------------------- benchmark 'serialization': 2 tests --------------------------------------------------------------------------
Name (time in us) Min Max Mean StdDev Median IQR Outliers OPS (Kops/s) Rounds Iterations
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
test_ser_baseline 2.6600 (1.0) 130.4550 (1.33) 2.9098 (1.0) 1.3230 (1.0) 2.7940 (1.0) 0.1320 (1.0) 302;882 343.6625 (1.0) 46642 1
test_ser_serde 5.0390 (1.89) 98.4540 (1.0) 5.6411 (1.94) 2.2398 (1.69) 5.4465 (1.95) 0.2890 (2.19) 491;912 177.2706 (0.52) 32936 1
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
谢谢:
向所有人Contributors致意-你做到了,谢谢!
版权所有
版权所有(c)2018@jondot。有关详细信息,请参见LICENSE。