Skip to main content

Python decorator for async properties.

Project description

async_property

https://img.shields.io/pypi/v/async_property.svg https://img.shields.io/travis/ryananguiano/async_property.svg Documentation Status Updates

Python decorator for async properties.

Install

To install async_property, run this command in your terminal:

$ pip install async-property

Or if you have pipenv:

$ pipenv install async-property

Usage

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

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

The property remote_value now returns an awaitable coroutine.

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.

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'
>>> 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.

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

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

>>> instance = await Foo()
>>> 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.

  • Cached properties use asyncio.Lock to ensure function is only called once.

  • AwaitLoader will call await instance.load(), if it exists, before loading properties.

Credits

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

The ObjectProxy class was taken from wrapt library by Graham Dumpleton.

History

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

async_property-0.1.4.tar.gz (14.6 kB view details)

Uploaded Source

Built Distribution

async_property-0.1.4-py2.py3-none-any.whl (9.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file async_property-0.1.4.tar.gz.

File metadata

  • Download URL: async_property-0.1.4.tar.gz
  • Upload date:
  • Size: 14.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for async_property-0.1.4.tar.gz
Algorithm Hash digest
SHA256 8fcaad69ab05607f3467730869e38c1c4bda583497421d67865390153f3e551d
MD5 af745c35851043cab7e35cec5b0267ae
BLAKE2b-256 26dabb91ab7149b8aa9158a9271d4a61504ba6298d564a6d6db373f7529afcc3

See more details on using hashes here.

File details

Details for the file async_property-0.1.4-py2.py3-none-any.whl.

File metadata

  • Download URL: async_property-0.1.4-py2.py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for async_property-0.1.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 513d013534c7d75fd31b8f50ae233c5d8f018d61521f3ad886937841ce943512
MD5 39953a56d16697e3f30f8beeba044329
BLAKE2b-256 d0426099789e8a5de83e30907ddaf10f2416afb7268902faa3f7d2ae38db2b34

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page