#๐Ÿ”’ Event listen plugin architecture designing

7 messages ยท Page 1 of 1 (latest)

dapper ridge
#

I'm writing an app in python and would like to offer user a plugin framework, which allow user to write plugins (as seperate .py files (module)) listen to events.
Basicially this:

# a seperate module
@listen("eventname")
async def listener():
  ...

Now however I'm confused which way I should use to collect listeners.

listeners = PluginListeners()
@listeners.add("eventname")
async def listener():
  ...

This allows user write less code, but user'll have trouble writing listener in class.

async def listener():
  ...

@inject
async def configure_plugin(plugin_manager = Inject[PluginManager]):
  plugin_manager.register("eventname", listener)

This way user can write arbitrary listeners, but my app'll have trouble record the plugin which registered the listener.

What should I choose now?

hushed robinBOT
#

@dapper ridge

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

devout frost
#

!decorator

hushed robinBOT
#
Decorators

A decorator is a function that modifies another function.

Consider the following example of a timer decorator:

>>> import time
>>> def timer(f):
...     def inner(*args, **kwargs):
...         start = time.time()
...         result = f(*args, **kwargs)
...         print('Time elapsed:', time.time() - start)
...         return result
...     return inner
...
>>> @timer
... def slow(delay=1):
...     time.sleep(delay)
...     return 'Finished!'
...
>>> print(slow())
Time elapsed: 1.0011568069458008
Finished!
>>> print(slow(3))
Time elapsed: 3.000307321548462
Finished!

More information:

devout frost
#

@dapper ridge

hushed robinBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.