deczoo


Namedeczoo JSON
Version 0.6.0 PyPI version JSON
download
home_pageNone
SummaryZoo for Python decorators
upload_time2023-12-28 13:58:07
maintainerNone
docs_urlNone
authorFrancesco Bruzzesi
requires_python>=3.8
licenseMIT License Copyright (c) 2022 Francesco Bruzzesi Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <img src="docs/img/deczoo-logo.png" width=185 height=185 align="right">

![](https://img.shields.io/github/license/FBruzzesi/deczoo)
<img src="docs/img/interrogate-shield.svg">
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
<img src="docs/img/coverage.svg">
<img src= "https://img.shields.io/pypi/pyversions/deczoo">

# 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](https://fbruzzesi.github.io/deczoo) | [Source Code](https://github.com/fbruzzesi/deczoo)

---

## 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](https://github.com/FBruzzesi/deczoo/issues) 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](https://fbruzzesi.github.io/deczoo/decorators/intro/).

## Installation

**deczoo** is published as a Python package on [pypi](https://pypi.org/), and it can be installed with pip, or directly from source using git, or with a local clone:

- **pip** (suggested):

    ```bash
    python -m pip install deczoo
    ```

- **pip + source/git**:

    ```bash
    python -m pip install git+https://github.com/FBruzzesi/deczoo.git
    ```

- **local clone**:

    ```bash
    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`](https://github.com/MaxHalford/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.

```python title="Example: log decorator"
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'
```

```python title="Example: shape_tracker decorator"
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](https://fbruzzesi.github.io/deczoo/api/decorators/) to see a basic example for each decorator.

## Contributing

Please read the [Contributing guidelines](https://fbruzzesi.github.io/deczoo/contribute/) in the documentation site.

## License

The project has a [MIT Licence](https://github.com/FBruzzesi/deczoo/blob/main/LICENSE)

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "deczoo",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": null,
    "author": "Francesco Bruzzesi",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/93/b9/7ed586cb3cb0d56d80abbb5c273a8dcf7e3c02f2da239e479f0401fb2929/deczoo-0.6.0.tar.gz",
    "platform": null,
    "description": "<img src=\"docs/img/deczoo-logo.png\" width=185 height=185 align=\"right\">\n\n![](https://img.shields.io/github/license/FBruzzesi/deczoo)\n<img src=\"docs/img/interrogate-shield.svg\">\n[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)\n<img src=\"docs/img/coverage.svg\">\n<img src= \"https://img.shields.io/pypi/pyversions/deczoo\">\n\n# Deczoo\n\n> A zoo for decorators\n\nThere are many great decorators out there that we use everyday. Why don't collect few of them?\n\nI found myself implementing over and over in different projects. The hope is to gather them here and use this codebase.\n\n---\n\n[Documentation](https://fbruzzesi.github.io/deczoo) | [Source Code](https://github.com/fbruzzesi/deczoo)\n\n---\n\n## Alpha Notice\n\nThis 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](https://github.com/FBruzzesi/deczoo/issues) of the repo.\n\n## What is a decorator?\n\nIn 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.\n\nDecorators 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.\n\nTo have a more in-depth explanation you can check the [decorators docs section](https://fbruzzesi.github.io/deczoo/decorators/intro/).\n\n## Installation\n\n**deczoo** is published as a Python package on [pypi](https://pypi.org/), and it can be installed with pip, or directly from source using git, or with a local clone:\n\n- **pip** (suggested):\n\n    ```bash\n    python -m pip install deczoo\n    ```\n\n- **pip + source/git**:\n\n    ```bash\n    python -m pip install git+https://github.com/FBruzzesi/deczoo.git\n    ```\n\n- **local clone**:\n\n    ```bash\n    git clone https://github.com/FBruzzesi/deczoo.git\n    cd deczoo\n    python -m pip install .\n    ```\n\n### Dependencies\n\nAs of now, the library has no additional required dependencies, however:\n\n- some functionalities works only on UNIX systems (`@memory_limit` and `@timeout`)\n- to use some decorators you may need to install additional dependencies (e.g. install [`chime`](https://github.com/MaxHalford/chime) to use `@chime_on_end`)\n\n## Getting started\n\nThe idea is kind of simple: each function in the library is a (function) decorator with a specific objective in mind.\n\n```python title=\"Example: log decorator\"\nfrom deczoo import log\n\n@log # equivalent to @log(log_time=True, log_args=True, log_error=True, logging_fn=print)\ndef custom_add(a, b, *args):\n    \"\"\"Adds all arguments together\"\"\"\n    return sum([a, b, *args])\n\n_ = custom_add(1, 2, 3, 4)\n# custom_add args=(a=1, b=2, args=(3, 4)) time=0:00:00.000062\n\n _ = custom_add(1, \"a\", 2)\n# custom_add args=(a=1, b=a, args=(2,)) time=0:00:00.000064 Failed with error: unsupported\n# operand type(s) for +: 'int' and 'str'\n```\n\n```python title=\"Example: shape_tracker decorator\"\nfrom deczoo import shape_tracker\n\n@shape_tracker(shape_in=True, shape_out=True, shape_delta=True, raise_if_empty=True)\ndef tracked_vstack(a: np.ndarray, b: np.ndarray) -> np.ndarray:\n    return np.vstack([a, b])\n\n_ = tracked_vstack(np.ones((1, 2)), np.ones((10, 2)))\n# Input: `a` has shape (1, 2)\n# Output: result has shape (11, 2)\n# Shape delta: (-10, 0)\n```\n\n### Features\n\nThe library implements the following decorators:\n\n- `call_counter`: tracks how many times a function has been called.\n- `catch`: wraps a function in a try-except block, returning a custom value, or raising a custom exception.\n- `check_args`: checks that function arguments satisfy its \"rule\".\n- `chime_on_end`: notify with chime sound on function end (success or error).\n- `log`: tracks function time taken, arguments and errors, such logs can be written to a file.\n- `timer`: tracks function time taken.\n- `memory_limit`: sets a memory limit while running the function.\n- `notify_on_end`: notifies when function finished running with a custom notifier.\n- `raise_if`: raises a custom exception if a condition is met.\n- `retry`: wraps a function with a \"retry\" block.\n- `shape_tracker`: tracks the shape of a dataframe/array-like object, in input and/or output.\n- `multi_shape_tracker`: tracks the shapes of input(s) and/or output(s) of a function.\n- `timeout`: sets a time limit for the function, terminates the process if it hasn't finished within such time limit.\n\n### Examples\n\nPlease refer to the [api page](https://fbruzzesi.github.io/deczoo/api/decorators/) to see a basic example for each decorator.\n\n## Contributing\n\nPlease read the [Contributing guidelines](https://fbruzzesi.github.io/deczoo/contribute/) in the documentation site.\n\n## License\n\nThe project has a [MIT Licence](https://github.com/FBruzzesi/deczoo/blob/main/LICENSE)\n",
    "bugtrack_url": null,
    "license": "MIT License\n        \n        Copyright (c) 2022 Francesco Bruzzesi\n        \n        Permission is hereby granted, free of charge, to any person obtaining a copy\n        of this software and associated documentation files (the \"Software\"), to deal\n        in the Software without restriction, including without limitation the rights\n        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n        copies of the Software, and to permit persons to whom the Software is\n        furnished to do so, subject to the following conditions:\n        \n        The above copyright notice and this permission notice shall be included in all\n        copies or substantial portions of the Software.\n        \n        THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n        SOFTWARE.",
    "summary": "Zoo for Python decorators",
    "version": "0.6.0",
    "project_urls": {
        "documentation": "https://fbruzzesi.github.io/deczoo/",
        "issue-tracker": "https://github.com/fbruzzesi/deczoo/issues",
        "repository": "https://github.com/fbruzzesi/deczoo"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "bc8dd71a9ab6377c4588335f31f24df618b518c1521ef7267f47e857c571fe23",
                "md5": "a2bb25bc085850da637e9b450077814d",
                "sha256": "d9a691aa41541626e4d7761ed27560f5f0ff6df37f58be3ebea229a3c8929fef"
            },
            "downloads": -1,
            "filename": "deczoo-0.6.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "a2bb25bc085850da637e9b450077814d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 14216,
            "upload_time": "2023-12-28T13:58:10",
            "upload_time_iso_8601": "2023-12-28T13:58:10.671463Z",
            "url": "https://files.pythonhosted.org/packages/bc/8d/d71a9ab6377c4588335f31f24df618b518c1521ef7267f47e857c571fe23/deczoo-0.6.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "93b97ed586cb3cb0d56d80abbb5c273a8dcf7e3c02f2da239e479f0401fb2929",
                "md5": "b0773e0b6c6154c0a415442ce68e3866",
                "sha256": "9acbec6a9410c72699cd02ed3b7c59ad5c0430f1ede07a574f905c00e7df3715"
            },
            "downloads": -1,
            "filename": "deczoo-0.6.0.tar.gz",
            "has_sig": false,
            "md5_digest": "b0773e0b6c6154c0a415442ce68e3866",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 560367,
            "upload_time": "2023-12-28T13:58:07",
            "upload_time_iso_8601": "2023-12-28T13:58:07.896202Z",
            "url": "https://files.pythonhosted.org/packages/93/b9/7ed586cb3cb0d56d80abbb5c273a8dcf7e3c02f2da239e479f0401fb2929/deczoo-0.6.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-12-28 13:58:07",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "fbruzzesi",
    "github_project": "deczoo",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "deczoo"
}
        
Elapsed time: 0.14829s