Skip to main content

Composable data transformation pipeline with lazy evaluation

Project description

philiprehberger-data-pipeline

Tests PyPI version License

Composable data transformation pipeline with lazy evaluation.

Installation

pip install philiprehberger-data-pipeline

Usage

from philiprehberger_data_pipeline import Pipeline

data = [
    {"name": " Alice ", "email": "alice@example.com", "status": "active", "age": 30},
    {"name": "Bob", "email": "bob@example.com", "status": "inactive", "age": 25},
    {"name": "Alice", "email": "alice@example.com", "status": "active", "age": 30},
]

result = (
    Pipeline(data)
    .filter(lambda r: r["status"] == "active")
    .map(lambda r: {**r, "name": r["name"].strip()})
    .unique_by("email")
    .sort_by("name")
    .collect()
)

Reusable Pipelines

clean_users = (
    Pipeline.define()
    .filter(lambda r: r.get("email"))
    .map(lambda r: {**r, "email": r["email"].lower()})
    .unique_by("email")
)

active = clean_users.run(active_users)
archived = clean_users.run(archived_users)

Aggregations

p = Pipeline(sales_data)
total = p.sum("amount")
average = p.avg("amount")
grouped = p.group_by("category")

Export

Pipeline(data).filter(...).to_csv("output.csv")
Pipeline(data).filter(...).to_json("output.json")

API

Function / Class Description
Pipeline(data) Composable, lazy data transformation pipeline
.filter(fn) Keep items where fn returns True
.map(fn) Transform each item
.flat_map(fn) Transform and flatten
.sort_by(key) Sort by key (string or callable)
.unique_by(key) Remove duplicates by key
.take(n) Take first n items
.skip(n) Skip first n items
.chunk(size) Split into chunks
.flatten() Flatten one level of nesting
.collect() Execute and return list
.first() Return first item
.count() Count items
.sum(key) Sum values
.avg(key) Average values
.min(key) Find minimum value
.max(key) Find maximum value
.reduce(fn, initial) Reduce to single value
.group_by(key) Group into dict
.to_csv(path) Export as CSV
.to_json(path) Export as JSON

Development

pip install -e .
python -m pytest tests/ -v

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

philiprehberger_data_pipeline-0.2.5.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

philiprehberger_data_pipeline-0.2.5-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_data_pipeline-0.2.5.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_data_pipeline-0.2.5.tar.gz
Algorithm Hash digest
SHA256 468729add4b4fedd5351c3cd475475e06e8cb50908ece9060394b42827a92ec8
MD5 6de2858a8db755fff56bcd23c4d62131
BLAKE2b-256 2b2367079e225b8c37e4fd073a731541149118cdd9e33f464fa1853667e66249

See more details on using hashes here.

File details

Details for the file philiprehberger_data_pipeline-0.2.5-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_data_pipeline-0.2.5-py3-none-any.whl
Algorithm Hash digest
SHA256 e318cc5bae9f205359ff4a541a54a826aed3158a6bc7f679b37cce99bf5ecb89
MD5 1d4f6e9c3732c1e689d8f9dc60f4ef3e
BLAKE2b-256 26cd0f61bbe0426ea2302dfe972c1a411b57120ac399df5a047e66b2e5c0f6d7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page