重新映射dict

dictremapper的Python项目详细描述


dictremapper正在重新映射dict库

  • 已订购(重要)
  • 支持继承(映射器)
  • 支持嵌套结构
  • (不包括每个层的选项)

简单示例

importrequestsimportjsonfromdictremapperimportRemapper,PathclassSummaryRemapper(Remapper):fullname=Path("full_name")url=Path("html_url")description=Path("description")url="https://api.github.com/repos/podhmo/dictremapper"data=requests.get(url).json()data=SummaryRemapper()(data)print(json.dumps(data,indent=2))

输出

{
  "fullname": "podhmo/dictremapper",
  "url": "https://github.com/podhmo/dictremapper",
  "description": "remapping dict"
}

支持继承(映射器)

fromdictremapperimportRemapper,PathclassURLRemapper(Remapper):url=Path("html_url")classDescriptionRemapper(Remapper):fullname=Path("full_name")description=Path("description")classSummaryRemapper(URLRemapper,DescriptionRemapper):star=Path("stargazers_count")url="https://api.github.com/repos/podhmo/dictremapper"data=requests.get(url).json()remapped=SummaryRemapper()(data)print(json.dumps(remapped,indent=2))

输出

{
  "url": "https://github.com/podhmo/dictremapper",
  "fullname": "podhmo/dictremapper",
  "description": "remapping dict",
  "star": 0
}

如果你想换订单的话。ChangeOrder也许有帮助。

fromdictremapperimportChangeOrderclassSummaryRemapper2(URLRemapper,DescriptionRemapper):url=ChangeOrder(URLRemapper.url)description=ChangeOrder(DescriptionRemapper.description)star=Path("stargazers_count")remapped=SummaryRemapper2()(data)print(json.dumps(remapped,indent=2))

输出

{
  "fullname": "podhmo/dictremapper",
  "url": "https://github.com/podhmo/dictremapper",
  "description": "remapping dict",
  "star": 0
}

支持嵌套结构

classMyMapper3(Remapper):body=Path("body",callback=MyMapper())children=Path("children",callback=MyMapper2(many=True))

排除每个层的选项

classMyMapper3(Remapper):body=Path("body",callback=MyMapper())children=Path("children",callback=MyMapper2(many=True,excludes=["object.name","object.age","id"]))MyMapper3(excludes=["children.object.description","body"])(d)

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java Kafka DSL Kstream>Ktable连接序列化编译错误   java为什么Bean A中的参数注入会破坏属性解析Bean B?(都是类型PropertyPlaceHolderConfigure的类型)   java Oracle SQL开发人员在尝试导出数据时调用TargetException   编辑JTree中返回的java意外值   Java,Lambda:如何从不同类型的列表集合中查找列表?   如何使用java执行命令   java如何将drawable分配给widgets按钮   JavaIntelliJ13JSF对Springbeans的支持   java转换。转换成补语   java Tomcat的性能测试   sendmail Java发送邮件函数   java重用ProjectReactor/Flux/Mono被认为是最佳实践吗?   javafx在Java中错误地转换为lambda表达式   反序列化json数组,其中属性值为数组java   java After ItemClickListener调用方法   此Java代码中有多少对象符合垃圾收集条件?   java程序“keytool”可以在以下包中找到