Add BLS client library, example scripts, and usage docs
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
43
examples/basic_pull.py
Normal file
43
examples/basic_pull.py
Normal file
@ -0,0 +1,43 @@
|
||||
"""
|
||||
Basic example — fetch a few series and print the latest values.
|
||||
Run from the bls/ directory: python3 examples/basic_pull.py
|
||||
"""
|
||||
|
||||
import sys, os
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
|
||||
|
||||
from config import BLS_API_KEY
|
||||
from bls_client import BLSClient
|
||||
from bls_client.queries import employment, prices, wages
|
||||
|
||||
client = BLSClient(BLS_API_KEY)
|
||||
|
||||
# ── Single series ────────────────────────────────────────────────────────────
|
||||
print("=== National Nonfarm Payrolls ===")
|
||||
result = client.fetch_latest(employment.nonfarm_payrolls(), years=1)
|
||||
for sid, s in result.items():
|
||||
obs = client.latest_obs(s)
|
||||
print(f" {obs['periodName']} {obs['year']}: {float(obs['value']):,.0f} thousand jobs")
|
||||
|
||||
# ── Named dict pull ──────────────────────────────────────────────────────────
|
||||
print("\n=== CPI Dashboard (latest month) ===")
|
||||
results = client.fetch_named(prices.cpi_dashboard(), 2025, 2026)
|
||||
for label, s in results.items():
|
||||
obs = client.latest_obs(s)
|
||||
if obs:
|
||||
print(f" {label:<30} {obs['value']:>8} ({obs['periodName']} {obs['year']})")
|
||||
|
||||
# ── DC region unemployment ───────────────────────────────────────────────────
|
||||
print("\n=== DC Region Unemployment Rates ===")
|
||||
results = client.fetch_named(employment.dc_region_unemployment(), 2025, 2026)
|
||||
for label, s in results.items():
|
||||
obs = client.latest_obs(s)
|
||||
if obs:
|
||||
print(f" {label:<20} {obs['value']}% ({obs['periodName']} {obs['year']})")
|
||||
|
||||
# ── Flatten to rows ──────────────────────────────────────────────────────────
|
||||
print("\n=== Raw rows (first 3) ===")
|
||||
result = client.fetch_latest(employment.nonfarm_payrolls(), years=1, catalog=True)
|
||||
rows = client.to_rows(result)
|
||||
for row in rows[:3]:
|
||||
print(f" {row}")
|
||||
Reference in New Issue
Block a user