Created
September 24, 2021 09:04
-
-
Save bencleary/43a6e5b1869c00f1f2e87928a61ddfae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# mylibrary/library.py | |
class Library: | |
def subscribe(self, topic): | |
# checks to see if already subscribed | |
if topic in self.topics: | |
raise AssertionError("Topic is already subscribed too") | |
def wrapper(handler): | |
# checks to see if this is part of a class | |
if not self._is_method(handler): | |
self.client.subscribe(topic) | |
print(f"--> subscribed to {topic}") | |
self.topics[topic] = handler | |
# appends a couple of poperties to the function or method | |
handler.subscription = True | |
handler.topic = topic | |
return handler | |
return wrapper | |
@staticmethod | |
def subscribe2(topic): | |
# Removes check so logic must be handled in regsiter | |
def wrapper(handler): | |
handler.subscription = True | |
handler.topic = topic | |
return handler | |
return wrapper | |
# status/controller.py | |
class StatusController: | |
""" | |
How can i access the app instance here? I have a series of methods which register the topic and the apprioate handler. It works fine if i leave the controller in main.py. | |
I feel like i'm most likely barking up the wrong tree. It works fine if i convert it to a Static method, but then my app.register method needs to handle the verification, etc. | |
I have tried inheritence, adding a library object to the class but to no avail. | |
""" | |
@library.subscribe("some.topic") | |
def some_handler(self, payload): | |
print(payload) | |
@Library.subscribe("some.topic") | |
def some_handler1(self, payload): | |
print(payload) | |
# main.py | |
""" | |
from mylibrary import Library | |
from mypackage import StatusController | |
""" | |
app = Library() | |
app.register(StatusController()) | |
app.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment