Skip to main content

Ruff

Deczoo

A zoo for decorators

There are many great decorators out there that we use everyday. Why don't collect few of them?

I found myself implementing over and over in different projects. The hope is to gather them here and use this codebase.


Documentation | Source Code


Alpha Notice

This codebase is experimental and is working for my use cases. It is very probable that there are cases not covered and for which it breaks (badly). If you find them, please feel free to open an issue in the issue page of the repo.

What is a decorator?

In short a python decorator is a way to modify or enhance the behavior of a function or a class without actually modifying the source code of the function or class.

Decorators are implemented as functions (or classes) that take a function or a class as input and return a new function or class that has some additional functionality.

To have a more in-depth explanation you can check the decorators docs section.

Installation

deczoo is published as a Python package on pypi, and it can be installed with pip, or directly from source using git, or with a local clone:

  • pip (suggested):

    python -m pip install deczoo
    
  • pip + source/git:

    python -m pip install git+https://github.com/FBruzzesi/deczoo.git
    
  • local clone:

    git clone https://github.com/FBruzzesi/deczoo.git
    cd deczoo
    python -m pip install .
    

Dependencies

As of now, the library has no additional required dependencies, however:

  • some functionalities works only on UNIX systems (@memory_limit and @timeout)
  • to use some decorators you may need to install additional dependencies (e.g. install chime to use @chime_on_end)

Getting started

The idea is kind of simple: each function in the library is a (function) decorator with a specific objective in mind.

from deczoo import log

@log # equivalent to @log(log_time=True, log_args=True, log_error=True, logging_fn=print)
def custom_add(a, b, *args):
    """Adds all arguments together"""
    return sum([a, b, *args])

_ = custom_add(1, 2, 3, 4)
# custom_add args=(a=1, b=2, args=(3, 4)) time=0:00:00.000062

 _ = custom_add(1, "a", 2)
# custom_add args=(a=1, b=a, args=(2,)) time=0:00:00.000064 Failed with error: unsupported
# operand type(s) for +: 'int' and 'str'
from deczoo import shape_tracker

@shape_tracker(shape_in=True, shape_out=True, shape_delta=True, raise_if_empty=True)
def tracked_vstack(a: np.ndarray, b: np.ndarray) -> np.ndarray:
    return np.vstack([a, b])

_ = tracked_vstack(np.ones((1, 2)), np.ones((10, 2)))
# Input: `a` has shape (1, 2)
# Output: result has shape (11, 2)
# Shape delta: (-10, 0)

Features

The library implements the following decorators:

  • call_counter: tracks how many times a function has been called.
  • catch: wraps a function in a try-except block, returning a custom value, or raising a custom exception.
  • check_args: checks that function arguments satisfy its "rule".
  • chime_on_end: notify with chime sound on function end (success or error).
  • log: tracks function time taken, arguments and errors, such logs can be written to a file.
  • timer: tracks function time taken.
  • memory_limit: sets a memory limit while running the function.
  • notify_on_end: notifies when function finished running with a custom notifier.
  • raise_if: raises a custom exception if a condition is met.
  • retry: wraps a function with a "retry" block.
  • shape_tracker: tracks the shape of a dataframe/array-like object, in input and/or output.
  • multi_shape_tracker: tracks the shapes of input(s) and/or output(s) of a function.
  • timeout: sets a time limit for the function, terminates the process if it hasn't finished within such time limit.

Examples

Please refer to the api page to see a basic example for each decorator.

Contributing

Please read the Contributing guidelines in the documentation site.

License

The project has a MIT Licence

Metadata

Release files for deczoo 0.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for deczoo 0.6.0
File Size Uploaded
deczoo-0.6.0.tar.gz 560.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for deczoo 0.6.0
File Interpreter ABI Platform
deczoo-0.6.0-py3-none-any.whl Python 3 none any Details

Total release size: 574.6 kB

Release files / deczoo-0.6.0.tar.gz

Download URL deczoo-0.6.0.tar.gz
Size 560.4 kB
Tags Source
SHA-256 checksum
How to use checksums
9acbec6a9410c72699cd02ed3b7c59ad5c0430f1ede07a574f905c00e7df3715
BLAKE2b-256 checksum
How to use checksums
93b97ed586cb3cb0d56d80abbb5c273a8dcf7e3c02f2da239e479f0401fb2929
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.25.0

Release files / deczoo-0.6.0-py3-none-any.whl

Download URL deczoo-0.6.0-py3-none-any.whl
Size 14.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d9a691aa41541626e4d7761ed27560f5f0ff6df37f58be3ebea229a3c8929fef
BLAKE2b-256 checksum
How to use checksums
bc8dd71a9ab6377c4588335f31f24df618b518c1521ef7267f47e857c571fe23
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.25.0

Release history Release notifications | RSS feed

This release

0.6.0 This release

2 release files

0.5.5

2 release files

0.5.4

2 release files

0.5.3

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page