Article | carefree huan \
Source: Python Technology “ID: pythonall”
To do a good job, you must sharpen your tools. We need to learn some “advanced” skills if we want to develop more easily and efficiently. Not long ago I saw a Python monk code, which used a short and concise module, I think it is quite useful, today to share with you.
This module, called Glom, is a small Python module that handles data and has the following features:
- Nested structure and path-based access
- Use the lightweight Pythonic specification for declarative data transformation
- Readable and meaningful error messages
- Built-in data detection and debugging capabilities
It’s kind of abstract, right? Let’s use an example to show you.
The installation
As a built-in Python module, you must know how to install it:
pip3 install glom
In seconds!
Simple to use
Let’s look at the simplest use:
d = {"a": {"b": {"c": 1}}}
print(glom(d, "a.b.c")) # 1
Copy the code
Here, we have a nested three layers of JSON structure, we want to get the value of the innermost layer of C, the normal writing should be:
print(d["a"] ["b"] ["c"])
Copy the code
Would you scoff if I said glom is better than the traditional way because you don’t have to write layer upon layer of brackets and quotes?
Ok, let’s look at the following situation:
d = {"a": {"b": None}}
print(d["a"] ["b"] ["c"])
Copy the code
Iterating through a None object, you get the following error:
Traceback (most recent call last):
File "/Users/cxhuan/Documents/python_workspace/mypy/pmodules/pglom/glomstudy.py", line 10, in <module>
print(d["a"] ["b"] ["c"])
TypeError: 'NoneType' object is not subscriptable
Copy the code
Let’s take a look at glom’s handling:
from glom import glom
d = {"a": {"b": None}}
print(glom(d, "a.b.c"))
Copy the code
Likewise, glom cannot pair the error output, and you get the following error:
Traceback (most recent call last):
File "/Users/cxhuan/Documents/python_workspace/mypy/pmodules/pglom/glomstudy.py", line 11, in <module>
print(glom(d, "a.b.c"))
File "/ Library/Frameworks/Python framework Versions / 3.7 / lib/python3.7 / site - packages/glom/core. Py." ", line 2181, in glom
raise err
glom.core.PathAccessError: error raised while processing, details below.
Target-spec trace (most recent last):
- Target: {'a': {'b': None}}
- Spec: 'a.b.c'
glom.core.PathAccessError: could not access 'c', part 2 of Path('a'.'b'.'c'), got error: AttributeError("'NoneType' object has no attribute 'c'")
Copy the code
If you look carefully at the error content, you will find that the error content is extremely detailed, at a glance, this is a magic tool for finding bugs in the program!
Complex usage
Just a simple example, let us have an intuitive understanding of glom, next let’s look at the definition of glom method:
glom(target, spec, **kwargs)
Let’s look at what parameters mean:
- Target: Target data, which can be dict, list, or any other object
- Spec: is what we want to output
Let’s use this method.
Let’s start with an example. We have a dict, and if we want to get all the names, we can do it with glom:
data = {"student": {"info": [{"name": "Zhang"}, {"name": "Bill"}]}}
info = glom(data, ("student.info"["name"]))
print(info) # ['Joe'.'bill']
Copy the code
In the traditional way, we might have to iterate to get it, but with glom, we only need one line of code, and the output is an array.
If you want a dict instead of an array, that’s easy:
info = glom(data, {"info": ("student.info"["name"])})
print(info) # {'info': ['Joe'.'bill']
Copy the code
We just need to assign the original array to a dictionary to receive it.
Deal with troublesome needs
If I now have two sets of data, I want to fetch the value of name:
data_1 = {"school": {"student": [{"name": "Zhang"}, {"name": "Bill"}]}}
data_2 = {"school": {"teacher": [{"name": "Miss Wang"}, {"name": "Miss Zhao"}]}}
spec_1 = {"name": ("school.student"["name"])}
spec_2 = {"name": ("school.teacher"["name"])}
print(glom(data_1, spec_1)) # {'name': ['Joe'.'bill']}
print(glom(data_2, spec_2)) # {'name': ['Miss Wang'.'Miss Zhao']}
Copy the code
That’s how we usually write it, right? What if we had multiple sets of data, and each set was taken in a similar way? We can use the Coalesce method to avoid repeating N rows of parameters: Coalesce: Coalesce: Coalesce: Coalesce: Coalesce: Coalesce: Coalesce: Coalesce: Coalesce: Coalesce
data_1 = {"school": {"student": [{"name": "Zhang"}, {"name": "Bill"}]}}
data_2 = {"school": {"teacher": [{"name": "Miss Wang"}, {"name": "Miss Zhao"}]}}
spec = {"name": (Coalesce("school.student"."school.teacher"),"name"])}
print(glom(data_1, spec)) # {'name': ['Joe'.'bill']}
print(glom(data_2, spec)) # {'name': ['Miss Wang'.'Miss Zhao']}
Copy the code
You can use Coalesce to aggregate multiple requirements and value them against the same spec.
Here’s another big kill — value calculation. Glom can also perform simple calculations of values. Let’s take an example:
data = {"school": {"student": [{"name": "Zhang"."age": 8}, {"name": "Bill"."age": 10}]}}
spec = {"sum_age": ("school.student"["age"], sum)}
print(glom(data, spec)) # {'sum_age': 18}
Copy the code
conclusion
Introduced so much, we should know the fierce place of Glom, it is said that many big guys like to use it. In fact, it has a lot of other practical functions to be explored, here will not be introduced. If you find the modules shared today useful, click “Watching” to support!
PS: Reply “Python” within the public number to enter the Python novice learning exchange group, together with the 100-day plan!
Old rules, brothers still remember, the lower right corner of the “watching” click, if you feel the content of the article is good, remember to share moments to let more people know!
[Code access ****]
Identify the qr code at the end of the article, reply: 210412