Claude Code for toolz: Functional Python Utilities — Claude Skills 360 Blog
Blog / AI / Claude Code for toolz: Functional Python Utilities
AI

Claude Code for toolz: Functional Python Utilities

Published: April 14, 2028
Read time: 5 min read
By: Claude Skills 360

toolz provides functional utilities for Python — function composition, currying, and sequence/dict operations. pip install toolz. Pipe: from toolz import pipe; pipe(data, step1, step2, step3). Compose: from toolz import compose; f = compose(str, abs, int). Thread: from toolz import thread_last; thread_last(data, str.strip, str.lower). Curry: from toolz import curry; add = curry(lambda x, y: x+y); inc = add(1). Groupby: from toolz import groupby; groupby(len, ["cat","dog","frog"]). Countby: countby(len, ["cat","dog","frog"]){3:2, 4:1}. Reduceby: reduceby(key_fn, binop, seq). Valmap: from toolz import valmap; valmap(str.upper, d). Keymap: keymap(str.lower, d). Itemmap: itemmap(lambda kv: (kv[0], kv[1]*2), d). Merge: from toolz import merge; merge(d1, d2). Merge_with: merge_with(sum, {"a":1}, {"a":2}){"a":3}. Take: from toolz import take; list(take(3, seq)). Drop: list(drop(2, seq)). Partition: partition_all(3, range(10)) → chunks of 3. Sliding_window: sliding_window(3, range(5)). Concat: concat([list1, list2]). Interleave: interleave([[1,2],[3,4]]). Juxt: from toolz import juxt; f = juxt(min, max, sum); f([1,2,3])(1,3,6). Memoize: from toolz import memoize; @memoize. Frequencies: from toolz import frequencies; frequencies("aabb"). Unique: unique([1,2,1,3]). Pluck: pluck("name", records). Get: get(["a","b"], d). Claude Code generates toolz pipelines, data transformation functions, and functional aggregation helpers.

CLAUDE.md for toolz

## toolz Stack
- Version: toolz >= 0.12 | pip install toolz  (cytoolz for C-speed: pip install cytoolz)
- Pipe: pipe(data, fn1, fn2, fn3) — left-to-right function chaining
- Compose: compose(fn3, fn2, fn1) — right-to-left (f(g(h(x))))
- Curry: curry(fn)(arg1) → partial | @curry decorator
- Dict ops: merge(d1,d2) | valmap/keymap | merge_with(reducer, d1, d2)
- Seq ops: groupby/countby/reduceby | take/drop/partition_all/sliding_window
- Utils: memoize | juxt | frequencies | unique | pluck | get

toolz Functional Pipeline

# app/functional.py — toolz pipe, compose, curry, groupby, merge, and sequence ops
from __future__ import annotations

from typing import Any, Callable, Iterable

from toolz import (
    compose,
    concat,
    countby,
    curry,
    drop,
    frequencies,
    get,
    groupby,
    interleave,
    interpose,
    juxt,
    keyfilter,
    keymap,
    memoize,
    merge,
    merge_with,
    partition_all,
    pipe,
    pluck,
    reduceby,
    sliding_window,
    take,
    thread_last,
    unique,
    valfilter,
    valmap,
)


# ─────────────────────────────────────────────────────────────────────────────
# 1. Pipeline helpers
# ─────────────────────────────────────────────────────────────────────────────

def make_pipeline(*fns: Callable) -> Callable:
    """
    Build a reusable left-to-right function pipeline.

    Example:
        clean = make_pipeline(str.strip, str.lower, lambda s: s.replace("-", "_"))
        clean("  Hello-World  ")  → "hello_world"
    """
    def pipeline(value: Any) -> Any:
        return pipe(value, *fns)
    return pipeline


def compose_validators(*predicates: Callable) -> Callable:
    """
    Return a function that passes iff all predicates return True.
    Short-circuits on first False.
    """
    def all_pass(value: Any) -> bool:
        return all(p(value) for p in predicates)
    return all_pass


# ─────────────────────────────────────────────────────────────────────────────
# 2. Curried helpers
# ─────────────────────────────────────────────────────────────────────────────

@curry
def safe_get(key: Any, default: Any, mapping: dict) -> Any:
    """Curried dict .get(key, default)."""
    return mapping.get(key, default)


@curry
def field(attr: str, obj: Any) -> Any:
    """Curried attribute/key accessor (works on dicts and objects)."""
    if isinstance(obj, dict):
        return obj.get(attr)
    return getattr(obj, attr, None)


@curry
def transform(fn: Callable, items: Iterable) -> list:
    """Curried map: apply fn to every item, return list."""
    return list(map(fn, items))


@curry
def keep(predicate: Callable, items: Iterable) -> list:
    """Curried filter: keep items where predicate(item) is truthy."""
    return list(filter(predicate, items))


@curry
def reject(predicate: Callable, items: Iterable) -> list:
    """Keep items where predicate is False (opposite of keep)."""
    return [x for x in items if not predicate(x)]


# ─────────────────────────────────────────────────────────────────────────────
# 3. Sequence operations
# ─────────────────────────────────────────────────────────────────────────────

def chunks(seq: Iterable, size: int) -> list[list]:
    """Split seq into chunks of size. Last chunk may be smaller."""
    return [list(c) for c in partition_all(size, seq)]


def windows(seq: Iterable, size: int) -> list[tuple]:
    """Return all overlapping windows of length size."""
    return list(sliding_window(size, seq))


def flatten(seq: Iterable) -> list:
    """Flatten one level of nesting."""
    return list(concat(seq))


def interleave_seqs(*seqs: Iterable) -> list:
    """Interleave multiple sequences element-by-element."""
    return list(interleave(seqs))


def first_n(seq: Iterable, n: int) -> list:
    """Return the first n items."""
    return list(take(n, seq))


def skip_n(seq: Iterable, n: int) -> list:
    """Return seq after skipping the first n items."""
    return list(drop(n, seq))


def unique_by(seq: Iterable, key: Callable) -> list:
    """Return unique items by key function (preserves order)."""
    return list(unique(seq, key=key))


def intersperse(sep: Any, seq: Iterable) -> list:
    """Insert sep between every pair of items."""
    return list(interpose(sep, seq))


def running_windows(seq: list, fn: Callable, size: int) -> list:
    """Apply fn to each sliding window, return list of results."""
    return [fn(window) for window in windows(seq, size)]


# ─────────────────────────────────────────────────────────────────────────────
# 4. Dict operations
# ─────────────────────────────────────────────────────────────────────────────

def deep_merge(*dicts: dict) -> dict:
    """Merge dicts left-to-right; last value wins for conflicting keys."""
    return merge(*dicts)


def merge_sum(*dicts: dict) -> dict:
    """Merge dicts, summing values for shared keys."""
    return merge_with(sum, *dicts)


def merge_lists(*dicts: dict) -> dict:
    """Merge dicts, concatenating list values for shared keys."""
    return merge_with(lambda a, b: (a if isinstance(a, list) else [a]) +
                                   (b if isinstance(b, list) else [b]),
                      *dicts)


def transform_values(d: dict, fn: Callable) -> dict:
    """Apply fn to every value."""
    return valmap(fn, d)


def transform_keys(d: dict, fn: Callable) -> dict:
    """Apply fn to every key."""
    return keymap(fn, d)


def filter_values(d: dict, predicate: Callable) -> dict:
    """Keep entries where predicate(value) is True."""
    return valfilter(predicate, d)


def filter_keys(d: dict, predicate: Callable) -> dict:
    """Keep entries where predicate(key) is True."""
    return keyfilter(predicate, d)


def pick(d: dict, keys: list) -> dict:
    """Extract a subset of keys from d."""
    return keyfilter(lambda k: k in set(keys), d)


def omit(d: dict, keys: list) -> dict:
    """Return d without the specified keys."""
    ks = set(keys)
    return keyfilter(lambda k: k not in ks, d)


# ─────────────────────────────────────────────────────────────────────────────
# 5. Aggregation
# ─────────────────────────────────────────────────────────────────────────────

def group(items: Iterable, key: Callable) -> dict[Any, list]:
    """Group items by key function."""
    return dict(groupby(key, items))


def count(items: Iterable, key: Callable) -> dict[Any, int]:
    """Count items by key function."""
    return dict(countby(key, items))


def reduce_by(items: Iterable, key: Callable, binop: Callable, init: Any = None) -> dict:
    """Reduce items grouped by key."""
    if init is not None:
        return dict(reduceby(key, binop, items, init))
    return dict(reduceby(key, binop, items))


def word_count(text: str) -> dict[str, int]:
    """Count word frequencies in a string."""
    words = text.lower().split()
    return dict(frequencies(words))


def pluck_field(records: Iterable[dict], field_name: str) -> list:
    """Extract field_name from every record."""
    return list(pluck(field_name, records))


def by_field(records: Iterable[dict], field_name: str) -> dict:
    """Index records by field_name."""
    return group(records, lambda r: r.get(field_name))


# ─────────────────────────────────────────────────────────────────────────────
# 6. Memoization
# ─────────────────────────────────────────────────────────────────────────────

def memoized(fn: Callable, cache: dict | None = None) -> Callable:
    """Wrap fn with toolz memoize. Optionally pass an explicit cache dict."""
    if cache is not None:
        return memoize(fn, cache=cache)
    return memoize(fn)


# ─────────────────────────────────────────────────────────────────────────────
# Demo
# ─────────────────────────────────────────────────────────────────────────────

if __name__ == "__main__":
    print("=== pipe ===")
    result = pipe(
        "  Hello, World!  ",
        str.strip,
        str.lower,
        lambda s: s.replace(",", ""),
    )
    print("pipe result:", result)

    print("\n=== make_pipeline ===")
    normalize = make_pipeline(str.strip, str.lower)
    print("pipeline:", [normalize(x) for x in ["  ALPHA  ", "beta ", " GAMMA"]])

    print("\n=== curry + transform ===")
    double = transform(lambda x: x * 2)
    print("double:", double([1, 2, 3, 4, 5]))

    print("\n=== chunks ===")
    print("chunks:", chunks(range(10), 3))

    print("\n=== windows ===")
    temps = [22, 24, 21, 25, 23, 26]
    moving_avg = running_windows(temps, lambda w: sum(w)/len(w), 3)
    print("3-day moving avg:", [round(v, 1) for v in moving_avg])

    print("\n=== group / count ===")
    words = ["apple", "banana", "avocado", "blueberry", "cherry", "apricot"]
    by_first = group(words, lambda w: w[0])
    cnt = count(words, lambda w: w[0])
    print("group by first:", {k: v for k, v in by_first.items()})
    print("count by first:", dict(cnt))

    print("\n=== merge ops ===")
    a = {"x": 1, "y": 2}
    b = {"y": 3, "z": 4}
    print("merge:      ", deep_merge(a, b))
    print("merge_sum:  ", merge_sum(a, b))

    print("\n=== dict transform ===")
    d = {"Name": "Alice", "Age": 30, "Score": None}
    print("lower keys:", transform_keys(d, str.lower))
    print("no nulls:  ", filter_values(d, lambda v: v is not None))
    print("pick:      ", pick(d, ["Name", "Score"]))

    print("\n=== word_count ===")
    text = "the cat sat on the mat the cat"
    print("word freq:", word_count(text))

    print("\n=== memoize ===")
    call_count = [0]
    def slow_fn(n):
        call_count[0] += 1
        return n * n
    fast_fn = memoized(slow_fn)
    _ = [fast_fn(x) for x in [2, 3, 2, 4, 3, 2]]
    print(f"calls: {call_count[0]} (vs 6 without memo)")

    print("\n=== juxt ===")
    stats = juxt(min, max, sum, len)
    data = [4, 7, 2, 9, 1, 6]
    mn, mx, total, count_val = stats(data)
    print(f"min={mn} max={mx} sum={total} count={count_val} mean={total/count_val:.1f}")

For the funcy alternative — funcy is a similar functional utility library with a partly overlapping API; toolz has a sister library cytoolz (C extension, same API but 2–5× faster) and is the more commonly cited functional Python toolkit; both are valid, but toolz + cytoolz has broader production adoption for data pipeline work. For the itertools / functools alternative — the stdlib itertools and functools cover many of the same operations, but toolz provides a unified API, consistent currying style, and dict utilities (valmap, merge_with) that the stdlib doesn’t offer in one place. The Claude Skills 360 bundle includes toolz skill sets covering pipe/compose pipelines, @curry decorator, make_pipeline/compose_validators, safe_get/field/transform/keep curry helpers, chunks/windows/flatten/unique_by/intersperse, group/count/reduce_by aggregation, transform_values/transform_keys/filter_values/filter_keys/pick/omit dict ops, merge_sum/merge_lists/deep_merge, word_count/pluck_field/by_field, memoized() caching, and juxt multi-function dispatch. Start with the free tier to try functional Python code generation.

Keep Reading

AI

Claude Code for email.contentmanager: Python Email Content Accessors

Read and write EmailMessage body content with Python's email.contentmanager module and Claude Code — email contentmanager ContentManager for the class that maps content types to get and set handler functions allowing EmailMessage to support get_content and set_content with type-specific behaviour, email contentmanager raw_data_manager for the ContentManager instance that handles raw bytes and str payloads without any conversion, email contentmanager content_manager for the standard ContentManager instance used by email.policy.default that intelligently handles text plain text html multipart and binary content types, email contentmanager get_content_text for the handler that returns the decoded text payload of a text-star message part as a str, email contentmanager get_content_binary for the handler that returns the raw decoded bytes payload of a non-text message part, email contentmanager get_data_manager for the get-handler lookup used by EmailMessage get_content to find the right reader function for the content type, email contentmanager set_content text for the handler that creates and sets a text part correctly choosing charset and transfer encoding, email contentmanager set_content bytes for the handler that creates and sets a binary part with base64 encoding and optional filename Content-Disposition, email contentmanager EmailMessage get_content for the method that reads the message body using the registered content manager handlers, email contentmanager EmailMessage set_content for the method that sets the message body and MIME headers in one call, email contentmanager EmailMessage make_alternative make_mixed make_related for the methods that convert a simple message into a multipart container, email contentmanager EmailMessage add_attachment for the method that attaches a file or bytes to a multipart message, and email contentmanager integration with email.message and email.policy and email.mime and io for building high-level email readers attachment extractors text body accessors HTML readers and policy-aware MIME construction pipelines.

5 min read Feb 12, 2029
AI

Claude Code for email.charset: Python Email Charset Encoding

Control header and body encoding for international email with Python's email.charset module and Claude Code — email charset Charset for the class that wraps a character set name with the encoding rules for header encoding and body encoding describing how to encode text for that charset in email messages, email charset Charset header_encoding for the attribute specifying whether headers using this charset should use QP quoted-printable encoding BASE64 encoding or no encoding, email charset Charset body_encoding for the attribute specifying the Content-Transfer-Encoding to use for message bodies in this charset such as QP or BASE64, email charset Charset output_codec for the attribute giving the Python codec name used to encode the string to bytes for the wire format, email charset Charset input_codec for the attribute giving the Python codec name used to decode incoming bytes to str, email charset Charset get_output_charset for returning the output charset name, email charset Charset header_encode for encoding a header string using the charset's header_encoding method, email charset Charset body_encode for encoding body content using the charset's body_encoding, email charset Charset convert for converting a string from the input_codec to the output_codec, email charset add_charset for registering a new charset with custom encoding rules in the global charset registry, email charset add_alias for adding an alias name that maps to an existing registered charset, email charset add_codec for registering a codec name mapping for use by the charset machinery, and email charset integration with email.message and email.mime and email.policy and email.encoders for building international email senders non-ASCII header encoders Content-Transfer-Encoding selectors charset-aware message constructors and MIME encoding pipelines.

5 min read Feb 11, 2029
AI

Claude Code for email.utils: Python Email Address and Header Utilities

Parse and format RFC 2822 email addresses and dates with Python's email.utils module and Claude Code — email utils parseaddr for splitting a display-name plus angle-bracket address string into a realname and email address tuple, email utils formataddr for combining a realname and address string into a properly quoted RFC 2822 address with angle brackets, email utils getaddresses for parsing a list of raw address header strings each potentially containing multiple comma-separated addresses into a list of realname address tuples, email utils parsedate for parsing an RFC 2822 date string into a nine-tuple compatible with time.mktime, email utils parsedate_tz for parsing an RFC 2822 date string into a ten-tuple that includes the UTC offset timezone in seconds, email utils parsedate_to_datetime for parsing an RFC 2822 date string into an aware datetime object with timezone, email utils formatdate for formatting a POSIX timestamp or the current time as an RFC 2822 date string with optional usegmt and localtime flags, email utils format_datetime for formatting a datetime object as an RFC 2822 date string, email utils make_msgid for generating a globally unique Message-ID string with optional idstring and domain components, email utils decode_rfc2231 for decoding an RFC 2231 encoded parameter value into a tuple of charset language and value, email utils encode_rfc2231 for encoding a string as an RFC 2231 encoded parameter value, email utils collapse_rfc2231_value for collapsing a decoded RFC 2231 tuple to a Unicode string, and email utils integration with email.message and email.headerregistry and datetime and time for building address parsers date formatters message-id generators header extractors and RFC-compliant email construction utilities.

5 min read Feb 10, 2029

Put these ideas into practice

Claude Skills 360 gives you production-ready skills for everything in this article — and 2,350+ more. Start free or go all-in.

Back to Blog

Get 360 skills free