Skip to main content

braincraft

License: MIT Version

A workshop of small, sharp utilities — carefully shaped helpers you reuse across projects to keep everyday coding tasks fast, tidy, and consistent.

Prerequisites

  • Python >=3.14

Installation

Install via pip:

pip install braincraft

Or add it as a Poetry dependency:

poetry add braincraft

Components

graph TD
    A[braincraft] --> B[ignorefile]
    A --> C[retry]
    B --> B1["IgnoreFile — gitignore-style path matching"]
    B --> B2["PatternHandler — extensible custom pattern handlers"]
    C --> C1["retry_rand_exp — async retry with full-jitter back-off"]
Module Exported symbols Purpose
ignorefile IgnoreFile, PatternHandler Gitignore-style ignore-file parsing with extensible handlers
retry retry_rand_exp Async retry with full-jitter exponential back-off

Usage

IgnoreFile

Reads a gitignore-style ignore file and determines whether a given path should be ignored. Pattern matching follows the full gitignore specification: *, ?, [...] wildcards, negation (!), directory-only patterns (trailing /), ** double-star rules, and anchoring.

Anchored patterns (containing / at the start or middle, e.g. doc/build or /dist) are matched relative to a base directory — by default the current working directory at the time IgnoreFile is created. An explicit base_dir (str | Path) can be supplied to override this. The ignore file can live anywhere, independently of base_dir.

Matching always occurs — no error is raised for paths outside the base directory.

from pathlib import Path
from braincraft import IgnoreFile

ig = IgnoreFile(Path(".gitignore"))

print(ig.is_ignored(Path("dist/output.js")))       # True
print(ig.is_ignored(Path("src/main.py")))          # False
print(ig.is_ignored(Path("build/")))               # True (if build/ is a directory)

Custom base directory

By default IgnoreFile uses the current working directory as the root for anchored patterns. Pass base_dir (str or Path) to pin matching to a specific directory regardless of where the process is running or where the ignore file lives.

from pathlib import Path
from braincraft import IgnoreFile

project = Path("/srv/myproject")
ig = IgnoreFile(project / ".gitignore", base_dir=project)

# Anchored pattern /dist matches relative to project, not the process CWD
print(ig.is_ignored(project / "dist" / "bundle.js"))   # True
print(ig.is_ignored(project / "src" / "main.py"))      # False

# Works equally well with plain strings
ig2 = IgnoreFile("/srv/myproject/.gitignore", base_dir="/srv/myproject")
print(ig2.is_ignored("/srv/myproject/dist/bundle.js"))  # True

Custom pattern handlers

Extend matching behaviour by registering a PatternHandler subclass. Custom handlers are consulted first; returning None falls through to the built-in gitignore handler.

from pathlib import Path
from braincraft import IgnoreFile, PatternHandler


class SizePatternHandler(PatternHandler):
    """Ignore files larger than a size encoded as 'size:>NNN' in the ignore file."""

    def matches(self, pattern: str, path: Path, base_dir: Path) -> bool | None:
        if not pattern.startswith("size:>"):
            return None  # not our pattern — let the built-in handle it
        limit = int(pattern.removeprefix("size:>"))
        if path.is_file():
            return path.stat().st_size > limit
        return None


ig = IgnoreFile(Path(".myignore"))
ig.register_handler(SizePatternHandler())

print(ig.is_ignored(Path("huge_dump.bin")))  # True if file > limit

retry_rand_exp

Calls an async coroutine with automatic retry and full-jitter exponential back-off. Retries on any exception up to max_attempts times, sleeping a random jittered duration between attempts. Re-raises the last exception when all attempts are exhausted.

from braincraft import retry_rand_exp

async def fetch_data(url: str) -> str:
    # your async operation here
    ...

result = await retry_rand_exp(
    fetch_data,
    "https://example.com/api",
    max_attempts=5,
    base_delay=1.0,
    max_delay=30.0,
)

Development

Prerequisites

Install dependencies

poetry install

Format and lint

poetry run black braincraft; poetry run pylint braincraft

Run tests with coverage

poetry run pytest --cov=braincraft tests --cov-report html

Changelog

See CHANGELOG.md for a full history of changes.

License

This project is licensed under the MIT License — see the LICENSE file for details.

Author

Ron Webb

Download files

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

Source Distribution

braincraft-1.2.0.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

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

braincraft-1.2.0-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file braincraft-1.2.0.tar.gz.

File metadata

  • Download URL: braincraft-1.2.0.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.0 CPython/3.14.6 Linux/6.17.0-1018-azure

File hashes

Hashes for braincraft-1.2.0.tar.gz
Algorithm Hash digest
SHA256 d367230ebcf1e9672e5d3fdcc59e0cecd0d1349680c0a244ee542157ac2f3f03
MD5 0ea559d5c621c2b87fd37704e9ea5727
BLAKE2b-256 4de68712f0aec0138fdee7201e68e6f31c6500962241cfd2bc765b262ee92f42

See more details on using hashes here.

File details

Details for the file braincraft-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: braincraft-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 11.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.0 CPython/3.14.6 Linux/6.17.0-1018-azure

File hashes

Hashes for braincraft-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 23f8559a89c70cb6edb903d9e895d1c1405ab02bd32a79e953886226204e2149
MD5 1bb5ea6b2a66d3faae286f9a422657b5
BLAKE2b-256 14549ad2bead1de30154345f8b244186600a6f33840154cd5f55af4b7a07aa9a

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 Sentry Error logging StatusPage Status page