Skip to main content

A foundational toolkit of pure Python utilities and helpers.

Project description

soniclue-basekit

A foundational toolkit of pure Python utilities and helpers.

Installation

pip install soniclue-basekit
# or
uv add soniclue-basekit

Modules

ExecTimer

Times a block of code using wall-clock elapsed time. Works as a context manager or function decorator. Optionally logs elapsed time and fires a post-action callback.

from soniclue_basekit import ExecTimer

# Context manager
with ExecTimer("loading data") as t:
    load_data()
print(t.interval)  # seconds elapsed

# Decorator
@ExecTimer("train step")
def train():
    ...

ExecTimeProfiler

Profiles execution time across named stages within a single thread. Supports nested stages, inner-time exclusion, and summary statistics (mean, std dev).

from soniclue_basekit import ExecTimeProfiler

profiler = ExecTimeProfiler()
with profiler.stage("preprocess"):
    ...
with profiler.stage("inference"):
    ...
profiler.print_report()

Attempter

Wraps a callable and re-invokes it on failure according to a pluggable retry strategy. Raise NoMoreAttempt inside the callable to abort retrying immediately.

from soniclue_basekit import Attempter, ExponentialBackoff, NoMoreAttempt

def fetch():
    resp = requests.get(url)
    if resp.status_code == 404:
        raise NoMoreAttempt()  # no point retrying
    resp.raise_for_status()
    return resp.json()

result = Attempter(fetch).execute(ExponentialBackoff(base=1.0, multiplier=2.0), max_attempts=5)

Retry strategies:

Class Behaviour
ConstantPause(interval) Fixed wait between retries
RandomPause(low, high) Uniform random wait in [low, high]
ExponentialBackoff(base, multiplier, jitter) base × multiplierᵃᵗᵗᵉᵐᵖᵗ + optional random jitter

LockProtector

Wraps an object with a threading.Lock. Supports enforcing lock acquisition order across protectors and optional profiling of contention.

from soniclue_basekit import LockProtector

state = LockProtector({"count": 0})
with state.acquire() as s:
    s["count"] += 1

DaemonThread / TerminableThread

Thread base classes with graceful shutdown support. TerminableThread exposes is_terminating() / set_terminating(). DaemonThread adds periodic-task scaffolding and optional OS-level thread naming (Linux).

from soniclue_basekit import DaemonThread

class Poller(DaemonThread):
    def _run_once(self):
        check_queue()

poller = Poller(interval_sec=5, name="poller")
poller.start()
...
poller.set_terminating()
poller.join()

ScriptRunner

Runs shell scripts (written as Python strings) in a subprocess with optional timeout, stdout/stderr capture, and streaming line callbacks.

from soniclue_basekit import ScriptRunner

runner = ScriptRunner("echo hello && echo world")
runner.run()

CliFormatter

Fluent builder for CLI argument strings passed to subprocess. Handles shell-quoting, key prefixes, and multi-value flags.

from soniclue_basekit import CliFormatter

cmd = (
    CliFormatter("ffmpeg")
    .add("-i", input_path)
    .add("-vf", "scale=1280:720")
    .add("-y", output_path)
    .build()
)

run_in_process / run_in_process_decorator

Run a callable in an isolated subprocess and return its result (or re-raise its exception) in the calling process. Useful for isolating GPU memory, avoiding fork-safety issues, or enforcing timeouts.

from soniclue_basekit import run_in_process, run_in_process_decorator

result = run_in_process(my_fn, timeout_sec=30)

@run_in_process_decorator
def heavy_job(x):
    return x * 2

is_pytest_env

Returns True when running inside a pytest session — handy for guards that should only activate during tests.

from soniclue_basekit import is_pytest_env

Development

Requires uv.

Run tests

uv run pytest

Lint

uv run ruff check src tests

Format

uv run ruff format src tests

License

Apache 2.0 — see LICENSE.

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

soniclue_basekit-0.1.0.tar.gz (29.6 kB view details)

Uploaded Source

Built Distribution

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

soniclue_basekit-0.1.0-py3-none-any.whl (25.2 kB view details)

Uploaded Python 3

File details

Details for the file soniclue_basekit-0.1.0.tar.gz.

File metadata

  • Download URL: soniclue_basekit-0.1.0.tar.gz
  • Upload date:
  • Size: 29.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for soniclue_basekit-0.1.0.tar.gz
Algorithm Hash digest
SHA256 42d6dea922d99bd95c2d2d756a763c02662a942ef39b25809930d52b994cc3e4
MD5 59dd3f4d880b09e936f64e45a58e6236
BLAKE2b-256 71c1d89b97e7a4410b28c60d9348811a4afbe3645a63705d8c2e34a7faa8f608

See more details on using hashes here.

File details

Details for the file soniclue_basekit-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: soniclue_basekit-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 25.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for soniclue_basekit-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 db5cf2a26e1e865963f8e84483645c04b050b4a166adb2b38c04e494e84fb060
MD5 c50ae7af992c8df9291a9f77db06e34b
BLAKE2b-256 713271547743e7e6e977213eeb231ee5de9ef55a77d2922761250e3b192239ba

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