Simple tools for creating and managing decorators in Python.
Table of Contents
Provides simple decorator utilities for Python 3.x.
To get a local copy up and running follow these simple example steps.
- Python-lib - CallableType has been used for advanced type checking.
- Clone the repo
git clone https://github.com/TahsinCr/python-decoratorbasic.git- Install PIP packages
pip install decoratorbasicLet's create a decorator named Timer
from typing import Any, Callable, TypeVar, Generic
import time
from decoratorbasic import (
DecoratorBasic, get_decorator_callable, get_decorators
)
TCallable = TypeVar('TCallable', bound=Callable)
class TimerDecorator(DecoratorBasic[TCallable], Generic[TCallable]):
def definator(self, callable:TCallable):
print(f"Add Decorator '{callable.__name__}': '{self.__class__.__name__}'")
return callable
def decorator(self, callable:TCallable, *args, **kwargs):
self.start = time.time()
result = callable(*args, **kwargs)
self.end = time.time()
print(f"Result '{callable.__name__}': {self.end - self.start}")
return result
@TimerDecorator()
def test_range(start:int, stop:int):
for n in range(start, stop): ...
@TimerDecorator()
class testo:
def __init__(self):
for n in range(1, 10000000):...
test_range(1,4000000)
print('DecoratorFunction: {}; BaseFuntion: {}'.format(test_range, get_decorator_callable(test_range)))
print('Decorators: {}'.format(str(get_decorators(test_range))))
testo()Output
Add Decorator 'test_range': 'TimerDecorator'
Add Decorator 'testo': 'TimerDecorator'
Result 'test_range': 0.06389307975769043
DecoratorFunction: <function test_range at 0x000001C8395540E0>; BaseFuntion: <function test_range at 0x000001C839161440>
Decorators: [<__main__.TimerDecorator object at 0x000001C8393C1400>]
Result '__init__': 0.1724531650543213
For more examples, please refer to the Documentation
- Add documentation.
See the open issues for a full list of proposed features (and known issues).
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement".
Don't forget to give the project a star! Thanks again!
-
Fork the Project
-
Create your Feature Branch (
git checkout -b feature/AmazingFeature) -
Commit your Changes (
git commit -m 'Add some AmazingFeature') -
Push to the Branch (
git push origin feature/AmazingFeature) -
Open a Pull Request
Distributed under the MIT License. See LICENSE for more information.
Email: TahsinCr@outlook.com
X: @TahsinCrs
LinkedIn: TahsinCr
