Skip to main content

dhis2w-ql

d2ql — a pipeline query and transform language with an embedded expression language, d2path. Pure engine, no DHIS2 required: it queries any JSON-shaped data — lists of dicts, Pydantic models, local .json/.ndjson files — with a pushdown seam for backends that can answer parts of a query natively.

This package is the language engine alone: tokenizer, recursive-descent parser, Pydantic AST, expression evaluator, query planner, and execution engine over a source-agnostic DataSource protocol. Its only dependency is pydantic. The DHIS2 binding (live DataSource, pushdown compiler, CLI, MCP tools) lives in the query plugin in dhis2w-core; FHIR is a consumer of the engine (d2path evaluates over any JSON, and the transform stage can emit FHIR resources) rather than a dependency.

Install

uv add dhis2w-ql        # or: pip install dhis2w-ql

Quickstart — d2path over your own JSON

d2path is the expression layer: path navigation, operators, and ~40 functions with collection semantics.

from dhis2w_ql import Evaluator, parse_expression

facilities = [
    {"name": "Ngelehun CHC", "level": 4, "tags": ["chc", "rural"]},
    {"name": "Kailahun MCHP", "level": 4, "tags": ["mchp"]},
    {"name": "Bo District", "level": 2, "tags": []},
]

expression = parse_expression("where(level = 4 and tags.count() > 0).name.select(upper())")
print(Evaluator().evaluate(expression, facilities))
# ['NGELEHUN CHC', 'KAILAHUN MCHP']

Quickstart — a full pipeline over in-memory rows

The pipeline layer adds stages (where, select, transform, order, paging, group by, fold), named definitions, and sinks. InMemoryBinder maps resource names to row lists; any backend can implement the same DataSource protocol and advertise which filters/ordering/paging it can execute natively — the planner pushes that prefix down and runs the rest locally.

import asyncio

from dhis2w_ql import InMemoryBinder, QueryEngine, parse

program = parse("""
facilities
  | where level = 4
  | select name, level
  | order name asc
  | limit 10
""")

engine = QueryEngine(program, InMemoryBinder({"facilities": facilities}))
result = asyncio.run(engine.run_terminal())
print(result.rows)
# [{'name': 'Kailahun MCHP', 'level': 4}, {'name': 'Ngelehun CHC', 'level': 4}]

Programs can also read local files directly — read("facilities.json") or read("events.ndjson") as the source — and end in a sink (>> "out.csv", >> stdout as ndjson).

Language shape

define ActiveAggregates:
  dataElements | where domainType = "AGGREGATE"

ActiveAggregates
  | where name ~ "ANC"
  | select id, name, categoryCombo.name as combo
  | transform { code: id, label: name }
  | order name asc
  | limit 20
  >> "elements.csv"
  • Expression layer (d2path): path navigation, operators, and functions with collection semantics — used inside where, select, order, and transform.
  • Pipeline layer: stages separated by |, optionally ending in a >> sink.
  • Definitions: define NAME: ... and define function NAME(args): ... make a .d2ql file a reusable library of named queries and helpers.

The pipeline | is the stage separator; collection union is the union() function (not the | operator) to keep the two unambiguous.

Collection semantics in one minute

  • Every expression evaluates to a collection; navigation (a.b) flattens one level per hop.
  • A single-element collection collapses to its scalar in results.
  • String functions (upper(), lower(), length(), trim(), toChars()) operate on a singleton focus; map them over a collection with select(...)name.select(upper()).
  • Comparisons are existential over collections: tags = "chc" is true when any element matches.

Documentation

The curated catalogs ship in the package: dhis2w_ql.SAMPLES (sample programs) and dhis2w_ql.DOC_EXAMPLES (the evaluator-verified example catalog behind the docs page).

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dhis2w_ql-1.14.0.tar.gz (48.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dhis2w_ql-1.14.0-py3-none-any.whl (56.2 kB view details)

Uploaded Python 3

File details

Details for the file dhis2w_ql-1.14.0.tar.gz.

File metadata

  • Download URL: dhis2w_ql-1.14.0.tar.gz
  • Upload date:
  • Size: 48.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for dhis2w_ql-1.14.0.tar.gz
Algorithm Hash digest
SHA256 c71525c6f7d398071a60f4140a35c9285eba7a928bb87a6787eef26254e4568c
MD5 d5b80b8d45ae2423c05d315385fd1fe9
BLAKE2b-256 5cc968c21b423b00db3a77425e6c9446e2ac8fcd869be5a8486676d21bb5fcfe

See more details on using hashes here.

Provenance

The following attestation bundles were made for dhis2w_ql-1.14.0.tar.gz:

Publisher: pypi-publish.yml on winterop-com/dhis2w-utils

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dhis2w_ql-1.14.0-py3-none-any.whl.

File metadata

  • Download URL: dhis2w_ql-1.14.0-py3-none-any.whl
  • Upload date:
  • Size: 56.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for dhis2w_ql-1.14.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7b6d75c1f5ddaaf5d49aa7c3fbbb6e7a68e9d7ef20e21b349011f340b5409df3
MD5 2c9f30c593b1c062fdb1ca50a4ae10aa
BLAKE2b-256 5c66a5e91c4b1b2f122bf3cc1ecfb143a87fe98e145941baaaf599af91089033

See more details on using hashes here.

Provenance

The following attestation bundles were made for dhis2w_ql-1.14.0-py3-none-any.whl:

Publisher: pypi-publish.yml on winterop-com/dhis2w-utils

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

1.17.0

2 files

1.16.0

2 files

1.15.0

2 files

This release

1.14.0 This release

2 files

1.13.4

2 files

1.13.3

2 files

1.13.2

2 files

1.13.1

2 files

1.13.0

2 files

1.12.0

2 files

1.11.0

2 files

1.10.0

2 files

1.9.0

2 files

1.8.3

2 files

1.8.2

2 files

1.8.1

2 files

1.8.0

2 files

1.7.0

2 files

1.6.0

2 files

1.5.0

2 files

1.3.0

2 files

1.2.0

2 files

1.1.0

2 files

1.0.0

2 files

0.99.1

2 files

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