Skip to content

TahsinCr/python-decoratorbasic

Repository files navigation

Contributors Forks Stargazers Issues MIT License LinkedIn

Logo Logo


Logo

Python decoratorbasic

Simple tools for creating and managing decorators in Python.

Changelog · Report Bug · Request Feature

Table of Contents
  1. About The Project

  2. Getting Started

  3. Usage
  4. Roadmap
  5. Contributing
  6. License
  7. Contact
  8. Acknowledgments

About The Project

Provides simple decorator utilities for Python 3.x.

(back to top)

Built With

  • python-shield

(back to top)

Getting Started

To get a local copy up and running follow these simple example steps.

Prerequisites

  • Python-lib - CallableType has been used for advanced type checking.

Installation

  1. Clone the repo
git clone https://github.com/TahsinCr/python-decoratorbasic.git
  1. Install PIP packages
pip install decoratorbasic

(back to top)

Usage

Let'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

(back to top)

Roadmap

  • Add documentation.

See the open issues for a full list of proposed features (and known issues).

(back to top)

Contributing

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!

  1. Fork the Project

  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)

  3. Commit your Changes (git commit -m 'Add some AmazingFeature')

  4. Push to the Branch (git push origin feature/AmazingFeature)

  5. Open a Pull Request

(back to top)

License

Distributed under the MIT License. See LICENSE for more information.

(back to top)

Contact

Email: TahsinCr@outlook.com

X: @TahsinCrs

LinkedIn: TahsinCr

(back to top)

Acknowledgments

(back to top)

About

Simple tools for creating and managing decorators in Python.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages