Periods¶
Two hand-written StrEnums covering DHIS2's period names — neither is exposed as an enum upstream (both are Java class hierarchies / 45 bare boolean fields), so we mirror them here for type-safe authoring.
PeriodType— the 24 frequencies aDataSetcollects data at (Daily,Weekly,Monthly,Quarterly,Yearly, …). Source: upstreamPeriodTypeEnum.java.RelativePeriod— the 45 rolling windows aVisualization/EventVisualization/Mapcan pin itself to (last12Months,thisYear,lastSixMonth, …). Mirrors the field names on the generatedRelativePeriodsmodel atdhis2w_client.generated.v42.oas.RelativePeriods. Used byVisualizationSpec.relative_periods— pass a set to emit the correspondingrelativePeriodsblock on the wire.
periods
¶
Hand-written period enums + ISO period math.
PeriodType is the frequency a DataSet collects data at (Daily, Monthly,
Quarterly…). DHIS2's /api/schemas reports DataSet.periodType as TEXT
(not CONSTANT) because upstream it's a Java class hierarchy —
MonthlyPeriodType, DailyPeriodType, etc. are subclasses, not enum
constants. We list the canonical type-name strings here as a StrEnum.
RelativePeriod mirrors the 45 boolean flags on the generated
RelativePeriods model (generated/v42/oas/relative_periods.py) — the
rolling windows a Visualization / EventVisualization / Map can pin
itself to (last12Months, thisYear, lastSixMonth, …). Use the enum
on builder APIs so callers don't have to remember the exact flag casing,
and materialise the selected set into a RelativePeriods model by
toggling the matching fields to True.
parse_period, next_period_id, previous_period_id, and
period_start_end are pure-python helpers that round-trip DHIS2's six
absolute period-id shapes (Daily / Weekly / Monthly / Quarterly /
SixMonthly / Yearly) without any HTTP. Useful when iterating over a
window for analytics queries without leaning on DHIS2's relative-period
shortcuts.
Both enums + the period math are version-independent — the value sets haven't changed across 2.40+.
Classes¶
PeriodType
¶
Bases: StrEnum
DHIS2 period types — the valid values for DataSet.periodType.
Source code in packages/dhis2w-client/src/dhis2w_client/periods.py
RelativePeriod
¶
Bases: StrEnum
Rolling-window flags on the RelativePeriods model.
Source code in packages/dhis2w-client/src/dhis2w_client/periods.py
PeriodKind
¶
Bases: StrEnum
Coarse classification used by the period-math helpers.
Source code in packages/dhis2w-client/src/dhis2w_client/periods.py
Period
¶
Bases: BaseModel
A parsed DHIS2 absolute period id with its start/end date range.
Source code in packages/dhis2w-client/src/dhis2w_client/periods.py
Functions¶
parse_period(period_id)
¶
Parse a DHIS2 period id (e.g. 202403, 2024W12, 20240315).
Recognises the six absolute kinds: daily, weekly (ISO), monthly,
quarterly, six-monthly, yearly. Raises ValueError for unrecognised
shapes (relative-period names like LAST_12_MONTHS are not absolute
period ids — use RelativePeriod for those).
Source code in packages/dhis2w-client/src/dhis2w_client/periods.py
next_period_id(period_id)
¶
Return the period id immediately following period_id in the same kind.
previous_period_id(period_id)
¶
Return the period id immediately preceding period_id in the same kind.
period_start_end(period_id)
¶
Return (start, end) dates (both inclusive) for a DHIS2 period id.