Skip to main content

truely async properties

Project description

Install

pip3 install aioproperty

Documentation

You can find documentation here

Description

aioproperty presents async properties with both async getter and setter in one place.

Example:

from aioproperty import aioproperty
import asyncio

class SomeClass:
    
    @aioproperty
    async def hello(self, value):
        await asyncio.sleep(1)
        return value

some_obj = SomeClass()
some_obj.hello = 'hello'

async def run():
    print(await some_obj.hello)

asyncio.run(run())

aioproperty is not a property in a classic meaning, it keeps values inside asincio tasks. Once you set a new value, it is scheduled in a task. If any task is running now, it will wait untill it is finished. When you get value, you actually get a current task, and you can await it to get a value. More of that: you can use math without awaiting like that:

other = some_obj.hello + ' byby'
print(await other)

We also introduce chaining:

class SomeClass:
    
    @aioproperty
    async def hello(self, value):
        await asyncio.sleep(1)
        return value
    
    @hello.chain
    async def some_more(self, value):
        push_value(value)

Chains works like a reducer. It applies each function to a new value iteratively. It can return value or return nothing. If nothing is return, the value will be kept asis. Any chained function is inserted by default in the end of chain, but you can control it with a priority parameter, or with is_first parameter. If is_first is True, it will be inserted in the beginning of the chain.

You can also use inheritance and modify inhereted properties with our special decorator inject:

class Parent:
    
    @aioproperty
    async def hello(self, value):
        await asyncio.sleep(1)
        return value
    
    @hello.chain
    async def some_more(self, value):
        push_value(value)

class Child(Parent):
    
    @inject(Parent.hello, priority=100)
    async def injected_method(self, value):
        print('hello from injection')
    
    #another form of injection using name of a property
    @inject('hello')
    async def injected_method_2(self, value):
        print('hello from injection 2')

Read more in our docs

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

aioproperty-0.3.2.tar.gz (12.0 kB view details)

Uploaded Source

File details

Details for the file aioproperty-0.3.2.tar.gz.

File metadata

  • Download URL: aioproperty-0.3.2.tar.gz
  • Upload date:
  • Size: 12.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.5.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.1

File hashes

Hashes for aioproperty-0.3.2.tar.gz
Algorithm Hash digest
SHA256 14700e5dfa3e92cc6985742e9fac71b77122b2b885689c9ca7eee4ff30d8c5ea
MD5 cc57d183d9ad62e6266cc8d6fa34c94b
BLAKE2b-256 3725b1d32701f8368f2c02502048bc0e55d231cc6f97ff4f6b9260715faee9b0

See more details on using hashes here.

Supported by

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