Decorators to define lazy methods and functions
Project description
lazy-decorators
The lazy-decorators package provides a set of decorators for implementing lazy and cached properties in Python. It allows expensive computations to be deferred until they are actually needed and caches the results for later use.
Installation
pip install lazy-decorators
Concept
In applications where operations like database connections or heavy data processing exist, executing them early can waste resources. With lazy-decorators, such properties are evaluated only when accessed, and the results are cached. If dependencies change, the cache can be invalidated manually or automatically.
The package provides two main types of decorators:
NotThreadSafe: For single-threaded environmentsThreadSafe: For multi-threaded environments
Example Usage (NotThreadSafe)
from lazy_decorators import NotThreadSafe
class DatabaseClient(NotThreadSafe.CachedPropertyDependencyMixin):
def __init__(self, configuration):
self.configuration = configuration
# The connection is created lazily and cached until dependencies change
@NotThreadSafe.dependent_cached_property(depends_on=['configuration'])
def connection(self):
# Create a connection object based on current configuration
return f'Connection established with: {self.configuration}'
# Create a client with initial configuration
client = DatabaseClient(configuration={'user': 'user_1', 'password': 'pass_1'})
# First access triggers connection creation and caches the result
connection_first = client.connection
# Subsequent access returns the cached connection
connection_cached = client.connection
# Changing configuration does not automatically recreate connection
client.configuration = {'user': 'user_2', 'password': 'pass_2'}
connection_stale = client.connection
# Manually invalidate the cache
client.invalidate_cache('connection')
# Next access recreates the connection and caches it
connection_new = client.connection
Example Usage (ThreadSafe)
from lazy_decorators import ThreadSafe
import time
import threading
class DataLoader(ThreadSafe.CachedPropertyDependencyMixin):
def __init__(self, file_path):
self.file_path = file_path
# Data is loaded lazily and shared safely across multiple threads
@ThreadSafe.dependent_cached_property(depends_on=['file_path'])
def data(self):
# Simulate loading heavy data from file
time.sleep(2)
return f'Data loaded successfully from {self.file_path}'
loader = DataLoader(file_path='dataset.csv')
# Function to access data concurrently
def load_data_task():
# Only first thread triggers data loading, others use cached value
data = loader.data
# Launch multiple threads accessing the property
threads = [threading.Thread(target=load_data_task) for _ in range(3)]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
# Change dependency and invalidate cache
loader.file_path = 'dataset_v2.csv'
loader.invalidate_cache('data')
# Next access reloads the data and caches it again
data_new = loader.data
Suggested Study Path
- Caches
- Usage examples:
how_to_use.py - Lazy object instantiation:
lazy_instantiation.py - Thread-Safe versions:
Use Cases
- Managing heavy resources such as databases or large files
- Expensive computations that are called repeatedly
- Dynamic dependencies between properties
- Multi-threaded projects requiring safe caching
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 lazy-decorators-1.0.1.tar.gz.
File metadata
- Download URL: lazy-decorators-1.0.1.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c7ee85b226fb9ab9079e98bdae548a2f3bb0cfef3856b40140b4c7bc7be303c7
|
|
| MD5 |
8bb57a43fdb50fe6e8b0856b32985aa1
|
|
| BLAKE2b-256 |
aac4ecff5a564372589e21c235db427f92d6cf72c75d9e6c23198d522901cfcf
|
File details
Details for the file lazy_decorators-1.0.1-py3-none-any.whl.
File metadata
- Download URL: lazy_decorators-1.0.1-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c4eea8984c36bca845b755e5fcc8b540e221a5acffd6d20c5f59a0f08a078595
|
|
| MD5 |
d842667b3db9228a6070255601601bfd
|
|
| BLAKE2b-256 |
eb652072497c152933f2ecacbe299fc86e2bcb78150c05e5044f10b459122aeb
|