重新映射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)