kde_cpi.data package

Top-level data module for CPI processing.

class kde_cpi.data.Area(code, name)[source]

Bases: object

Geographic area metadata from the CPI area lookup table.

Parameters:
code: str
name: str
class kde_cpi.data.CpiDatabaseLoader(dsn=None, schema='public', connection_kwargs=NOTHING)[source]

Bases: object

Persist CPI datasets into PostgreSQL using asyncpg primitives.

Parameters:
async bulk_load(dataset, *, truncate=True)[source]

Copy the full dataset into PostgreSQL, optionally truncating first.

Return type:

None

Parameters:
async close()[source]

Close the open connection, if any.

Return type:

None

async connect(**overrides)[source]

Establish (or reuse) the async connection.

Return type:

Connection

Parameters:

overrides (Any)

connection_kwargs: dict[str, Any]
dsn: str | None
async ensure_schema()[source]

Create the CPI tables if they do not already exist.

Return type:

None

async fetch_dataset()[source]

Reconstruct a Dataset from the current database contents.

Return type:

Dataset

async merge_dataset(dataset)[source]

Synchronize metadata and upsert observations in place.

Return type:

None

Parameters:

dataset (Dataset)

schema: str
async sync_metadata(dataset)[source]

Upsert mapping tables and series definitions without touching observations.

Return type:

None

Parameters:

dataset (Dataset)

async upsert_observations(observations)[source]

Upsert one or more observation rows.

Return type:

None

Parameters:

observations (Iterable[Observation])

class kde_cpi.data.CpiDatasetBuilder(client=NOTHING)[source]

Bases: object

Coordinate retrieval and parsing of CPI datasets from BLS flat files.

Parameters:

client (CpiHttpClient)

client: CpiHttpClient
close()[source]

Close the underlying HTTP client.

Return type:

None

load_current_observations()[source]

Load only the current-year data partition.

Return type:

Dataset

load_dataset(*, data_files=None)[source]

Fetch mapping tables, series definitions, and observations into a dataset.

Return type:

Dataset

Parameters:

data_files (Sequence[str] | None)

class kde_cpi.data.CpiHttpClient(base_url='https://download.bls.gov/pub/time.series/cu/', timeout=30.0, session=NOTHING, headers=NOTHING)[source]

Bases: object

Thin HTTP wrapper around the BLS CPI flat-file endpoints.

Parameters:
base_url: str
close()[source]

Release the underlying HTTP session.

Return type:

None

get_text(filename, *, encoding='utf-8')[source]

Fetch a remote CPI resource and return its decoded text payload.

Return type:

str

Parameters:
  • filename (str)

  • encoding (str)

headers: dict[str, str]
session: Session
timeout: float
class kde_cpi.data.Dataset(areas=NOTHING, items=NOTHING, periods=NOTHING, footnotes=NOTHING, series=NOTHING, observations=NOTHING)[source]

Bases: object

Aggregate CPI dataset containing mapping tables, series, and observations.

Parameters:
add_area(area)[source]

Insert or update an area in the dataset.

Return type:

None

Parameters:

area (Area)

add_footnote(footnote)[source]

Insert or update a footnote definition.

Return type:

None

Parameters:

footnote (Footnote)

add_item(item)[source]

Insert or update an item in the dataset.

Return type:

None

Parameters:

item (Item)

add_period(period)[source]

Insert or update a period definition.

Return type:

None

Parameters:

period (Period)

add_series(series)[source]

Insert or update a series metadata record.

Return type:

None

Parameters:

series (Series)

areas: dict[str, Area]
extend_observations(observations)[source]

Append observation records to the dataset, dropping duplicates.

Return type:

None

Parameters:

observations (Iterable[Observation])

footnotes: dict[str, Footnote]
items: dict[str, Item]
observations: list[Observation]
periods: dict[str, Period]
series: dict[str, Series]
to_dict()[source]

Return a JSON-friendly representation of the dataset.

Return type:

dict[str, object]

class kde_cpi.data.Footnote(code, text)[source]

Bases: object

Footnote reference associated with CPI observations.

Parameters:
code: str
text: str
class kde_cpi.data.Item(code, name, display_level, selectable, sort_sequence)[source]

Bases: object

Item metadata describing CPI product groupings.

Parameters:
  • code (str)

  • name (str)

  • display_level (int)

  • selectable (bool)

  • sort_sequence (int)

code: str
display_level: int
name: str
selectable: bool
sort_sequence: int
class kde_cpi.data.Observation(series_id, year, period, value, footnotes=NOTHING)[source]

Bases: object

Single CPI observation value tied to a series and period.

Parameters:
footnotes: tuple[str, ...]
is_annual()[source]

Return True when the observation corresponds to annual data.

Return type:

bool

period: str
series_id: str
value: Decimal
year: int
class kde_cpi.data.Period(code, abbr, name)[source]

Bases: object

Period metadata describing CPI reporting intervals.

Parameters:
abbr: str
code: str
name: str
class kde_cpi.data.Series(series_id, area_code, item_code, seasonal, periodicity_code, base_code, base_period, begin_year, begin_period, end_year, end_period, *, series_title='')[source]

Bases: object

Metadata describing a CPI series and its structural attributes.

Parameters:
  • series_id (str)

  • area_code (str)

  • item_code (str)

  • seasonal (str)

  • periodicity_code (str)

  • base_code (str)

  • base_period (str)

  • begin_year (int)

  • begin_period (str)

  • end_year (int)

  • end_period (str)

  • series_title (str)

area_code: str
base_code: str
base_period: str
begin_period: str
begin_year: int
end_period: str
end_year: int
is_seasonally_adjusted()[source]

Return True when the series is flagged as seasonally adjusted.

Return type:

bool

item_code: str
periodicity_code: str
seasonal: str
series_id: str
series_title: str
async kde_cpi.data.load_full_history(dsn, *, schema='public', truncate=True, data_files=None)[source]

Load the full CPI history and write it into the database.

Return type:

Dataset

Parameters:
async kde_cpi.data.update_current_periods(dsn, *, schema='public')[source]

Refresh the current-year CPI data without truncating history.

Return type:

Dataset

Parameters:

Submodules