Skip to main content

Lazy evaluation for function/method/property getter

Project description

Lazy Evaluation

build shields License: MIT

Lazy evaluate function/class method/class property getter. The target will be evaluated once and only once on first call , and concurrent calls will get the result immediately once the target is ready and gets the same exception when the target raises some exception.

Compatible with both sync and async.

Installation

pip install lazy-async

Example

from lazy_async import lazy, lazy_getter
import asyncio
import time

class ExampleClass:
    def __init__(self):
        self.sync_called = 0
        self.async_called = 0

    @lazy
    def func1(self):
        time.sleep(5)
        self.sync_called += 1
        return 'something'

    @lazy
    async def func2(self):
        await asyncio.sleep(5)
        self.async_called += 1
        return 'something'

    @lazy
    def func3(self):
        time.sleep(5)
        raise ValueError('SomeException')

    @lazy
    async def func4(self):
        await asyncio.sleep(5)
        raise ValueError('SomeException')

    @lazy_getter
    def func5(self):
        time.sleep(5)
        return 'something'

    @lazy_getter
    async def func6(self):
        await asyncio.sleep(5)
        return 'something'

See unittest for detailed example.

Project details


Download files

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

Files for lazy-async, version 0.1
Filename, size File type Python version Upload date Hashes
Filename, size lazy-async-0.1.tar.gz (2.3 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page