Skip to main content

A collection of Python utilities for timing, concurrency control, thread safety, and design patterns.

Project description

Vextra Utils

I built this because I use the following decorators or singletons in almost all of my code.

Installation

uv add vextra_utils

Decorators

with_timer

Measures and logs execution time of sync or async functions.

from vextra_utils import with_timer

@with_timer
def foo():
    return bar # logs: "foo took 1.2345s"

@with_timer
async def foo():
    return bar # logs: "foo took 0.5678s"

with_semaphore

Limits concurrent execution of async functions using a semaphore.

import asyncio
from vextra_utils import with_semaphore

semaphore = asyncio.Semaphore(3)  # max 3 concurrent

@with_semaphore(semaphore)
async def fetch_url(url: str) -> str:
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as resp:
            return await resp.text()

# Only 3 requests run concurrently
await asyncio.gather(*[fetch_url(url) for url in urls])

synchronized

Makes function execution thread-safe.

from threading import Lock
from vextra_utils import synchronized

# With shared lock
shared_lock = Lock()

@synchronized(shared_lock)
def update_counter():
    # thread-safe critical section
    pass

# Without lock (creates RLock automatically)
@synchronized()
def another_safe_function():
    pass

Metaclasses

SingletonMeta

Thread-safe singleton pattern implementation.

from vextra_utils import SingletonMeta

class Database(metaclass=SingletonMeta):
    def __init__(self, connection_string: str):
        self.connection = connect(connection_string)

db1 = Database("postgres://...")
db2 = Database("different://...")  # ignored, returns same instance

assert db1 is db2  # True

Clear instances for testing:

SingletonMeta.clear_instances()

API Reference

Utility Type Description
with_timer Decorator Logs execution time for sync/async functions
with_semaphore(sem) Decorator Limits async concurrency with semaphore
synchronized(lock?) Decorator Thread-safe execution with optional lock
SingletonMeta Metaclass Thread-safe singleton pattern

Requirements

  • Python 3.13+
  • No runtime dependencies
  • Optional: loguru for enhanced logging (falls back to stdlib logging)

License

MIT


Tests generated by Claude xD

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

vextra_utils-1.0.0.tar.gz (29.5 kB view details)

Uploaded Source

Built Distribution

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

vextra_utils-1.0.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file vextra_utils-1.0.0.tar.gz.

File metadata

  • Download URL: vextra_utils-1.0.0.tar.gz
  • Upload date:
  • Size: 29.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for vextra_utils-1.0.0.tar.gz
Algorithm Hash digest
SHA256 fd9895da43bdfd1134efdde274553f19eac5bc9c0772d08c789704d9b893aae5
MD5 6c0caecc2e3963eff40da612585533d4
BLAKE2b-256 ff7670ca037d1733e29b1a0d025f64b11b221f0334961e9cc792716fcf854e07

See more details on using hashes here.

Provenance

The following attestation bundles were made for vextra_utils-1.0.0.tar.gz:

Publisher: publish.yml on vnniciusg/vextra_utils

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file vextra_utils-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: vextra_utils-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for vextra_utils-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fa7a31329a5ba9d6974c7370a5a7119fc9845b4221d8472675cb190b51654f36
MD5 acaa3120ac9719367ce2be9f989b77b2
BLAKE2b-256 b6626d7f687bc014a947cc186def9bb72bedf2659cde47db388422156db04b4f

See more details on using hashes here.

Provenance

The following attestation bundles were made for vextra_utils-1.0.0-py3-none-any.whl:

Publisher: publish.yml on vnniciusg/vextra_utils

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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