Skip to main content

a event bus

Project description

fancy-event

Usage

import fancy.eventbus as fe


class MyEvent:
    value: int

    def __init__(self, value):
        self.value = value


class MyEvent2:
    pass


class Target:
    def __init__(self):
        fe.register(self)

    @fe.SubscribeEvent()
    def on_start(self, event: fe.StartEvent):
        print("start!")
        fe.post(MyEvent(1))

    @fe.SubscribeEvent()
    def my_event(self, event: MyEvent):
        print("my_event", event.value)
        fe.post(MyEvent2())
        fe.post(self)

    @fe.SubscribeEvent()
    def my_event2(self, event: MyEvent2):
        print("my_event2")
        raise RuntimeError("an error")

    @fe.SubscribeEvent(priority=fe.EventPriority.HIGHEST)  # execute before my_event2
    def self_as_event(self, event: "Target"):  # forwarding ref
        print("self_as_event", event is self)

    @fe.SubscribeEvent()
    def error_handler(self, e: RuntimeError):
        print("handle by runtime error", e)

    @fe.SubscribeEvent(priority=fe.EventPriority.LOW)  # method name should different
    def error_handler2(self, e: Exception):
        print("handle by exception", e)
        return False  # returned false will cancel the event

    @fe.SubscribeEvent(priority=fe.EventPriority.LOWEST)
    def error_handler3(self, e: BaseException):
        print("this should be canceled.", e)


loop = fe.EventLoop()
# registers
Target()

loop.start()
start!
my_event 1
self_as_event True
my_event2
handle by runtime error an error
handle by exception an error

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

fancy-eventbus-0.1.0a0.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fancy_eventbus-0.1.0a0-py3-none-any.whl (14.1 kB view details)

Uploaded Python 3

File details

Details for the file fancy-eventbus-0.1.0a0.tar.gz.

File metadata

  • Download URL: fancy-eventbus-0.1.0a0.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0.post20200814 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.6.10

File hashes

Hashes for fancy-eventbus-0.1.0a0.tar.gz
Algorithm Hash digest
SHA256 625fd21e6fb21b45f290d5142c211c75f3f494eaa1bc5e7f9da4142a8b49df06
MD5 ae28ecb45f72b8e6cd5dcbb41fa8e010
BLAKE2b-256 0b9fba842423082afd5211cedeccc6badb154055e718a3a80ec5267265b7bcde

See more details on using hashes here.

File details

Details for the file fancy_eventbus-0.1.0a0-py3-none-any.whl.

File metadata

  • Download URL: fancy_eventbus-0.1.0a0-py3-none-any.whl
  • Upload date:
  • Size: 14.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0.post20200814 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.6.10

File hashes

Hashes for fancy_eventbus-0.1.0a0-py3-none-any.whl
Algorithm Hash digest
SHA256 7a9b9903c0d9b4b11e4ae35be0661a4329a5d55046346b6eac21b71d81aaadb2
MD5 16c2f89009959ed136b1732ef21f23c6
BLAKE2b-256 16b0d8941c6b9c69df0b9c4a248479f107b547ae17840cf1b5b5bd79f48a9d9b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page