|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +from glom import glom, Coalesce |
| 5 | + |
| 6 | +d = {"a": {"b": {"c": 1}}} |
| 7 | +print(glom(d, "a.b.c")) |
| 8 | + |
| 9 | +d = {"a": {"b": None}} |
| 10 | +# print(d["a"]["b"]["c"]) |
| 11 | +# print(glom(d, "a.b.c")) |
| 12 | + |
| 13 | +data = {"student": {"info": [{"name": "张三"}, {"name": "李四"}]}} |
| 14 | +info = glom(data, ("student.info", ["name"])) |
| 15 | +print(info) |
| 16 | + |
| 17 | +info = glom(data, {"info": ("student.info", ["name"])}) |
| 18 | +print(info) |
| 19 | + |
| 20 | + |
| 21 | +data_1 = {"school": {"student": [{"name": "张三"}, {"name": "李四"}]}} |
| 22 | +data_2 = {"school": {"teacher": [{"name": "王老师"}, {"name": "赵老师"}]}} |
| 23 | + |
| 24 | +spec_1 = {"name": ("school.student", ["name"])} |
| 25 | +spec_2 = {"name": ("school.teacher", ["name"])} |
| 26 | +print(glom(data_1, spec_1)) |
| 27 | +print(glom(data_2, spec_2)) |
| 28 | + |
| 29 | + |
| 30 | +spec = {"name": (Coalesce("school.student", "school.teacher"), ["name"])} |
| 31 | + |
| 32 | +print(glom(data_1, spec)) |
| 33 | +print(glom(data_2, spec)) |
| 34 | + |
| 35 | +data = {"school": {"student": [{"name": "张三", "age": 18}, {"name": "李四", "age": 20}]}} |
| 36 | +spec = {"sum_age": ("school.student", ["age"], sum)} |
| 37 | +print(glom(data, spec)) |
0 commit comments