Skip to content

A comprehensive collection of Python decorators and utility functions to enhance your development workflow.

License

Notifications You must be signed in to change notification settings

LitePy/PythonWrap

Repository files navigation

PythonWrap

A comprehensive collection of Python decorators and utility functions to enhance your development workflow.

Installation

pip install python-wrap

Features

PythonWrap provides a rich set of decorators that can be used either as decorators or as standalone functions:

Performance & Profiling

  • @timing: Measure function execution time
  • @profile: Profile function performance
  • @benchmark: Compare execution times
  • @measure_memory: Track memory usage

Error Handling & Reliability

  • @retry: Retry failed operations
  • @retry_on_exception: Retry on specific exceptions
  • @timeout: Set execution time limits
  • @revert_on_failure: Automatic state rollback on failure

Caching & Optimization

  • @memoize: Cache function results
  • @cache: Time-based result caching
  • @once: Single execution guarantee

Debugging & Logging

  • @log_args: Log function arguments
  • @log_return: Log return values
  • @trace: Log call stack traces
  • @audit: Comprehensive function call auditing

Type Safety & Validation

  • @check_type: Runtime type checking
  • @validate_args: Custom argument validation

Development Tools

  • @deprecate: Mark deprecated functions
  • @no_debug: Disable debug output
  • @mock_data: Easy data mocking
  • @rate_limit: Control execution frequency

Concurrency

  • @run_in_thread: Asynchronous execution
  • @transactional: Atomic operations

Usage Examples

Using as Decorators

frompython_wrapimporttiming, retry, memoize# As a decorator@timingdefslow_operation(): time.sleep(1) return"Done"# As a decorator with parameters@retry(max_attempts=3, delay=1.0)defunreliable_operation(): returnapi_call() # Simple decorator@memoizedeffibonacci(n): ifn<2: returnnreturnfibonacci(n-1) +fibonacci(n-2)

Using as Functions

frompython_wrapimporttiming, retry, memoize# Using timing as a functiondefslow_operation(): time.sleep(1) return"Done"result=timing(slow_operation)() # Using retry as a functiondefunreliable_operation(): returnapi_call() result=retry(unreliable_operation, max_attempts=3, delay=1.0)() # Using memoize as a functiondeffibonacci(n): ifn<2: returnnreturnfibonacci(n-1) +fibonacci(n-2) memoized_fib=memoize(fibonacci) result=memoized_fib(10)

Advanced Usage

frompython_wrapimportvalidate_args, check_type, timeout# As decorators@validate_args(x=lambdax: x>0, y=lambday: y<100)defprocess_numbers(x, y): returnx+y@check_type(name=str, age=int)defcreate_user(name, age): return{"name": name, "age": age} # As functionsdeflong_running_task(): process_large_dataset() timed_task=timeout(5)(long_running_task) result=timed_task()

Combining Multiple Functions

frompython_wrapimporttiming, retry, log_args# As decorators@timing@retry(max_attempts=3)@log_argsdefcomplex_operation(x, y): returnexpensive_calculation(x, y) # As functionsdefcomplex_operation(x, y): returnexpensive_calculation(x, y) # Compose functionsmonitored_op=timing(retry(log_args(complex_operation), max_attempts=3)) result=monitored_op(1, 2)

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

A comprehensive collection of Python decorators and utility functions to enhance your development workflow.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages