Skip to main content

gymnasdicts

https://img.shields.io/pypi/v/gymnasdicts.svg https://img.shields.io/travis/unai/gymnasdicts.svg Documentation Status Updates

query json dicts

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.

Metadata

Release files for gymnasdicts 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gymnasdicts 0.1.2
File Size Uploaded Provenance
gymnasdicts-0.1.2.tar.gz 20.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for gymnasdicts 0.1.2
File Interpreter ABI Platform
gymnasdicts-0.1.2-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 27.3 kB

Release files / gymnasdicts-0.1.2.tar.gz

Download URL gymnasdicts-0.1.2.tar.gz
Size 20.5 kB
Tags Source
SHA-256 checksum
How to use checksums
e2440db95863ba75fa8a8a53131201d3dd922efa51f15c2ad16b006954a3a67e
BLAKE2b-256 checksum
How to use checksums
e6a12e676e528609d74b9eaf10669439559f1bef16e8e0a58eabbccad704804d
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / gymnasdicts-0.1.2-py2.py3-none-any.whl

Download URL gymnasdicts-0.1.2-py2.py3-none-any.whl
Size 6.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
c2ab54100b63215ee8b52ab9a07fcd5bca99781186996afd525e7a82aef2f7b3
BLAKE2b-256 checksum
How to use checksums
cd00eeb81ecd61aee87633cb4366167158477e16f86bf90f2391146e6db4b62f
Upload date
Uploaded using Trusted Publishing?
What is 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

Security for gymnasdicts 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page