Skip to main content

Simple and useful decorator utilities.

Project description

decorator-utils

Installation

python3 -m pip install decorator-utils

Usage

Without context

from random import randint

from decorator_utils import function_wrapper


@function_wrapper(pre_cb=lambda i: print(f'Calling with param {i}'),
                  post_cb=lambda r, i: print(f'Function call result `{r}` with param `{i}`'))
def random_int(increment):
    number = randint(0, 10)
    print(f'-> Generated number {number}')

    return number + increment


if __name__ == '__main__':
    n = randint(0, 10)

    random_int(n)

With context

from random import randint
from time import perf_counter_ns
from typing import final

from decorator_utils import DecoratorContext


class TraceCall(DecoratorContext):
    __time: int

    @final
    def pre_cb(self, *args, **kwargs) -> None:
        self.__time = perf_counter_ns()
        print(f'Starting function call with args `{args}` and kwargs `{kwargs}`.')

    @final
    def post_cb(self, result, *args, **kwargs) -> None:
        elapsed_time = (perf_counter_ns() - self.__time) / 1e+9
        print(f'Function took {elapsed_time} seconds to return value `{result}`.')


@TraceCall
def random_int(increment):
    number = randint(0, 10)
    print(f'-> Generated number {number}')

    return number + increment


if __name__ == '__main__':
    n = randint(0, 10)

    random_int(n)

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

decorator_utils-1.1.1.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

decorator_utils-1.1.1-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file decorator_utils-1.1.1.tar.gz.

File metadata

  • Download URL: decorator_utils-1.1.1.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for decorator_utils-1.1.1.tar.gz
Algorithm Hash digest
SHA256 c5955b25042b34dae85920bad0dd3c19918c02a7dfa1f0f150e14de1e6fe14bb
MD5 000785e6dc147255a222bf871d030f97
BLAKE2b-256 4f98711b97491f388b1db3625b6aa2afc786e9a37748b5d3c4ca6530b99d5020

See more details on using hashes here.

File details

Details for the file decorator_utils-1.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for decorator_utils-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0fa057019f080ac541a142a0aa54377fc372364aa688c8bfd63bc596a59829f8
MD5 9e6a7209defd425b26fc810bb843a46f
BLAKE2b-256 59491676516f19ae31f14b80594eb56affb37bf12232471f04dd4547a04c7129

See more details on using hashes here.

Supported by

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