一个声明性对象转换器和格式化工具,用于聚合嵌套数据。
项目描述
glom
以Python方式重构数据
真实的应用程序有真实的数据,真实的数据是嵌套的。对象嵌套在对象中,对象嵌套在列表中。
glom是一种处理真实世界数据的新方法,它具有以下特点:
- 基于路径访问嵌套数据结构
- 可读性强的错误消息
- 声明性数据转换,使用轻量级、Pythonic规范
- 内置数据探索和调试功能
所有这些以及更多,作为完全文档化的、纯Python包,在Python 3.7+上进行了测试,以及PyPy3。安装就像
pip install glom
当您安装glom时,您还可以获得glom命令行界面,让您可以在控制台中实验,但从不限制您使用shell脚本
Usage: glom [FLAGS] [spec [target]]
Command-line interface to the glom library, providing nested data access and data
restructuring with the power of Python.
Flags:
--help / -h show this help message and exit
--target-file TARGET_FILE path to target data source (optional)
--target-format TARGET_FORMAT
format of the source data (json, python, toml,
or yaml) (defaults to 'json')
--spec-file SPEC_FILE path to glom spec definition (optional)
--spec-format SPEC_FORMAT format of the glom spec definition (json, python,
python-full) (defaults to 'python')
--indent INDENT number of spaces to indent the result, 0 to disable
pretty-printing (defaults to 2)
--debug interactively debug any errors that come up
--inspect interactively explore the data
您可以在命令行上完成任何事情都可以轻松转换为Python代码,因此当复杂性开始增加时,您始终有一个前进的方向。
示例
没有glom
>>> data = {'a': {'b': {'c': 'd'}}}
>>> data['a']['b']['c']
'd'
>>> data2 = {'a': {'b': None}}
>>> data2['a']['b']['c']
Traceback (most recent call last):
...
TypeError: 'NoneType' object is not subscriptable
有glom
>>> glom(data, 'a.b.c')
'd'
>>> glom(data2, 'a.b.c')
Traceback (most recent call last):
...
PathAccessError: could not access 'c', index 2 in path Path('a', 'b', 'c'), got error: ...
了解更多信息
如果所有这些都很有趣,请继续探索下面的glom
上面的所有链接都充满了示例,但如果你发现关于文档或glom本身有任何不足之处,请提交一个issue!
与此同时,请记住:当你有嵌套数据时,glom它!☄️
项目详情
下载文件
下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。
源分布
glom-23.5.0.tar.gz (196.7 kB 查看哈希值)
构建分布
glom-23.5.0-py3-none-any.whl (102.7 kB 查看哈希值)
关闭
glom-23.5.0.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 06af5e3486aacc59382ba34e53ebeabd7a9345d78f7dbcbee26f03baa4b83bac |
|
MD5 | e6925074e93b573408e3e38b19a58f3c |
|
BLAKE2b-256 | abf89caa4312149741e7870cdce88c7d6388b0b0d076e4107303ed1417877abf |
关闭
glom-23.5.0-py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | fe4e9be4dc93c11a99f8277042e4bee95419c02cda4b969f504508b0a1aa6a66 |
|
MD5 | 25bc57d11e50a9507a03b34e43dae061 |
|
BLAKE2b-256 | 46239ee0ae745acb65d655bb11240722f98495dcecf543e9ff3b1d21cccea252 |