Package: events-manager
Events ManagerPython implementation of theEvents Managementsystem (akaObserver pattern).Getting startedRequirementsPython >= 3.8Installationpipinstallevents-managerUsageListening and emitting an EventListener is invoked when the event is emitted.fromasyncioimportrun,sleepfromevents_managerimportEvent,emit,listenclassFooEvent(Event):passdeffoo_listener(event:FooEvent):print(f"'foo_listener' invoked with{event}")asyncdefmain():listen(FooEvent,foo_listener)print("Emitting event...")emit(FooEvent())# do the other stuff...awaitsleep(1)if__name__=='__main__':run(main())Output:Emitting event...
'foo_listener' invoked with <__main__.FooEvent object at 0x7fdce6f5a0a0>

Process finished with exit code 0syncandasynclistenersfromasyncioimportrun,sleepfromevents_managerimportEvent,emit,listenclassFooEvent(Event):passasyncdeffoo_listener(event:FooEvent):print(f"'foo_listener' invoked with{event}")asyncdefmain():listen(FooEvent,foo_listener)print("Emitting event...")emit(FooEvent())# do the other stuff...awaitsleep(1)if__name__=='__main__':run(main())Output:Emitting event...
'foo_listener' invoked with <__main__.FooEvent object at 0x7f81e76ad0a0>

Process finished with exit code 0argsandkwargssupportThelistenmethod supports alsoargsandkwargsthat will be passed to the listened listener.fromasyncioimportrun,sleepfromevents_managerimportEvent,emit,listenclassFooEvent(Event):passasyncdeffoo_listener(event:FooEvent,bar,baz):print(f"'foo_listener' invoked with{event},{bar}and{baz}")asyncdefmain():listen(FooEvent,foo_listener,False,'bar',baz='baz')print("Emitting event...")emit(FooEvent())# do the other stuff...awaitsleep(1)if__name__=='__main__':run(main())Output:Emitting event...
'foo_listener' invoked with <__main__.FooEvent object at 0x7fdbd0fa50d0>, bar and baz

Process finished with exit code 0Register a listener with@ondecoratorInstead of callinglistenmethod, you can also use the@ondecorator.fromasyncioimportrun,sleepfromevents_managerimportEvent,emit,onclassFooEvent(Event):pass@on(FooEvent)deffoo_listener(event:FooEvent):print(f"'foo_listener' invoked with{event}")asyncdefmain():print("Emitting event...")emit(FooEvent())# do the other stuff...awaitsleep(1)if__name__=='__main__':run(main())Output:Emitting event...
'foo_listener' invoked with <__main__.FooEvent object at 0x7fa0a9a47100>

Process finished with exit code 0Unregister a listenerCallunregistermethod passing the event type that you want to stop listening and the listener.fromasyncioimportrun,sleepfromevents_managerimportEvent,emit,listen,unregisterclassFooEvent(Event):passdeffoo_listener(event:FooEvent):print(f"'foo_listener' invoked with{event}")asyncdefmain():listen(FooEvent,foo_listener)print("Emitting first event...")emit(FooEvent())# let the event be processedawaitsleep(1)unregister(FooEvent,foo_listener)print("Emitting second event...")emit(FooEvent())# do the other stuff...awaitsleep(1)if__name__=='__main__':run(main())Output:Emitting first event...
'foo_listener' invoked with <__main__.FooEvent object at 0x7f92c79b9070>
Emitting second event...

Process finished with exit code 0DevelopmentRun Tests./testStyle Check./lintLicenseDistributed under the MIT License. SeeLICENSEfile for more information.