Skip to main content

Simple tools for creating and managing decorators in Python.

This project has been archived.

The maintainers of this project have marked this project as archived. No new releases are expected.

Project description

Contributors Forks Stargazers Issues MIT License LinkedIn

Python decoratorbasic

Simple tools for creating and managing decorators in Python.

Changelog · Report Bug · Request Feature

About The Project

Provides simple decorator utilities for Python 3.x.

Built With

  • python-shield

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

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


License

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


Contact

Tahsin Çirkin - @TahsinCrs - TahsinCr@outlook.com

Project: TahsinCr/python-decoratorbasic

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

decoratorbasic-1.0.1.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

decoratorbasic-1.0.1-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file decoratorbasic-1.0.1.tar.gz.

File metadata

  • Download URL: decoratorbasic-1.0.1.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for decoratorbasic-1.0.1.tar.gz
Algorithm Hash digest
SHA256 21afbbf10e7229f93ea78b695ae2529f875f78b8a760dfcce95b4c8c2541af9d
MD5 329ff4110d59b8e31c164c4309c36a77
BLAKE2b-256 53b86822e6d5b9937f781dfd1236a17963a1f57fc3d8217585c2abd0ad69c0a5

See more details on using hashes here.

File details

Details for the file decoratorbasic-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: decoratorbasic-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for decoratorbasic-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 667d0eb938f8ed85a5dd92f0af5988b4b1296f27f72ddf5b6992482f6dadcc0b
MD5 724157a0c5d205a399a393f5dcdd54ba
BLAKE2b-256 58c8830651eb89d542be19311d3a1a16b412f4e5baa8f1d5fbb412c84434041e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page