重新映射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 OutOfMemoryError以及如何使运行继续   java树映射内存使用率   使用带有java spark的javascript获取api的挂起承诺状态(POST请求)   java Android:存储用户单击的CardView   输入了有关拖动的java信息   单元测试中的java DecimalFormat(“·#,##0”)前缀   来自客户端的java超时webservice调用   java未使用的超类变量   如何在Java中的参数提示中强制执行整数限制   启动期间Tomcat中出现java错误:找不到DB名称   java Marshall/Unmarshall地图   一个类在另一个类中的java访问变量   如何在java中对字符数组的文本进行对齐?   从jenkins运行时,使用selenium上传excel文件的java不起作用   java将double转换为BigInteger   java Android键盘问题与按键释放   旋转java在两个旋转矩形之间的碰撞检测   在TestNG中执行时,XML中出现java启动错误无法继续执行测试