重新映射dict


dictremapper的Python项目详细描述

dictremapper正在重新映射dict库

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

简单示例

import requests
import json
from dictremapper import Remapper, Path


class SummaryRemapper(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"
}

支持继承(映射器)

from dictremapper import Remapper, Path


class URLRemapper(Remapper):
    url = Path("html_url")


class DescriptionRemapper(Remapper):
    fullname = Path("full_name")
    description = Path("description")


class SummaryRemapper(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也许有帮助。

from dictremapper import ChangeOrder


class SummaryRemapper2(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
}

支持嵌套结构

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

排除每个层的选项

 class MyMapper3(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)

推荐PyPI第三方库


热门话题
dcn dtt taskflow mangum quorum 有效率 ffx 财务数据 feynman vndb multimedia myhello hgvs zambi 等位基因 classic luqum 家谱 工作组 lbfgs domoticz 编辑器 diss topojson 药丸 vali scrapers 规格化 spins dtools 子项目 zopeskel pyeos torii bbcf hdl pod1m 格式 gentl sj value commvault hrf callme basiq alwaysdata utilitybelt 掩模 restructedtext pipin