pythonintermediate
attrs Classes as Immutable Pipeline Records
Use attrs to create fast, validated, immutable record types for data pipeline stage outputs.
pythonPress ⌘/Ctrl + Shift + C to copy
import attr
from datetime import datetime
from typing import Optional
@attr.s(frozen=True, slots=True)
class SaleRecord:
sale_id: int = attr.ib(validator=attr.validators.instance_of(int))
customer: str = attr.ib(validator=attr.validators.min_len(1))
amount: float = attr.ib(validator=attr.validators.gt(0))
currency: str = attr.ib(default='USD')
created_at: datetime = attr.ib(factory=datetime.utcnow)
notes: Optional[str] = attr.ib(default=None)
record = SaleRecord(sale_id=1, customer='Acme Corp', amount=1250.0)
print(attr.asdict(record))Use Cases
- typed pipeline records
- immutable domain models
- data engineering
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
pythonbeginner
Dataclasses as Pipeline Data Models
Use Python dataclasses to define typed, immutable data models passed between pipeline stages.
Best for: typed pipeline stages
#dataclasses#typing
pythonadvanced
Python ETL Pipeline Example
Complete extract-transform-load pipeline with error handling, logging, and incremental processing.
Best for: Automating data ingestion from CSV to warehouse
#etl#pipeline
pythonintermediate
Retry Logic for Data Pipelines
Configurable retry decorator with exponential backoff and jitter for resilient data pipeline tasks.
Best for: Resilient API calls in data pipelines
#retry#resilience
pythonadvanced
Databricks Notebook Data Pipeline
Databricks notebook with Delta Lake reads, transformations, merge operations, and table optimization.
Best for: Medallion architecture data pipelines on Databricks
#databricks#delta-lake