Skip to main content

zimer

A lightweight utility package that ships two decorators that make everyday Python development a bit easier:

  • zimer – measure the execution time of synchronous and asynchronous functions, optionally repeating them and printing the average runtime.
  • with_retry – automatically retry a function when it raises an exception, with optional exponential back-off. Works with both sync and async callables.

Installation

pip install zimer

zimer – timing decorator

from zimer import zimer
import time, asyncio

@zimer                      # one run, prints elapsed time
def slow_sync():
    time.sleep(1)

@zimer(repeats=3)           # three runs, prints average
async def slow_async():
    await asyncio.sleep(0.5)

slow_sync()
asyncio.run(slow_async())

with_retry – retry decorator

from zimer import with_retry
import random, asyncio

# --- synchronous example -----------------------------------------
@with_retry(num_retries=3, backoff=1, backoff_exponent=2)  # 1s, 4s between tries
def flaky_sync():
    if random.random() < 0.7:
        raise ValueError("Still broken …")
    return "✓ finally worked"

# --- asynchronous example ----------------------------------------
@with_retry                # default: 5 retries, no back-off
async def flaky_async():
    if random.random() < 0.5:
        raise RuntimeError("Try again …")
    return "✓ async worked"

print(flaky_sync())
print(asyncio.run(flaky_async()))

Arguments:

  • num_retries – total attempts (initial call + retries). Must be ≥ 1.
  • backoff – initial pause between retries (seconds). Must be ≥ 0.
  • backoff_exponent – growth factor applied to the retry count to compute the wait time: sleep = backoff * ((attempt + 1) ** backoff_exponent).

Testing

Clone the repo and run:

pip install -r requirements-dev.txt  # pytest, etc.
pytest -q

All unit tests live under tests/ and cover both decorators.


License

MIT © 2024 Zubin Aysola

Metadata

Release files for zimer 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for zimer 0.4.0
File Size Uploaded
zimer-0.4.0.tar.gz 3.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for zimer 0.4.0
File Interpreter ABI Platform
zimer-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 7.0 kB

Release files / zimer-0.4.0.tar.gz

Download URL zimer-0.4.0.tar.gz
Size 3.8 kB
Tags Source
SHA-256 checksum
How to use checksums
1cdeda2295e0890a70f7e7816dc782224e67aad7fb9431a9e3953f22ffaac21e
BLAKE2b-256 checksum
How to use checksums
98c51989f435b20ff50fdf44fb69ce81862e3195663bc929b6fd3839ae81dcfb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.11

Release files / zimer-0.4.0-py3-none-any.whl

Download URL zimer-0.4.0-py3-none-any.whl
Size 3.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5a1578c2c6c7027ae2c2b517e00c907378b85278ace8aaad34039940cb8a5a73
BLAKE2b-256 checksum
How to use checksums
b2395b479a7141688c67fa62540537dc51d48f5b88bdb770184b1314a8e5d4b7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.11

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page