ntgen:生成NamedTuple定义

ntgen的Python项目详细描述


命名元组生成器

CircleCIPyPI - Package VersionPyPI - Python VersionPyPI - License

根据数据自动生成带有typehints的NamedTuple定义。 如果您曾经觉得为正在处理的任何json数据准备namedtupleskeleton是一件乏味的事情,而且可能是这样 自动化,好吧,这是一个自动化过程的工具。在

使用

假设您要为以下json对象准备NamedTuple定义:

$ cat apartment.json
{"id": "1234-1234",
    "type": "living",
    "isAvailable": true,
    "countryCode": "DE",
    "address": {"borough": "Dulsberg",
        "city": "Hamburg",
        "houseNumber": "2",
        "latitude": 53.587485,
        "longitude": 10.063215,
        "postalCode": "22049",
        "streetName": "Nordschleswiger Strasse",
        "area": "Hamburg"},
    "_attachments": "attachments/",
    "_ts": 15828103462}%

您只需运行以下命令:

^{pr2}$

默认情况下,输出将定向到stdout—您也可以将其重定向到一个文件,以便使用 类定义。在

运行时配置

要了解所有运行时配置选项,请运行:

$ ntgen --help
usage: ntgen [--out OUT][--name NAME][-s][-c][-f][-t][--max_level MAX_LEVEL][-h]
             input

positional arguments:
  input                 (str, default=None) Json file containing an object
                        with the data to analyzed

optional arguments:
  --out OUT             (Union[str, NoneType], default=None) Destination file
                        to write the Python code to
  --name NAME           (str, default=NTGenTuple) Name of the main NamedTuple
  -s, --snake-case      (bool, default=True) Convert the NamedTuple field
                        names to snake_case
  -c, --camel-case      (bool, default=True) Convert the NamedTuple class
                        names to CamelCase
  -f, --constructors    (bool, default=False) Insert generic methods that will
                        allow for parsing of the analyzed data structures
  -t, --as-dict         (bool, default=False) Insert generic methods allowing
                        for dumping the nested NamedTuple hierarchy to a dict
  --max_level MAX_LEVEL
                        (Union[int, NoneType], default=None) Specify the max
                        nesting level of the NamedTuple
  -h, --help            show this help message and exit

其他调用选项

也可以从Python上下文中使用库:

>>>fromntgenimportgenerate_from_dict>>>data={'name':'John Wick','profession':'assassin','age':34}>>>print(generate_from_dict(data=data,name="Character"))classCharacter(NamedTuple):name:strprofession:strage:int

安装

您需要运行Python>;=3.6。在

pip install ntgen

验证是否正确安装了最新的程序包版本:

>>>importntgen>>>ntgen.__version__'0.2.0'

许可证

这个项目是在麻省理工学院许可下授权的-有关详细信息,请参阅LICENSE文件

作者

马吉拉帕茨

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

推荐PyPI第三方库


热门话题
tomcat Java条带错误   java OPENTSDB fsck修复程序不更正重复点   java JavaFX在控制器内切换自身的可见性   java maven surefire插件未并行执行运行程序   读取导致Freemarker模板引擎中TemplateException的Java对象   无法使Java库与我的Android应用程序一起工作   安卓 java。lang.IllegalStateException游标   使用Java检索XML文件中的XSL URL和名称   java如何从文本文件集合中提取特定值   电子邮件java mail gmail   java为什么finalize()只被垃圾收集器调用一次?   java方法findViewById(int)对于Json类型是未定义的。蛇形   java在安卓中尝试从brother打印机打印位图时遇到以下异常   java在颤振中支持Kotlin的优势   java从后面编写文本   java制作列表。第一个列表可以有相同的数字,第二个将是价格,尝试制作第三个列表,如果它们是相同的数字,它将添加价格