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.1.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.1-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: vextra_utils-1.0.1.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.1.tar.gz
Algorithm Hash digest
SHA256 dfb0eb46cefc56ab36a51115398ec8cb7969a8542be9938b2d3694fed6bbc2ea
MD5 8ceee0c8a5bb513c3bfe72db2052d307
BLAKE2b-256 83b20febd5685dc722734f7f25d92bdef9b5d935abaef8693c37d666de0223b7

See more details on using hashes here.

Provenance

The following attestation bundles were made for vextra_utils-1.0.1.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.1-py3-none-any.whl.

File metadata

  • Download URL: vextra_utils-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.5 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a774e61db7e4aab01a45bc479552ba9f50184a2839c773a59d9e3291abf37164
MD5 a2bae43037ccccb49fed43f859d2d192
BLAKE2b-256 d79ffb4b4eea13a93c4a342ba977dcbe48b280bfee3a02d20534ab0a37b5f160

See more details on using hashes here.

Provenance

The following attestation bundles were made for vextra_utils-1.0.1-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