ntgen:生成NamedTuple定义
ntgen的Python项目详细描述
命名元组生成器
根据数据自动生成带有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文件
作者
马吉拉帕茨
- 项目
标签: