How to decorate a function and get a class in python
Solution 1:
Your decorator should return a function that takes the decorated function and creates a class whose run
method just calls the decorated function.
def core_factory(topics):
def wrapper(f):
class _(BaseCore):
@classmethod
def run(cls, topic, payload):
return f(topic, payload)
_.topics = topics
_.__name__ = f.__name__
return _
return wrapper
@core_factory(["glados/tcs/licht/set"])
def Licht(topic, payload):
mqtt_publish("tcs/bus/cmd", "1200")