Skip to main content

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.

Metadata

Release files for uw 0.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for uw 0.0.5
File Size Uploaded
uw-0.0.5.tar.gz 10.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for uw 0.0.5
File Interpreter ABI Platform
uw-0.0.5-py3-none-any.whl Python 3 none any Details

Total release size: 21.6 kB

Release files / uw-0.0.5.tar.gz

Download URL uw-0.0.5.tar.gz
Size 10.7 kB
Tags Source
SHA-256 checksum
How to use checksums
b6300b9637a97347fa16aefc2cb99f7f713ecc8c613e2a1e5352a8c1ef92834b
BLAKE2b-256 checksum
How to use checksums
bd9646f6dcb26adf4c76dbcb5fbf079c674b23a47a7d2e3e8ad710dd5d7236f0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.13

Release files / uw-0.0.5-py3-none-any.whl

Download URL uw-0.0.5-py3-none-any.whl
Size 10.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
006246c2f6c728443bebde6928577a024b57b107fdf06efda9b23d08e26a2be0
BLAKE2b-256 checksum
How to use checksums
159659b6f0b1b55b97f8adc511d3287074a42d94bb5a47a283c5b979beae5143
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.13

Release history Release notifications | RSS feed

This release

0.0.5 This release

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page