Skip to main content

Python 3.7+ async decorators and testing tools

Project description

Build Status Coverage Status

atools

Python 3.7+ async-enabled decorators and tools including

  • memoize - a function decorator for sync and async functions that memoizes results.
  • async_test (coming soon) - a test class/function decorator that enables unittest.TestCase functions to be async.
  • DevMock (coming soon) - a variant of MagicMock that allows first occurrence of a call to pass through to original code and caches results on filesystem. Subsequent calls to DevMock return cached results.

memoize

Decorates a function call and caches return value for given inputs.

This decorator is not thread safe but is safe with concurrent awaits.

If 'size' is provided, memoize will only retain up to 'size' return values.

If 'expire' is provided, memoize will only retain return values for up to 'expire' duration.
  'expire' duration is given in seconds or a string such as '10s', '1m', or '1d1h1m1s' where
  days, hours, minutes, and seconds are represented by 'd', 'h', 'm', and 's' respectively.

If 'pass_unhashable' is True, memoize will not remember calls that are made with parameters
  that cannot be hashed instead of raising an exception.

if 'thread_safe' is True, the decorator is guaranteed to be thread safe.

Examples:

    - Body will run once for unique input 'bar' and result is cached.
        @memoize
        def foo(bar) -> Any: ...

    - Same as above, but async. Concurrent calls with the same 'bar' are safe and will only
      generate one call
        @memoize
        async def foo(bar) -> Any: ...

    - Calls to foo(1), foo(bar=1), and foo(1, baz='baz') are equivalent and only cached once
        @memoize
        def foo(bar, baz='baz'): ...

    - Only 10 items are cached. Acts as an LRU.
        @memoize(size=10)
        def foo(bar, baz) -> Any: ...

   - Items are evicted after 1 minute.
        @memoize(expire='1m')
        def foo(bar) -> Any: ...

async_test

Coming Soon

DevMock

Coming Soon

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

atools-0.1.2.tar.gz (6.1 kB view hashes)

Uploaded Source

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