decorator-utils


Namedecorator-utils JSON
Version 1.1.1 PyPI version JSON
download
home_pageNone
SummarySimple and useful decorator utilities.
upload_time2024-04-14 07:20:35
maintainerNone
docs_urlNone
authorNone
requires_python>=3.9
licenseMIT License Copyright (c) 2024 Julio C. Galindo 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 decorator utilities
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            decorator-utils
===============


Installation
------------

```shell
python3 -m pip install decorator-utils
```

Usage
-----

### Without context

```python
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

```python
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)
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "decorator-utils",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "decorator, utilities",
    "author": null,
    "author_email": "\"Julio C. Galindo\" <jcgalindo.jcgh@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/4f/98/711b97491f388b1db3625b6aa2afc786e9a37748b5d3c4ca6530b99d5020/decorator_utils-1.1.1.tar.gz",
    "platform": null,
    "description": "decorator-utils\n===============\n\n\nInstallation\n------------\n\n```shell\npython3 -m pip install decorator-utils\n```\n\nUsage\n-----\n\n### Without context\n\n```python\nfrom random import randint\n\nfrom decorator_utils import function_wrapper\n\n\n@function_wrapper(pre_cb=lambda i: print(f'Calling with param {i}'),\n                  post_cb=lambda r, i: print(f'Function call result `{r}` with param `{i}`'))\ndef random_int(increment):\n    number = randint(0, 10)\n    print(f'-> Generated number {number}')\n\n    return number + increment\n\n\nif __name__ == '__main__':\n    n = randint(0, 10)\n\n    random_int(n)\n```\n\n### With context\n\n```python\nfrom random import randint\nfrom time import perf_counter_ns\nfrom typing import final\n\nfrom decorator_utils import DecoratorContext\n\n\nclass TraceCall(DecoratorContext):\n    __time: int\n\n    @final\n    def pre_cb(self, *args, **kwargs) -> None:\n        self.__time = perf_counter_ns()\n        print(f'Starting function call with args `{args}` and kwargs `{kwargs}`.')\n\n    @final\n    def post_cb(self, result, *args, **kwargs) -> None:\n        elapsed_time = (perf_counter_ns() - self.__time) / 1e+9\n        print(f'Function took {elapsed_time} seconds to return value `{result}`.')\n\n\n@TraceCall\ndef random_int(increment):\n    number = randint(0, 10)\n    print(f'-> Generated number {number}')\n\n    return number + increment\n\n\nif __name__ == '__main__':\n    n = randint(0, 10)\n\n    random_int(n)\n```\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2024 Julio C. Galindo  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.",
    "summary": "Simple and useful decorator utilities.",
    "version": "1.1.1",
    "project_urls": {
        "Documentation": "https://github.com/stickm4n/decorator-utils/wiki",
        "Issues": "https://github.com/stickm4n/decorator-utils/issues",
        "Repository": "https://github.com/stickm4n/decorator-utils"
    },
    "split_keywords": [
        "decorator",
        " utilities"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "59491676516f19ae31f14b80594eb56affb37bf12232471f04dd4547a04c7129",
                "md5": "9e6a7209defd425b26fc810bb843a46f",
                "sha256": "0fa057019f080ac541a142a0aa54377fc372364aa688c8bfd63bc596a59829f8"
            },
            "downloads": -1,
            "filename": "decorator_utils-1.1.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "9e6a7209defd425b26fc810bb843a46f",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 7245,
            "upload_time": "2024-04-14T07:20:33",
            "upload_time_iso_8601": "2024-04-14T07:20:33.927538Z",
            "url": "https://files.pythonhosted.org/packages/59/49/1676516f19ae31f14b80594eb56affb37bf12232471f04dd4547a04c7129/decorator_utils-1.1.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4f98711b97491f388b1db3625b6aa2afc786e9a37748b5d3c4ca6530b99d5020",
                "md5": "000785e6dc147255a222bf871d030f97",
                "sha256": "c5955b25042b34dae85920bad0dd3c19918c02a7dfa1f0f150e14de1e6fe14bb"
            },
            "downloads": -1,
            "filename": "decorator_utils-1.1.1.tar.gz",
            "has_sig": false,
            "md5_digest": "000785e6dc147255a222bf871d030f97",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 5234,
            "upload_time": "2024-04-14T07:20:35",
            "upload_time_iso_8601": "2024-04-14T07:20:35.154731Z",
            "url": "https://files.pythonhosted.org/packages/4f/98/711b97491f388b1db3625b6aa2afc786e9a37748b5d3c4ca6530b99d5020/decorator_utils-1.1.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-14 07:20:35",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "stickm4n",
    "github_project": "decorator-utils",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "decorator-utils"
}
        
Elapsed time: 0.23700s