Skip to main content
src/common/contracts.py · chapter Shared rules, file 4 · 534 lines · open in the repository

What it does

Single source of truth for dataset schemas, in code, plus the loaders for the config/ registries that are themselves contracts. This mirrors docs/data-contracts.md. tests/test_contracts.py fails if the two disagree, so always change both in the same commit. Usage:

What is inside

  • ALLOWED_TYPES · line 32: The only type names allowed in contracts (see docs/data-contracts.md).
  • CONTRACTS · line 36: dataset -> {column: type}. Column order here is the order used for Spark schemas and the column order the config/ CSV files must have.
  • KAFKA_TOPIC_PREFIX · line 234: Kafka topic per streamed dataset. One master topic replaced the planned prices and attention topics (decision 0004); the cluster is shared by the class, so the name carries the project prefix.
  • KAFKA_TOPICS · line 235
  • MARKET_WIDE_TICKER · line 238: Ticker value for events that apply to the whole market.
  • DATE_RULE · line 241: Grammar for event_types.date_rule.
  • read_registry(dataset, path) · line 249: Rows of a config/ CSV as dicts, after checking its header against the contract, so a renamed or missing column fails here instead of as a KeyError somewhere later. utf-8-sig also reads files saved by Excel as “CSV UTF-8”, which start with a byte-order mark.
  • load_event_types(path, in_scope_only) · line 263: Read the event registry as a list of rows (dicts keyed by column name).
  • load_retailers(path, in_scope_only) · line 273: Read config/retailers.csv, the stocks the project studies (the market index, common.study.MARKET_TICKER, is not in it). in_scope_only=True drops candidates.
  • price_tickers(retailers_path) · line 280: Every ticker that needs prices: the in-scope retailers, then the market index.
  • load_wiki_articles(path, in_scope_only, retailers_path) · line 285: Read config/wiki_articles.csv: Wikipedia titles per ticker, several per ticker when an article was renamed. in_scope_only=True keeps titles of in-scope retailers only.
  • load_retail_series(path) · line 297: Read config/retail_series.csv: FRED series code -> retail category.
  • check_columns(dataset, actual) · line 302: Compare actual {column: type} pairs with the contract.
  • require_contract(dataset, actual) · line 322: Raise ValueError listing every mismatch. Call it before writing a dataset.
  • _arrow_type_name(arrow_type) · line 329
  • pandas_columns(df) · line 339: {column: contract type} of a pandas DataFrame, as it will be stored in Parquet.
  • parquet_columns(path) · line 350: {column: contract type} of a Parquet file or a folder of them (such as a Spark output), read from the schema only. Subfolder names like batch_id=3 are not columns.
  • spark_schema(dataset) · line 362: Return a pyspark StructType for a dataset (pyspark is imported only here).
  • _WEEKDAY_INDEX · line 379
  • _nth_weekday_of_month(year, month, weekday_name, n) · line 382: The n-th occurrence of weekday_name in (year, month); n=-1 means the last one.
  • _Fixed() · line 403
  • _NthWeekday() · line 408
  • _Offset() · line 414
  • _parse_date_rule(rule) · line 419: The parsed date_rule; None for ‘listed’, whose dates are in event_dates.csv.
  • _resolve_date_for_year(event_type, year, types_by_id, cache, resolving) · line 437
  • expand_events(start_year, end_year, event_types_path, event_dates_path, ticker) · line 469: Expand config/event_types.csv rules and config/event_dates.csv listed dates into one row per event occurrence per year, matching the events contract (event_type, event_date, event_end_date, ticker).
  • spark_columns(df) · line 524: Turn a Spark DataFrame’s schema into {column: contract type} for check_columns.

Connections

Uses: src/common/settings.py, src/common/study.py Used by: etl/prices_download.py, etl/prices_clean.py, etl/pageviews_download.py, etl/pageviews_clean.py, etl/retail_clean.py, etl/build_events.py, etl/join_master.py, scripts/create_topics.py, src/producers/replay_master.py, scripts/peek_topic.py, src/streaming/job.py, src/streaming/signals.py, scripts/check_streaming.py, src/eventstudy/car.py, src/eventstudy/stats.py, src/eventstudy/charts.py, scripts/export_results.py, src/streaming/compare.py, scripts/compare_stream_batch.py, src/ml/features.py, src/ml/train.py, src/dashboard/app.py, src/hosting/snapshot.py, src/hosting/publish_hf.py, tests/test_contracts.py, tests/test_config.py, tests/test_event_calendar.py, tests/test_expand_events.py, tests/test_clean_files.py, tests/test_no_hardcoding.py, tests/test_eventstudy.py, tests/test_compare.py, tests/test_ml_features.py

Source

src/common/contracts.py