Python
Python is the dominant language in data science because it connects numerical libraries, notebooks, orchestration, and service code. Production Python needs stronger boundaries than exploratory work: isolated environments, typed interfaces, deterministic functions, explicit configuration, and testing around data and service contracts.
Modules, configuration, and typed boundaries
Keep notebook exploration outside importable library code. Put reusable transformations in modules, expose training and backfill workflows through CLIs, keep configuration in typed objects, and make database access explicit through sql adapters. Type hints do not prove correctness, but they make API design reviewable at module boundaries.
Typed configuration boundary
This snippet parses a typed dataclass configuration and raises a validation error when the feature window is invalid.
from dataclasses import dataclass
@dataclass(frozen=True)
class FeatureConfig:
name: str
window_minutes: int
default: float = 0.0
def parse_config(raw: dict) -> FeatureConfig:
cfg = FeatureConfig(**raw)
if cfg.window_minutes <= 0:
raise ValueError("window_minutes must be positive")
return cfg
print(parse_config({"name": "tickets_last_60m", "window_minutes": 60}))
try:
parse_config({"name": "bad", "window_minutes": 0})
except ValueError as exc:
print(type(exc).__name__, str(exc))Observed output:
FeatureConfig(name='tickets_last_60m', window_minutes=60, default=0.0)
ValueError window_minutes must be positiveThe dataclass gives a small immutable configuration contract. In a service, this object can be tested without reading environment variables or starting a framework. In a mixed stack, keep this boundary as explicit as the state boundary in javascript application architecture.
Failure modes
Common failures are hidden global state, unpinned dependencies, imports that depend on the current working directory, mutable default values, and code paths that silently change dtype or timezone semantics. During refactoring, protect behavior with golden examples before moving notebook logic into modules.
References
- Python tutorial: Virtual Environments and Packages
- Python documentation: dataclasses
- Python documentation: typing
Nav
Section — Software Engineering