ezdecs


Nameezdecs JSON
Version 1.0.0 PyPI version JSON
download
home_pagehttps://github.com/none-None1/ezdecs
SummarySome decorators that simplify Python code
upload_time2025-10-13 05:49:58
maintainerNone
docs_urlNone
authorNone1
requires_pythonNone
licenseNone
keywords decorator easy
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # ezdecs: Some decorators that simplify Python code
Wanna time your code execution easily? Don't wanna spend too much time on input validation? Maybe ezdecs can help!

## timer(): Timing execution

By adding `@timer` before a function, it times the function when the function is called.

```python
from ezdecs import *
@timer
def f():
    print('Hello, world!')
    return 3
print(f())
'''
Example output:
Hello, world!
(3, 6.939999999966417e-05)
'''
```

## noexcept() and cexcept(): Silence exceptions

Using the decorator `@noexcept`, any exception raised is silenced.
```python
from ezdecs import *
@noexcept
def divide(a,b):
    return a/b
print(divide(9,3)) # 3.0
print(divide(1,0)) # None
```

If you still wanna know what exception is raised, use `@cexcept`.
```python
from ezdecs import *
@cexcept
def divide(a,b):
    return a/b
print(divide(9,3)) # (3.0, None)
print(divide(1,0)) # (None, ZeroDivisionError('division by zero'))
```

## tnexcept(): Retry when an exception occurs
By using `@tnexcept`, the function is repeated until no exception occurs. This is especially useful for input validation
and retrying.

For example, instead of writing:
```python
def read_number():
    while 1:
        try:
            result=int(input('Please enter a number: '))
        except:
            pass
        else:
            return result
print(read_number())
```
All you have to write is:
```python
from ezdecs import *
@tnexcept
def read_number():
    return int(input('Please enter a number: '))
print(read_number())
'''
Example interaction:
Please enter a number: a
Please enter a number: 3.14
Please enter a number: 3
3
'''
```

The keyword argument `handler` allows printing custom error messages when encountering invalid input by raising `AssertionError`.

```python
from ezdecs import *
@tnexcept(handler=print)
def enter_password():
    psw=input('Please enter password: ')
    assert psw=="123","Incorrect password."
enter_password()
print('Correct!')
'''
Example interaction:
Please enter password: a
Incorrect password.
Please enter password: 1234
Incorrect password.
Please enter password: 123
Correct!
'''
```

For documentation on other decorators see docstring.

## Aliases
In order to shorten code, every decorator has a two-character alias:
```text
TM=timer
NE=noexcept
CE=cexcept
BL=block
NL=nolog
TE=tnexcept
RP=repeat
FE=foreach
```

## Problems with recursion
Sometimes using these decorators on recursive functions may cause problems.

The following is an attempt to time a recursive Fibonacci function:
```python
from ezdecs import *
@TM
def f(n):
    if n<2:
        return n
    return f(n-1)+f(n-2)
print(f(5))
```
Instead of the time and result, a complex nested tuple is returned. That's because after being wrapped by the decorator, 
`f` now returns a tuple instead of an integer.

To make it work correctly, the `__wrapped__` attribute can be used to retrieve the unwrapped function:
```python
from ezdecs import *
@TM
def f(n):
    if n<2:
        return n
    fw=f.__wrapped__
    return fw(n-1)+fw(n-2)
print(f(5))
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/none-None1/ezdecs",
    "name": "ezdecs",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "decorator, easy",
    "author": "None1",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/76/91/dbd64e66471469b6fb677c13eba72b0d01561f0892703495762f4a1611b1/ezdecs-1.0.0.tar.gz",
    "platform": null,
    "description": "# ezdecs: Some decorators that simplify Python code\r\nWanna time your code execution easily? Don't wanna spend too much time on input validation? Maybe ezdecs can help!\r\n\r\n## timer(): Timing execution\r\n\r\nBy adding `@timer` before a function, it times the function when the function is called.\r\n\r\n```python\r\nfrom ezdecs import *\r\n@timer\r\ndef f():\r\n    print('Hello, world!')\r\n    return 3\r\nprint(f())\r\n'''\r\nExample output:\r\nHello, world!\r\n(3, 6.939999999966417e-05)\r\n'''\r\n```\r\n\r\n## noexcept() and cexcept(): Silence exceptions\r\n\r\nUsing the decorator `@noexcept`, any exception raised is silenced.\r\n```python\r\nfrom ezdecs import *\r\n@noexcept\r\ndef divide(a,b):\r\n    return a/b\r\nprint(divide(9,3)) # 3.0\r\nprint(divide(1,0)) # None\r\n```\r\n\r\nIf you still wanna know what exception is raised, use `@cexcept`.\r\n```python\r\nfrom ezdecs import *\r\n@cexcept\r\ndef divide(a,b):\r\n    return a/b\r\nprint(divide(9,3)) # (3.0, None)\r\nprint(divide(1,0)) # (None, ZeroDivisionError('division by zero'))\r\n```\r\n\r\n## tnexcept(): Retry when an exception occurs\r\nBy using `@tnexcept`, the function is repeated until no exception occurs. This is especially useful for input validation\r\nand retrying.\r\n\r\nFor example, instead of writing:\r\n```python\r\ndef read_number():\r\n    while 1:\r\n        try:\r\n            result=int(input('Please enter a number: '))\r\n        except:\r\n            pass\r\n        else:\r\n            return result\r\nprint(read_number())\r\n```\r\nAll you have to write is:\r\n```python\r\nfrom ezdecs import *\r\n@tnexcept\r\ndef read_number():\r\n    return int(input('Please enter a number: '))\r\nprint(read_number())\r\n'''\r\nExample interaction:\r\nPlease enter a number: a\r\nPlease enter a number: 3.14\r\nPlease enter a number: 3\r\n3\r\n'''\r\n```\r\n\r\nThe keyword argument `handler` allows printing custom error messages when encountering invalid input by raising `AssertionError`.\r\n\r\n```python\r\nfrom ezdecs import *\r\n@tnexcept(handler=print)\r\ndef enter_password():\r\n    psw=input('Please enter password: ')\r\n    assert psw==\"123\",\"Incorrect password.\"\r\nenter_password()\r\nprint('Correct!')\r\n'''\r\nExample interaction:\r\nPlease enter password: a\r\nIncorrect password.\r\nPlease enter password: 1234\r\nIncorrect password.\r\nPlease enter password: 123\r\nCorrect!\r\n'''\r\n```\r\n\r\nFor documentation on other decorators see docstring.\r\n\r\n## Aliases\r\nIn order to shorten code, every decorator has a two-character alias:\r\n```text\r\nTM=timer\r\nNE=noexcept\r\nCE=cexcept\r\nBL=block\r\nNL=nolog\r\nTE=tnexcept\r\nRP=repeat\r\nFE=foreach\r\n```\r\n\r\n## Problems with recursion\r\nSometimes using these decorators on recursive functions may cause problems.\r\n\r\nThe following is an attempt to time a recursive Fibonacci function:\r\n```python\r\nfrom ezdecs import *\r\n@TM\r\ndef f(n):\r\n    if n<2:\r\n        return n\r\n    return f(n-1)+f(n-2)\r\nprint(f(5))\r\n```\r\nInstead of the time and result, a complex nested tuple is returned. That's because after being wrapped by the decorator, \r\n`f` now returns a tuple instead of an integer.\r\n\r\nTo make it work correctly, the `__wrapped__` attribute can be used to retrieve the unwrapped function:\r\n```python\r\nfrom ezdecs import *\r\n@TM\r\ndef f(n):\r\n    if n<2:\r\n        return n\r\n    fw=f.__wrapped__\r\n    return fw(n-1)+fw(n-2)\r\nprint(f(5))\r\n```\r\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Some decorators that simplify Python code",
    "version": "1.0.0",
    "project_urls": {
        "Homepage": "https://github.com/none-None1/ezdecs"
    },
    "split_keywords": [
        "decorator",
        " easy"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "09f1fbc6bb6d053c2e746583c0c309d3522c7c9e94290f8f7e110c7bc4ebc5e4",
                "md5": "13d30171354d86b05eecfaeb871e1f64",
                "sha256": "fa2d03de8a5a53868ebed6e79c85af85ded60643c61a227f3b65e763713c6d2f"
            },
            "downloads": -1,
            "filename": "ezdecs-1.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "13d30171354d86b05eecfaeb871e1f64",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 5005,
            "upload_time": "2025-10-13T05:49:56",
            "upload_time_iso_8601": "2025-10-13T05:49:56.866920Z",
            "url": "https://files.pythonhosted.org/packages/09/f1/fbc6bb6d053c2e746583c0c309d3522c7c9e94290f8f7e110c7bc4ebc5e4/ezdecs-1.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7691dbd64e66471469b6fb677c13eba72b0d01561f0892703495762f4a1611b1",
                "md5": "b6f99a8e29a76071ba79e9e6b61cb08c",
                "sha256": "09377b10ec86c59e402e03a9105f6c339e76a5a1bc05265e943ffb00f6fa596c"
            },
            "downloads": -1,
            "filename": "ezdecs-1.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "b6f99a8e29a76071ba79e9e6b61cb08c",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 3257,
            "upload_time": "2025-10-13T05:49:58",
            "upload_time_iso_8601": "2025-10-13T05:49:58.335260Z",
            "url": "https://files.pythonhosted.org/packages/76/91/dbd64e66471469b6fb677c13eba72b0d01561f0892703495762f4a1611b1/ezdecs-1.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-10-13 05:49:58",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "none-None1",
    "github_project": "ezdecs",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "ezdecs"
}
        
Elapsed time: 0.84243s