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 only call the function 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()
>>> await instance.value
loading value
123
>>> await instance.value
123

AwaitLoader

If you have multiple cached properties and would like to load them concurrently, you can subclass AwaitLoader. This makes your class instance awaitable and will load all @async_cached_property fields.

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.

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.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.1.tar.gz (14.2 kB view details)

Uploaded Source

Built Distribution

async_property-0.1.1-py2.py3-none-any.whl (9.5 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: async_property-0.1.1.tar.gz
  • Upload date:
  • Size: 14.2 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.1.tar.gz
Algorithm Hash digest
SHA256 1a377ff936aa1ecb7b3736f36c05f3ab45cbe057d9e5ffaeb91ebb88b88aa3ad
MD5 3e382a4ea3d4e5582cfd80a4870896d1
BLAKE2b-256 fdbc8c4620b5106c9332bd7de30ad3d3809b18542672aadc5f390aa9b67b9f04

See more details on using hashes here.

File details

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

File metadata

  • Download URL: async_property-0.1.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 9.5 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.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 5d393560f072bfb36db716f0f39665577b3d457d3218a7c48b89c7ba15a2e0fd
MD5 ac030e4ef3e9b4d7b5c3b4120d795edb
BLAKE2b-256 c8757e0a7697a3bc8220cf6ed41ce9a480478b41831e7f07fd0760778fcb2aa8

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