Python snippets

Keep your team's Python snippets in one base.

Every Python team re-derives the same helpers — a retry with backoff, a TTL cache, a chunker for bulk inserts — every few sprints, usually in a Slack thread that scrolls away by Friday. The-Snip keeps those patterns in one searchable, reviewed base: snippets with VS Code-grade highlighting, saved API calls stored beside the code that uses them, and Markdown runbooks connected with [[wikilinks]]. Your AI agents get the same base over REST and MCP — they search it before writing new code and file what they learn back, with a human review gate deciding what becomes canon.

Python snippets worth keeping

Retry with exponential backoff

import time, random

def retry(fn, attempts=5, base=0.2):
    for i in range(attempts):
        try:
            return fn()
        except Exception:
            if i == attempts - 1:
                raise
            time.sleep(base * 2**i + random.random() * base)

Wrap a flaky call without a dependency. The jitter term matters: without it, every worker retries on the same schedule and you hammer the recovering service in lockstep.

Chunk an iterable

from itertools import islice

def chunks(it, size):
    it = iter(it)
    while batch := list(islice(it, size)):
        yield batch

Batch rows before a bulk insert or a rate-limited API call. Works on any iterable — generators included — without materializing the whole thing first.

Timed cache (TTL)

import time, functools

def ttl_cache(seconds):
    def deco(fn):
        store = {}
        @functools.wraps(fn)
        def wrap(*a):
            now = time.monotonic()
            if a not in store or now - store[a][1] > seconds:
                store[a] = (fn(*a), now)
            return store[a][0]
        return wrap
    return deco

Memoize a pure function for a few seconds — config lookups, feature flags, token metadata — without pulling in cachetools. time.monotonic() survives clock changes.

Safe dict get by path

def dig(d, *keys, default=None):
    for k in keys:
        if not isinstance(d, dict):
            return default
        d = d.get(k, default)
    return d

Read nested JSON from a third-party API without a pile of try/except or a chain of .get() calls that still explodes when a level is a list.

Bounded asyncio.gather

import asyncio

async def gather_limited(coros, limit=10):
    sem = asyncio.Semaphore(limit)

    async def run(coro):
        async with sem:
            return await coro

    return await asyncio.gather(*(run(c) for c in coros))

gather() with a thousand coroutines will happily open a thousand connections. A semaphore caps concurrency while keeping results in order — the snippet every scraper and backfill script eventually needs.

Questions, answered.

Can my AI agent add Python snippets to the base?

Yes. Agents use the REST API or the hosted MCP server — reads are free on every plan; agent writes (create and update) need the one Pro & Team plan ($8/user/month). Anything an agent creates lands in review first, so a human approves what becomes canon before the team, or other agents, trust it.

Does The-Snip highlight Python properly?

Yes — Python and 35+ other languages, using the same TextMate grammars VS Code uses, rendered server-side so pages stay fast. f-strings, decorators, walrus operators and type hints all tokenize correctly.

How is this better than a #snippets channel in Slack?

Slack scrolls away. Here every snippet has a title, tags and a collection, plus weighted full-text search where title matches outrank body matches — scoped to your workspace. The runbook and the saved API call that go with the code live next to it, cross-linked with [[wikilinks]].

Can I keep a snippet's sample output with it?

Yes — every snippet can carry a sample-output block, so a teammate (or an agent) sees what it produces before running it. For data-wrangling helpers like the ones above, that's often the fastest way to confirm it's the right tool.

What's on the free plan?

25 items, no card — and read-only agent access: any agent can search the base over REST and MCP on Free. The single paid plan — Pro & Team, $8/user/month or $80/user/year (two months free) — adds unlimited items, export, agent writes (create and update), team workspaces and the review workflow.

Start your base — free.

Free: 25 items, no card. Pro & Team: $8/user/mo — unlimited items, REST API, review workflow, and the hosted MCP server.