query json dicts
Project description
gymnasdicts
query json dicts
Free software: Apache Software License 2.0
Documentation: https://gymnasdicts.readthedocs.io.
Features
gymnasdicts is a lightweight library for querying json compatible nested-dictionaries in Python.
This package exposes one class with three functions that can be chained together so that they follow a sql-like convention.
Query
Query is the entry point for the library, it is the data-type that is acted on and returned by all other methods.
select
select identifies and names the nested-keys in the json-objects to be used. The first argument is the json object to be queried. The remaining arguments are in the keyword-arg form where:
the values are a restricted jsonpath, where no filtering is allowed.
the keys are user-defined variables to which the values above are assigned.
where
where filters the results of select by value. Its arguments are lambda functions where the argument names correspond to the variables defined in select.
into
into defines the shape of the output. Its only argument is lambda function where the argument names correspond to the variables defined in select.
example
from gymnasdicts import select
payload = {
"sales": [
{"id": 1, "number": 34, "date": "2020-01-04"},
{"id": 2, "number": 12, "date": "2020-02-05"},
{"id": 3, "number": -4, "date": "2020-03-06"},
],
"prices": [
{"id": 1, "cost": {"value": 0.98, "denomination": "pounds"}},
{"id": 2, "cost": {"value": 34, "denomination": "pence"}},
{"id": 3, "cost": {"value": 1.02, "denomination": "pounds"}},
],
"accounting": [
{"denomination": "pounds", "multiplier": 1, },
{"denomination": "pence", "multiplier": 0.01, }
]
}
q = Query(payload)
a = q.select(
sales_id = "$.sales[:].id",
number = "$.sales[:].number",
price_id = "$.prices[:].id",
cost = "$.prices[:].cost[:].value",
denom_1 = "$.prices[:].cost[:].denomination",
denom_2 = "$.accounting[:].denomination",
multiplier = "$.accounting[:].multiplier"
)
w = s.where(
lambda sales_id, price_id: sales_id == price_id,
lambda number: number > 0,
lambda denom_1, denom_2 : denom_1 == denom_2
)
i = w.into(lambda number, cost, multiplier: number * cost * multiplier)
assert sum(i) == 37.4
FAQ
What about joins?
select is effectively a cartesian join on all supplied jsonpaths, i.e.
Query({...}).select(x="$Tbl[:].a", y="$Tbl[:].b", z="$Tbl[:].c")
is equivalent to
select A.a as x, B.b as y, C.c as z from Tbl as A, Tbl as B, Tbl as C
so that where can be used to do the job of on.
This is hideous, what about memory?!
Generators take care of this.
Credits
This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.
Thanks to kclaurelie for useful discussion re: the relationship between select/where and keys/values.
History
0.1.0 (2020-07-28)
First release on PyPI.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file gymnasdicts-0.1.2.tar.gz
.
File metadata
- Download URL: gymnasdicts-0.1.2.tar.gz
- Upload date:
- Size: 20.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e2440db95863ba75fa8a8a53131201d3dd922efa51f15c2ad16b006954a3a67e |
|
MD5 | 07844132a32c2851998c5aff9c37e929 |
|
BLAKE2b-256 | e6a12e676e528609d74b9eaf10669439559f1bef16e8e0a58eabbccad704804d |
File details
Details for the file gymnasdicts-0.1.2-py2.py3-none-any.whl
.
File metadata
- Download URL: gymnasdicts-0.1.2-py2.py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c2ab54100b63215ee8b52ab9a07fcd5bca99781186996afd525e7a82aef2f7b3 |
|
MD5 | 8409aefcc23cee58b1830a7377af9c73 |
|
BLAKE2b-256 | cd00eeb81ecd61aee87633cb4366167158477e16f86bf90f2391146e6db4b62f |