Parameter Decorators

0.0.2 · active · verified Thu Apr 16

Parameter-decorators is a lightweight Python library providing handy decorators for automatically converting function or method parameters. It currently supports converting string inputs to `pathlib.Path` objects and vice-versa, streamlining argument handling. The library is in an early stage, with version 0.0.2 released, and aims for a release cadence as new conversion decorators are developed.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to use the `str_to_path` decorator. The `process_file` method is type-hinted to expect a `Path` object, but thanks to the decorator, it seamlessly accepts a string path, which is then converted before the method's execution. It also shows that you can still pass `Path` objects directly.

from parameter_decorators import str_to_path
from pathlib import Path

class MyProcessor:
    @str_to_path
    def process_file(self, path: Path):
        """Expects a Path object, but decorator allows string input."""
        print(f"Processing: {path.name} (Type: {type(path)})")
        # Simulate file operation
        if path.exists():
            print(f"File '{path}' exists.")
        else:
            print(f"File '{path}' does not exist, creating dummy.")
            path.touch()

# Example usage:
processor = MyProcessor()
processor.process_file("my_document.txt")
processor.process_file(Path("another_file.log")) # Can still pass Path directly

view raw JSON →