Skip to main content

A library for helping configure callbacks with asyncio and aiosignal

Project description

aiocallback:

PyPI version PyPI - Downloads License: MIT Tests pre-commit Checked with mypy Code style: black Imports: isort

An asynchronous helper for writing custom event wrapper class functions and is a good framework for library development made with good typehinting that is built around aiosignal under the hood with a few modifications added onto it for better typehinting and easy usage.

One of my biggest pet peves of all time is when callbacks are not being properly hinted at. This library aims to fix that for vscode and other ides such as when calling the send() function.

Usage:

Dependencies

Installing

The easiest way is to install aiocallback is from PyPI using pip:

pip install aiocallback

Running

First, import the library.

from aiocallback import event, subclassevent, contextevent
import asyncio

class Config:
    """an example of configuring callbacks"""

    # NOTE: Typehinting will be passed to other objects 
    # Thanks in largepart to ParamSpec and Concatenate
    
    # NOTE: @event considers the function to be an abstract method, However you can use a subclassevent to retain typechecking if you need something that isn't so abstract
    @event
    async def on_print(self, cb:str):
        """callbacks for a print method"""

    @subclassevent
    async def on_nonabstract(self, cb:str):
        """a nonabstract method can be called with other events as being part of the signal"""
        print(f"I am callable! \"{cb}\"")




cfg = Config()
# You can also call the append method just like with aiosignal as ours is primarly a subclass of it.
@cfg.on_print
async def test(cb:str):
    print(f"called test {cb}")



async def main():
    # This uses aiosignal under the hood so remeber to freeze the callbacks when your setup is complete
    cfg.on_print.freeze()
    cfg.on_nonabstract.freeze()

    await cfg.on_print.send("Hello world")
    await cfg.on_nonabstract.send("Hello world")

if __name__ == "__main__":
    asyncio.run(main())

TODO:

[] pypi release

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

aiocallback-0.1.0.tar.gz (5.4 kB view details)

Uploaded Source

File details

Details for the file aiocallback-0.1.0.tar.gz.

File metadata

  • Download URL: aiocallback-0.1.0.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for aiocallback-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7a70c9ea83df38b91bbc8837180429f239c861e7f1f86e9fa4e6a785115592ff
MD5 4c40482fb47bd317d85678b869df8501
BLAKE2b-256 9b2306efc09ece0f4cdb585077d9230431183f0d16edfeef358838c3d3003f8e

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