useful decorators
Project description
uw
useful decorators
To install: pip install uw
Overview
The uw package provides a collection of Python decorators and utilities designed to simplify common programming tasks such as argument injection, automatic method decoration, resource management, and property caching. These tools are especially useful for enhancing code readability and reducing boilerplate.
Features
Decorators
inject_args
Injects arguments into a function, ignoring those that are not accepted by the function. This is useful when you want to pre-specify some of the arguments a function should use without modifying the function itself.
Usage Example:
from uw import inject_args
@inject_args(a=2, b=3)
def formula(a, b, c=1):
return (a - b) * c
print(formula()) # Output: -1
print(formula(c=10)) # Output: -10
make_sure_to_close
Creates an ad-hoc context manager to ensure that resources are closed after use. This is particularly useful when dealing with objects that require explicit closure.
Usage Example:
from uw import make_sure_to_close
with make_sure_to_close(open('file.txt', 'w')) as f:
f.write('Hello, world!')
# File is automatically closed after the block
decorate_all_methods
Applies a decorator to all methods of a class, optionally excluding or including specific methods.
Usage Example:
from uw import decorate_all_methods, just_print_exceptions
@decorate_all_methods(just_print_exceptions)
class Processor:
def process(self):
raise Exception("Processing error")
def calculate(self):
raise Exception("Calculation error")
p = Processor()
p.process() # Prints "Processing error"
p.calculate() # Prints "Calculation error"
autoargs
Automatically assigns constructor arguments to instance variables, reducing the need for boilerplate code in the __init__ method.
Usage Example:
from uw import autoargs
class Configuration:
@autoargs()
def __init__(self, host, port, debug=False):
pass
config = Configuration('localhost', 8080)
print(config.host) # Output: localhost
print(config.port) # Output: 8080
print(config.debug) # Output: False
Utilities
lazyprop
A descriptor for creating properties that are computed on first access and cached for subsequent accesses.
Usage Example:
from uw import lazyprop
class DataSet:
def __init__(self, data):
self._data = data
@lazyprop
def summary(self):
print("Calculating summary")
return sum(self._data) / len(self._data)
ds = DataSet([1, 2, 3])
print(ds.summary) # Output: Calculating summary, then 2.0
print(ds.summary) # Output: 2.0 (cached, no recalculation)
Contributing
Contributions to the uw package are welcome! Please feel free to submit pull requests or create issues for bugs and feature requests on our GitHub repository.
For more detailed information on each function and class, refer to the source code documentation available in the __init__.py file within the package.
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
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 uw-0.0.5.tar.gz.
File metadata
- Download URL: uw-0.0.5.tar.gz
- Upload date:
- Size: 10.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b6300b9637a97347fa16aefc2cb99f7f713ecc8c613e2a1e5352a8c1ef92834b
|
|
| MD5 |
74c564cdf7de803fbc82305b82277aac
|
|
| BLAKE2b-256 |
bd9646f6dcb26adf4c76dbcb5fbf079c674b23a47a7d2e3e8ad710dd5d7236f0
|
File details
Details for the file uw-0.0.5-py3-none-any.whl.
File metadata
- Download URL: uw-0.0.5-py3-none-any.whl
- Upload date:
- Size: 10.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
006246c2f6c728443bebde6928577a024b57b107fdf06efda9b23d08e26a2be0
|
|
| MD5 |
e2d3f7317243bcc22ed8b7e587a855d9
|
|
| BLAKE2b-256 |
159659b6f0b1b55b97f8adc511d3287074a42d94bb5a47a283c5b979beae5143
|