async-property


Nameasync-property JSON
Version 0.2.2 PyPI version JSON
download
home_pagehttps://github.com/ryananguiano/async_property
SummaryPython decorator for async properties.
upload_time2023-07-03 17:21:55
maintainer
docs_urlNone
authorRyan Anguiano
requires_python
licenseMIT license
keywords async_property
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage
            ==============
async_property
==============


.. image:: https://img.shields.io/pypi/v/async_property.svg
    :target: https://pypi.org/project/async-property/

.. image:: https://app.travis-ci.com/ryananguiano/async_property.svg?branch=master
    :target: https://app.travis-ci.com/github/ryananguiano/async_property

.. image:: https://readthedocs.org/projects/async-property/badge/?version=latest
    :target: https://async-property.readthedocs.io/en/latest/?badge=latest
    :alt: Documentation Status

.. image:: https://pyup.io/repos/github/ryananguiano/async_property/shield.svg
    :target: https://pyup.io/repos/github/ryananguiano/async_property/
    :alt: Updates


Python decorator for async properties.

* Python: 3.7+
* Free software: MIT license
* Documentation: https://async-property.readthedocs.io
* Package: https://pypi.org/project/async-property
* Source code: https://github.com/ryananguiano/async_property

Install
-------

To install async_property, run this command in your terminal:

.. code-block:: console

    $ pip install async-property


Or if you have pipenv:

.. code-block:: console

    $ pipenv install async-property


Usage
-----

You can use ``@async_property`` just as you would with ``@property``, but on an async function.

.. code-block:: python

    class Foo:
        @async_property
        async def remote_value(self):
            return await get_remote_value()

The property ``remote_value`` now returns an awaitable coroutine.

.. code-block:: python

    instance = Foo()
    await instance.remote_value


Cached Properties
~~~~~~~~~~~~~~~~~

``@async_cached_property`` will call the function only once. Subsequent awaits to the property will return a cached value.

.. code-block:: python

    class Foo:
        @async_cached_property
        async def value(self):
            print('loading value')
            return 123

    >>> instance = Foo()
    >>> instance.value
    <AwaitableOnly "Foo.value">

    >>> await instance.value
    loading value
    123
    >>> await instance.value
    123
    >>> instance.value
    123

    >>> instance.value = 'abc'
    >>> instance.value
    'abc'
    >>> await instance.value
    'abc'

    >>> del instance.value
    >>> await instance.value
    loading value
    123


AwaitLoader
~~~~~~~~~~~

If you have an object with multiple cached properties, you can subclass ``AwaitLoader``. This will make your class instances awaitable and will load all ``@async_cached_property`` fields concurrently. ``AwaitLoader`` will call ``await instance.load()``, if it exists, before loading properties.

.. code-block:: python


    class Foo(AwaitLoader):
        async def load(self):
            print('load called')

        @async_cached_property
        async def db_lookup(self):
            return 'success'

        @async_cached_property
        async def api_call(self):
            print('calling api')
            return 'works every time'

    >>> instance = await Foo()
    load called
    calling api
    >>> instance.db_lookup
    'success'
    >>> instance.api_call
    'works every time'

Features
--------

* Both regular and cached property.
* Cached properties can be accessed multiple times without repeating function call.
* Uses asyncio.Lock to ensure cached functions are called only once.
* Full test coverage with py.test


Credits
-------

This package was created with Cookiecutter_ and the `audreyr/cookiecutter-pypackage`_ project template.

.. _Cookiecutter: https://github.com/audreyr/cookiecutter
.. _`audreyr/cookiecutter-pypackage`: https://github.com/audreyr/cookiecutter-pypackage


The ObjectProxy_ class was taken from wrapt_ library by Graham Dumpleton.

.. _ObjectProxy: https://github.com/GrahamDumpleton/wrapt/blob/master/src/wrapt/wrappers.py
.. _wrapt: https://github.com/GrahamDumpleton/wrapt


=======
History
=======

0.2.2 (2023-07-03)
------------------

* Add Python 3.11 support and drop Python 3.6.

0.2.1 (2019-04-13)
------------------

* Update docs and readme

0.2.0 (2019-04-12)
------------------

* Use instance state to hold cache and locks

0.1.4 (2019-04-12)
------------------

* Fix inheritance issues on AwaitLoader

0.1.3 (2019-04-12)
------------------

* Cleanup code

0.1.2 (2019-04-12)
------------------

* Fix asyncio.Lock issues

0.1.1 (2019-04-11)
------------------

* Complete test coverage and update readme


0.1.0 (2019-04-11)
------------------

* First release on PyPI.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/ryananguiano/async_property",
    "name": "async-property",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "async_property",
    "author": "Ryan Anguiano",
    "author_email": "ryan.anguiano@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/a7/12/900eb34b3af75c11b69d6b78b74ec0fd1ba489376eceb3785f787d1a0a1d/async_property-0.2.2.tar.gz",
    "platform": null,
    "description": "==============\nasync_property\n==============\n\n\n.. image:: https://img.shields.io/pypi/v/async_property.svg\n    :target: https://pypi.org/project/async-property/\n\n.. image:: https://app.travis-ci.com/ryananguiano/async_property.svg?branch=master\n    :target: https://app.travis-ci.com/github/ryananguiano/async_property\n\n.. image:: https://readthedocs.org/projects/async-property/badge/?version=latest\n    :target: https://async-property.readthedocs.io/en/latest/?badge=latest\n    :alt: Documentation Status\n\n.. image:: https://pyup.io/repos/github/ryananguiano/async_property/shield.svg\n    :target: https://pyup.io/repos/github/ryananguiano/async_property/\n    :alt: Updates\n\n\nPython decorator for async properties.\n\n* Python: 3.7+\n* Free software: MIT license\n* Documentation: https://async-property.readthedocs.io\n* Package: https://pypi.org/project/async-property\n* Source code: https://github.com/ryananguiano/async_property\n\nInstall\n-------\n\nTo install async_property, run this command in your terminal:\n\n.. code-block:: console\n\n    $ pip install async-property\n\n\nOr if you have pipenv:\n\n.. code-block:: console\n\n    $ pipenv install async-property\n\n\nUsage\n-----\n\nYou can use ``@async_property`` just as you would with ``@property``, but on an async function.\n\n.. code-block:: python\n\n    class Foo:\n        @async_property\n        async def remote_value(self):\n            return await get_remote_value()\n\nThe property ``remote_value`` now returns an awaitable coroutine.\n\n.. code-block:: python\n\n    instance = Foo()\n    await instance.remote_value\n\n\nCached Properties\n~~~~~~~~~~~~~~~~~\n\n``@async_cached_property`` will call the function only once. Subsequent awaits to the property will return a cached value.\n\n.. code-block:: python\n\n    class Foo:\n        @async_cached_property\n        async def value(self):\n            print('loading value')\n            return 123\n\n    >>> instance = Foo()\n    >>> instance.value\n    <AwaitableOnly \"Foo.value\">\n\n    >>> await instance.value\n    loading value\n    123\n    >>> await instance.value\n    123\n    >>> instance.value\n    123\n\n    >>> instance.value = 'abc'\n    >>> instance.value\n    'abc'\n    >>> await instance.value\n    'abc'\n\n    >>> del instance.value\n    >>> await instance.value\n    loading value\n    123\n\n\nAwaitLoader\n~~~~~~~~~~~\n\nIf you have an object with multiple cached properties, you can subclass ``AwaitLoader``. This will make your class instances awaitable and will load all ``@async_cached_property`` fields concurrently. ``AwaitLoader`` will call ``await instance.load()``, if it exists, before loading properties.\n\n.. code-block:: python\n\n\n    class Foo(AwaitLoader):\n        async def load(self):\n            print('load called')\n\n        @async_cached_property\n        async def db_lookup(self):\n            return 'success'\n\n        @async_cached_property\n        async def api_call(self):\n            print('calling api')\n            return 'works every time'\n\n    >>> instance = await Foo()\n    load called\n    calling api\n    >>> instance.db_lookup\n    'success'\n    >>> instance.api_call\n    'works every time'\n\nFeatures\n--------\n\n* Both regular and cached property.\n* Cached properties can be accessed multiple times without repeating function call.\n* Uses asyncio.Lock to ensure cached functions are called only once.\n* Full test coverage with py.test\n\n\nCredits\n-------\n\nThis package was created with Cookiecutter_ and the `audreyr/cookiecutter-pypackage`_ project template.\n\n.. _Cookiecutter: https://github.com/audreyr/cookiecutter\n.. _`audreyr/cookiecutter-pypackage`: https://github.com/audreyr/cookiecutter-pypackage\n\n\nThe ObjectProxy_ class was taken from wrapt_ library by Graham Dumpleton.\n\n.. _ObjectProxy: https://github.com/GrahamDumpleton/wrapt/blob/master/src/wrapt/wrappers.py\n.. _wrapt: https://github.com/GrahamDumpleton/wrapt\n\n\n=======\nHistory\n=======\n\n0.2.2 (2023-07-03)\n------------------\n\n* Add Python 3.11 support and drop Python 3.6.\n\n0.2.1 (2019-04-13)\n------------------\n\n* Update docs and readme\n\n0.2.0 (2019-04-12)\n------------------\n\n* Use instance state to hold cache and locks\n\n0.1.4 (2019-04-12)\n------------------\n\n* Fix inheritance issues on AwaitLoader\n\n0.1.3 (2019-04-12)\n------------------\n\n* Cleanup code\n\n0.1.2 (2019-04-12)\n------------------\n\n* Fix asyncio.Lock issues\n\n0.1.1 (2019-04-11)\n------------------\n\n* Complete test coverage and update readme\n\n\n0.1.0 (2019-04-11)\n------------------\n\n* First release on PyPI.\n",
    "bugtrack_url": null,
    "license": "MIT license",
    "summary": "Python decorator for async properties.",
    "version": "0.2.2",
    "project_urls": {
        "Homepage": "https://github.com/ryananguiano/async_property"
    },
    "split_keywords": [
        "async_property"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c7809f608d13b4b3afcebd1dd13baf9551c95fc424d6390e4b1cfd7b1810cd06",
                "md5": "9aea6230c2e22a112b66eced8734f149",
                "sha256": "8924d792b5843994537f8ed411165700b27b2bd966cefc4daeefc1253442a9d7"
            },
            "downloads": -1,
            "filename": "async_property-0.2.2-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "9aea6230c2e22a112b66eced8734f149",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": null,
            "size": 9546,
            "upload_time": "2023-07-03T17:21:54",
            "upload_time_iso_8601": "2023-07-03T17:21:54.293993Z",
            "url": "https://files.pythonhosted.org/packages/c7/80/9f608d13b4b3afcebd1dd13baf9551c95fc424d6390e4b1cfd7b1810cd06/async_property-0.2.2-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a712900eb34b3af75c11b69d6b78b74ec0fd1ba489376eceb3785f787d1a0a1d",
                "md5": "048f9a5976ba792786374f999ad1a0e3",
                "sha256": "17d9bd6ca67e27915a75d92549df64b5c7174e9dc806b30a3934dc4ff0506380"
            },
            "downloads": -1,
            "filename": "async_property-0.2.2.tar.gz",
            "has_sig": false,
            "md5_digest": "048f9a5976ba792786374f999ad1a0e3",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 16523,
            "upload_time": "2023-07-03T17:21:55",
            "upload_time_iso_8601": "2023-07-03T17:21:55.688545Z",
            "url": "https://files.pythonhosted.org/packages/a7/12/900eb34b3af75c11b69d6b78b74ec0fd1ba489376eceb3785f787d1a0a1d/async_property-0.2.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-07-03 17:21:55",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "ryananguiano",
    "github_project": "async_property",
    "travis_ci": true,
    "coveralls": true,
    "github_actions": false,
    "tox": true,
    "lcname": "async-property"
}
        
Elapsed time: 0.62313s