Reference library

Data pipelines & processing

ETL-style flows, batch transforms, validation, and moving data between formats.

1 match
Data pipelines & processing easy

How to shard output by primary key hash mod N in Python

This code computes a consistent shard index for any primary key string using an MD5 hash mod the number of shards, enabling stable key-based data distribution.

hashing sharding hashlib
Python
import hashlib

def shard_id(primary_key: str, num_shards: int) -> int:
    """Return the shard index for a primary key using MD5 hash mod N."""
    digest = hashlib.md5(primary_key.encode("utf-8")).hexdigest()
    hash_int = int(digest, 16)
    return hash_int % num_shards

if __name__ == "__main__":
    keys = ["use…
11 0 Open

Browse by section

Each section groups closely related Python snippets.

Data pipelines & processing — Python code examples

What you will find here

This page collects data pipelines & processing snippets — short, copy-ready Python you can paste into our free online IDE and run without installing anything. Each sample includes a plain-English explanation and the full source code.

Samples vs tutorials and challenges

Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.