Skip to content

TahsinCr/python-decoratorfunc

Repository files navigation

Contributors Forks Stargazers Issues MIT License LinkedIn

Logo Logo


Logo

Python DecoratorFunc

Simple tools for creating and managing decorator functions 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 function 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 - DecoratorBasic has been used for basic decorator utilities.

Installation

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

(back to top)

Usage

Let's create a decorator named Timer

import time

from decoratorbasic import get_decorators, get_decorator_callable
from decoratorfunc import definator, decorator, is_decoratorfunc

@definator
def time_decorator(func):
    # The DecoratorFuncBasic class is automatically created.
    # It is named according to PEP8 conventions by using the function's name (time_decorator -> TimeDecorator).
    # The generated DecoratorFuncBasic class can be accessed via time_decorator.__class__.
    print(f"Add Decorator '{func.__name__}': '{time_decorator.__class__.__name__}'")
    return func

@time_decorator.decorator
def time_decorator(func, *args, **kwargs):
    # A function defined with the definator/decorator will return a DecoratorFuncBasic class.
    # This allows the function to be redefined using the definator/decorator again. (time_decorator.decorator/time_decorator.definator)
    start = time.time()
    result = func(*args, **kwargs)
    end = time.time()
    print(f"Result '{callable.__name__}': {end - start}")
    return result

# Since the time_decorator function returns the DecoratorFuncBasic class, the __call__ method of this class is used as a decorator.
# In this way, the DecoratorFuncBasic class behaves like DecoratorBasic and fulfills the decorator functionality.
@time_decorator
def test_range(start:int, stop:int):
    for n in range(start, stop): ...

@time_decorator
class testo:
    def __init__(self):
        for n in range(1, 10000000):...


test_range(1,4000000)

decoratorbases = get_decorators(test_range)
print('IsDecoratorFunc:', is_decoratorfunc(decoratorbases[0]))

testo()

Output

Add Decorator 'test_range': 'TimeDecorator'
Add Decorator 'testo': 'TimeDecorator'
Result 'callable': 0.10388374328613281
IsDecoratorFunc: True
Result 'callable': 0.1656045913696289

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 decorator functions in Python.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages