8000 GitHub - treborg/xpubsub: A basic pubsub module for communications within an app.
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

treborg/xpubsub

Repository files navigation

A basic pubsub module for communications within an app.

This project is an excercise to help me develop skills relating to:

You would probably be better off using PyPubSub

Interface

from xpubsub import PubSub
pub = PubSub()

HashOrList = Union[Hashable, list[Hashable]]

pub.add(topic_list: HashOrList, callback: Callable):
pub.remove(topic_list: HashOrList, callback: Callable):
pub.send(topic_list: HashOrList, message: Any):

Example: example.py

from xpubsub import PubSub

pub = PubSub()


def hello(topic, message):
    print(topic, message)


def goodbye(topic, msg):
    print("😭", topic, msg)


pub.add("hi", hello)
pub.add(["SHTF!", "go away"], goodbye)

pub.send("hi", "👋😍")
pub.send("SHTF!", "Head For The Hills!")
pub.send("go away", "its over")

pub.remove("go away", goodbye)
pub.send("go away", "nothing happens")  # this does nothing!

Output

hi 👋😍
😭 SHTF! Head For The Hills!
😭 go away its over

About

A basic pubsub module for communications within an app.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

0