# glom
*glom gets results, conglomeratively.*
Real applications have real data, and real data nests. Objects inside
of objects inside of lists of objects.
glom is a powerful and declarative way to handle real-world nested
data. A sort of *object templating*.
## Introduction
Think back, probably not too far, to a time you saw code like this:
```python
value = target.a['b']['c']
```
This code probably gives you back the value you want. But experienced
developers know how fragile it can be. This tiny line can raise any of
the following:
```
AttributeError: 'TargetType' object has no attribute 'a'
KeyError: 'b'
TypeError: 'NoneType' object has no attribute '__getitem__'
TypeError: list indices must be integers, not str
```
And for those last two, where in the line did the failure occur? `a`
or `a['b']` or `a['b']['c']`?
Experienced developers will often split this code up to be more
debuggable, but this leads to verbose, tedious code, with its own set
of maintenance issues.
Enter glom.
glom is a new, Pythonic approach to nested data that makes all these
problems go away.
```python
value = glom(target, 'a.b.c')
```
On success, you get your value, same as ever. On failure, you see:
```
PathAccessError: could not access 'c' from path Path('a', 'b', 'c'), got error: ...
```
And that's just the beginning.
## Object templating
glom goes far beyond deep access, implementing a coherent, declarative
approach for accessing and building objects. glom's envisions the
ideal data manipulation as code resembling the data itself.
For instance, if requirements change, and `target` becomes a list, our
access code becomes:
```python
value = glom(target, ['a.b.c'])
```
If we want a more full-fledged object wrapping for our results:
```python
value = glom(target, {'result_count': len,
'results': [{'c': 'a.b.c'}]})
```
glom not only calls callables (`len`), it supports chained calls with
tuples, fallback calls with `Coalesce`, and interactive debugging with
`Inspect`. See [the tutorial](https://github.com/mahmoud/glom/blob/master/glom/tutorial.py)
and API reference for more in-depth docs.
## FAQ
Paradigm shifts always raise a question or two.
### *What does "glom" mean?*
"glom" is short for "conglomerate", and can be used as a noun or
verb. An astronomer might say, "space dust gloms together to create
planets and planetoids". Got some data you need to transform? Glom it! ☄️
### Any other handy terminology?
A couple of terms that help navigate around glom's powerful semantics:
* **target** - Glom operates on a variety of inputs, so we simply
refer to the object being accessed as the "target"
* **spec** - *(aka "glomspec")* The accompanying template used to
specify the structure of the returned value.
### Other tips?
Just a few (for now):
* Specs don't have to live in the glom call. You can put them
anywhere. Commonly-used specs work as class attributes and globals.
* Using glom's declarative approach does wonders for code coverage,
much like [attrs](https://github.com/python-attrs/attrs) and
[schema](https://github.com/keleshev/schema), both of which go great
with glom.
* Advanced tips
* glom is designed to support all of Python's built-ins as targets,
and is readily extensible to other types, through glom's
`register()` call.
* If you're trying to minimize global state, consider
instantiating your own `glom.Glommer` object to encapsulate any
type registration changes.
## TODO
* Expand tutorial
* API docs
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
共37个文件
py:12个
rst:7个
txt:5个
资源分类:Python库 所属语言:Python 资源全名:glom-18.1.1.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
资源推荐
资源详情
资源评论
收起资源包目录
glom-18.1.1.tar.gz (37个子文件)
glom-18.1.1
MANIFEST.in 203B
PKG-INFO 1KB
glom.egg-info
PKG-INFO 1KB
requires.txt 19B
not-zip-safe 1B
SOURCES.txt 650B
entry_points.txt 48B
top_level.txt 5B
dependency_links.txt 1B
.tox-coveragerc 193B
docs
api.rst 3KB
tutorial.rst 376B
conf.py 6KB
snippets.rst 4KB
make.bat 808B
_templates
page.html 291B
cli.rst 3KB
faq.rst 6KB
by_analogy.rst 4KB
Makefile 601B
index.rst 1KB
glom
tutorial.py 9KB
core.py 40KB
cli.py 5KB
test
test_tutorial.py 380B
test_basic.py 7KB
__init__.py 0B
test_target_types.py 4KB
test_main.py 210B
__main__.py 104B
__init__.py 460B
pytest.ini 83B
tox.ini 867B
LICENSE 1KB
setup.cfg 38B
setup.py 2KB
README.md 3KB
共 37 条
- 1
资源评论
挣扎的蓝藻
- 粉丝: 12w+
- 资源: 15万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功