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
Tests generated by Claude xD
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dfb0eb46cefc56ab36a51115398ec8cb7969a8542be9938b2d3694fed6bbc2ea
|
|
| MD5 |
8ceee0c8a5bb513c3bfe72db2052d307
|
|
| BLAKE2b-256 |
83b20febd5685dc722734f7f25d92bdef9b5d935abaef8693c37d666de0223b7
|
Provenance
The following attestation bundles were made for vextra_utils-1.0.1.tar.gz:
Publisher:
publish.yml on vnniciusg/vextra_utils
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
vextra_utils-1.0.1.tar.gz -
Subject digest:
dfb0eb46cefc56ab36a51115398ec8cb7969a8542be9938b2d3694fed6bbc2ea - Sigstore transparency entry: 853796899
- Sigstore integration time:
-
Permalink:
vnniciusg/vextra_utils@6187789bb7d11bba309fd01aa5ebef5683e982e1 -
Branch / Tag:
refs/tags/v1.0.1 - Owner: https://github.com/vnniciusg
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@6187789bb7d11bba309fd01aa5ebef5683e982e1 -
Trigger Event:
push
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a774e61db7e4aab01a45bc479552ba9f50184a2839c773a59d9e3291abf37164
|
|
| MD5 |
a2bae43037ccccb49fed43f859d2d192
|
|
| BLAKE2b-256 |
d79ffb4b4eea13a93c4a342ba977dcbe48b280bfee3a02d20534ab0a37b5f160
|
Provenance
The following attestation bundles were made for vextra_utils-1.0.1-py3-none-any.whl:
Publisher:
publish.yml on vnniciusg/vextra_utils
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
vextra_utils-1.0.1-py3-none-any.whl -
Subject digest:
a774e61db7e4aab01a45bc479552ba9f50184a2839c773a59d9e3291abf37164 - Sigstore transparency entry: 853796901
- Sigstore integration time:
-
Permalink:
vnniciusg/vextra_utils@6187789bb7d11bba309fd01aa5ebef5683e982e1 -
Branch / Tag:
refs/tags/v1.0.1 - Owner: https://github.com/vnniciusg
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@6187789bb7d11bba309fd01aa5ebef5683e982e1 -
Trigger Event:
push
-
Statement type: