Skip to main content

A simple decorator for timing functions.

Project description

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

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

zimer-0.4.0.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

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

zimer-0.4.0-py3-none-any.whl (3.2 kB view details)

Uploaded Python 3

File details

Details for the file zimer-0.4.0.tar.gz.

File metadata

  • Download URL: zimer-0.4.0.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.11

File hashes

Hashes for zimer-0.4.0.tar.gz
Algorithm Hash digest
SHA256 1cdeda2295e0890a70f7e7816dc782224e67aad7fb9431a9e3953f22ffaac21e
MD5 85f4115fdf5075dda70170522116e184
BLAKE2b-256 98c51989f435b20ff50fdf44fb69ce81862e3195663bc929b6fd3839ae81dcfb

See more details on using hashes here.

File details

Details for the file zimer-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: zimer-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 3.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.11

File hashes

Hashes for zimer-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5a1578c2c6c7027ae2c2b517e00c907378b85278ace8aaad34039940cb8a5a73
MD5 301b4db7a5f0da4f28fe54ec9099024d
BLAKE2b-256 b2395b479a7141688c67fa62540537dc51d48f5b88bdb770184b1314a8e5d4b7

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