deferrer


Namedeferrer JSON
Version 1.0.0 PyPI version JSON
download
home_pageNone
SummaryFancy `defer` for Python >= 3.12 (partially supported in Python 3.11)
upload_time2025-01-13 08:03:32
maintainerNone
docs_urlNone
authorNone
requires_python<3.14,>=3.11
licenseMIT License Copyright (c) 2024 傅立业(Chris Fu) 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 defer sugar
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Fancy `defer` for Python >= 3.12 (partially supported in Python 3.11)

[![Python package](https://github.com/Azureblade3808/py-deferrer/actions/workflows/python-package.yml/badge.svg)](https://github.com/Azureblade3808/py-deferrer/actions/workflows/python-package.yml)
[![Coverage Status](https://coveralls.io/repos/github/Azureblade3808/py-deferrer/badge.svg)](https://coveralls.io/github/Azureblade3808/py-deferrer)

## Installation

You may install `deferrer` by running `pip install deferrer`.

## Usage

### `defer`

There are two designed ways to use `defer`. You may use either of them, or mix them up.

One is to use `defer` as a syntactic sugar in the form of `defer and ...`. Example:

```python
>>> from deferrer import defer

>>> def f():
...     defer and print("deferred")
...     print("normal")

>>> import sys
>>> if sys.version_info < (3, 12):
...     from deferrer import defer_scope
...     f = defer_scope(f)

>>> f()
normal
deferred

```

The other is to use `defer` as a function wrapper. Example:

```python
>>> from deferrer import defer

>>> def f():
...     defer(print)("deferred")
...     print("normal")

>>> import sys
>>> if sys.version_info < (3, 12):
...     from deferrer import defer_scope
...     f = defer_scope(f)

>>> f()
normal
deferred

```

Note that when the deferred function can be invoke with no arguments, an empty call is not required, which means `defer` can be used as a decorator. Example:

```python
>>> from deferrer import defer

>>> def f():
...     @defer
...     def _():
...         print("deferred")
...
...     print("normal")

>>> import sys
>>> if sys.version_info < (3, 12):
...     from deferrer import defer_scope
...     f = defer_scope(f)

>>> f()
normal
deferred

```

### `defer_scope`

You can use `defer_scope` to declare a code range that deferred actions should be gathered in and executed after.

It's not rare that you may want to defer some actions in a loop and get them executed at the end of each cycle. But unlike some other languages, loops in Python don't create new scopes, which makes it inconvenient to use `defer` in them. `defer_scope` can be used to wrap an iterable to gather deferred actions in each iteration and execute them at the end of the iteration. Example:

```python
>>> from deferrer import defer, defer_scope

>>> def f():
...     for i in defer_scope(range(3)):
...         defer and print("deferred", i)
...         print("normal", i)

>>> f()
normal 0
deferred 0
normal 1
deferred 1
normal 2
deferred 2

```

Sometimes, you may want to use `defer` outside of a function. `defer_scope` can be used as a context manager to gather deferred actions when it's entered and execute them when it's exited. Example:

```python
>>> from deferrer import defer, defer_scope

>>> with defer_scope():
...     # Note that `defer and ...` itself is an expression, and
...     # its value (i.e. the `defer` object) may get printed by
...     # some interpretters.
...     # Use an assignment to suppress the printing behavior.
...     _ = defer and print("deferred")
...     print("normal")
normal
deferred

```

Also, `defer_scope` can be used to wrap a function to help `defer` to work properly in Python 3.11. Note that `locals()` in Python 3.11 returns a copy of the local scope, which makes it impossible for `defer` to inject deferred actions into the real local scope. Without a function level `defer_scope`, deferred actions would be executed immediately after they are evaluated.

## Known Limitations

-   `deferrer` has only been tested on CPython. It may not work on other Python implementations.
-   `defer` must not be used together with `await`. Code like `defer and await ...` is syntactically acceptable but will cause segmentetation fault without being detected beforehand.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "deferrer",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<3.14,>=3.11",
    "maintainer_email": null,
    "keywords": "defer, sugar",
    "author": null,
    "author_email": "Chris Fu <17433201@qq.com>",
    "download_url": "https://files.pythonhosted.org/packages/88/ad/b96ac5aac5b6a99471949ab6161c12ded295eb676330cfd61cc6de52d004/deferrer-1.0.0.tar.gz",
    "platform": null,
    "description": "# Fancy `defer` for Python >= 3.12 (partially supported in Python 3.11)\n\n[![Python package](https://github.com/Azureblade3808/py-deferrer/actions/workflows/python-package.yml/badge.svg)](https://github.com/Azureblade3808/py-deferrer/actions/workflows/python-package.yml)\n[![Coverage Status](https://coveralls.io/repos/github/Azureblade3808/py-deferrer/badge.svg)](https://coveralls.io/github/Azureblade3808/py-deferrer)\n\n## Installation\n\nYou may install `deferrer` by running `pip install deferrer`.\n\n## Usage\n\n### `defer`\n\nThere are two designed ways to use `defer`. You may use either of them, or mix them up.\n\nOne is to use `defer` as a syntactic sugar in the form of `defer and ...`. Example:\n\n```python\n>>> from deferrer import defer\n\n>>> def f():\n...     defer and print(\"deferred\")\n...     print(\"normal\")\n\n>>> import sys\n>>> if sys.version_info < (3, 12):\n...     from deferrer import defer_scope\n...     f = defer_scope(f)\n\n>>> f()\nnormal\ndeferred\n\n```\n\nThe other is to use `defer` as a function wrapper. Example:\n\n```python\n>>> from deferrer import defer\n\n>>> def f():\n...     defer(print)(\"deferred\")\n...     print(\"normal\")\n\n>>> import sys\n>>> if sys.version_info < (3, 12):\n...     from deferrer import defer_scope\n...     f = defer_scope(f)\n\n>>> f()\nnormal\ndeferred\n\n```\n\nNote that when the deferred function can be invoke with no arguments, an empty call is not required, which means `defer` can be used as a decorator. Example:\n\n```python\n>>> from deferrer import defer\n\n>>> def f():\n...     @defer\n...     def _():\n...         print(\"deferred\")\n...\n...     print(\"normal\")\n\n>>> import sys\n>>> if sys.version_info < (3, 12):\n...     from deferrer import defer_scope\n...     f = defer_scope(f)\n\n>>> f()\nnormal\ndeferred\n\n```\n\n### `defer_scope`\n\nYou can use `defer_scope` to declare a code range that deferred actions should be gathered in and executed after.\n\nIt's not rare that you may want to defer some actions in a loop and get them executed at the end of each cycle. But unlike some other languages, loops in Python don't create new scopes, which makes it inconvenient to use `defer` in them. `defer_scope` can be used to wrap an iterable to gather deferred actions in each iteration and execute them at the end of the iteration. Example:\n\n```python\n>>> from deferrer import defer, defer_scope\n\n>>> def f():\n...     for i in defer_scope(range(3)):\n...         defer and print(\"deferred\", i)\n...         print(\"normal\", i)\n\n>>> f()\nnormal 0\ndeferred 0\nnormal 1\ndeferred 1\nnormal 2\ndeferred 2\n\n```\n\nSometimes, you may want to use `defer` outside of a function. `defer_scope` can be used as a context manager to gather deferred actions when it's entered and execute them when it's exited. Example:\n\n```python\n>>> from deferrer import defer, defer_scope\n\n>>> with defer_scope():\n...     # Note that `defer and ...` itself is an expression, and\n...     # its value (i.e. the `defer` object) may get printed by\n...     # some interpretters.\n...     # Use an assignment to suppress the printing behavior.\n...     _ = defer and print(\"deferred\")\n...     print(\"normal\")\nnormal\ndeferred\n\n```\n\nAlso, `defer_scope` can be used to wrap a function to help `defer` to work properly in Python 3.11. Note that `locals()` in Python 3.11 returns a copy of the local scope, which makes it impossible for `defer` to inject deferred actions into the real local scope. Without a function level `defer_scope`, deferred actions would be executed immediately after they are evaluated.\n\n## Known Limitations\n\n-   `deferrer` has only been tested on CPython. It may not work on other Python implementations.\n-   `defer` must not be used together with `await`. Code like `defer and await ...` is syntactically acceptable but will cause segmentetation fault without being detected beforehand.\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2024 \u5085\u7acb\u4e1a\uff08Chris Fu\uff09  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": "Fancy `defer` for Python >= 3.12 (partially supported in Python 3.11)",
    "version": "1.0.0",
    "project_urls": {
        "Bug Tracker": "https://github.com/Azureblade3808/py-deferrer/issues",
        "Homepage": "https://github.com/Azureblade3808/py-deferrer"
    },
    "split_keywords": [
        "defer",
        " sugar"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c148a60fbcd14cd6cb7b5d36f76f63c554bf0a958d3e7ad4cf73da1fe4a863be",
                "md5": "9a24ed07dbd70690aec45caa37d694ec",
                "sha256": "536bd09ada03b4236ff621c4df96b23b268957c03e47811743882c3a2e98e755"
            },
            "downloads": -1,
            "filename": "deferrer-1.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "9a24ed07dbd70690aec45caa37d694ec",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<3.14,>=3.11",
            "size": 14574,
            "upload_time": "2025-01-13T08:03:30",
            "upload_time_iso_8601": "2025-01-13T08:03:30.231295Z",
            "url": "https://files.pythonhosted.org/packages/c1/48/a60fbcd14cd6cb7b5d36f76f63c554bf0a958d3e7ad4cf73da1fe4a863be/deferrer-1.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "88adb96ac5aac5b6a99471949ab6161c12ded295eb676330cfd61cc6de52d004",
                "md5": "6a58c40c4fafe83cdb9698a2ac7fa8cf",
                "sha256": "39aa923a556b0c6e2ad36a8f91528023276b3fbd6b54d3b2fd565083a1f3c2b1"
            },
            "downloads": -1,
            "filename": "deferrer-1.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "6a58c40c4fafe83cdb9698a2ac7fa8cf",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<3.14,>=3.11",
            "size": 14057,
            "upload_time": "2025-01-13T08:03:32",
            "upload_time_iso_8601": "2025-01-13T08:03:32.986657Z",
            "url": "https://files.pythonhosted.org/packages/88/ad/b96ac5aac5b6a99471949ab6161c12ded295eb676330cfd61cc6de52d004/deferrer-1.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-01-13 08:03:32",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Azureblade3808",
    "github_project": "py-deferrer",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "deferrer"
}
        
Elapsed time: 7.74474s