An asynchronous helper framework for writing custom event wrapper class functions made with good typehinting that is based off aiosignal with better modifications added for better typehinting and easier usage with tools such as pyright or mypy allowing for arguments to be properly typehinted at when performing any created callback which ultimately means less confusion and more action.
One of my biggest pet peves of all time is when static type-checkers don't pick up what functions parameters are being used. This library aims to fix static typecheckers when send() functions are being used, so that developers aren't second guessing what different paremeters are needed. This is a big advantage over aiosignal and was the main reson behind it's creation.
Aiocallback should be used when dealing with creating custom context objects or callbacks. An example might be scraping an api by a given hour and calling for that data that can be defined by multiple functions. However, there are many more creative ways to use this library.
- frozenlist we dropped aiosignal in favor of frozenlist temporarly until aiosignal plans to support ParamSpec There isn't much demand for it yet but I did help revive that library recently.
- typing-extensions Typehinting for Python 3.9, plan to drop typing-extensions when 3.9 hits End of Life so that ParamSpec can be utilized to it's fullest potential.
The easiest way is to install aiocallback is from PyPI using pip:
pip install aiocallback
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())
There's an alternative way to use aiocallback where you don't need to freeze many Configurable event descriptors at a time. You should use it if your not planning to use a dataclass although we plan to implement a special EventList for msgspec.
from aiocallback import EventList, event
class MyEvents(EventList):
@event
async def on_event(self, item:str):...
events = MyEvents()
# all events get frozen for you and this method is built-in.
events.freeze()
- aiosignal I am a contributor over there and I revived this project pretty recently. It's a very good replacement if you want speed over control.
- Trusted Publishing
- Smaller improvements to aiocallback like documentation for future readthedocs page.