#๐Ÿ”’ best practice: supporting user-contributed code

16 messages ยท Page 1 of 1 (latest)

mystic sundial
#

I'm developing a generic computer vision application in Python. I use dxcam to grab a user's screen, use yolov8 in ultralytics to detect features, and then use opencv to decorate the features detected in the screenshot. I then display a click-through always-on-top opencv window, so it appears to the user in near-real-time. Think something like Snapchat filters.

I am implementing the decoration code as classes that have a consistent interface. For example, one might draw boxes around flowers, one might put a tiara on any heads, one might draw a black box over guns. The user will be presented with a GUI that shows all the available decorators, and then selects which to "turn on".

I want to make it as easy as possible for other people to contribute other decorators. What I am imagining so far is to have a package folder /decorators/. To create a new decorator, a user will simply drop a .py file into that folder - for example, corvusdec003.py. That py file MUST define a class named decorator, which must have functions description (shown to user in GUI), initialize, and decorate_image.

Is this the best solution? I would still need some way to programmatically enumerate all the modules in the package and load the decorator class from each of them. I'm sure there's a way to do that, but this seems strange enough that I want to ask if there's a better way (to do something that admittedly is a weird thing to do, automatically incorporating user-contributed code).

languid vigilBOT
#

@mystic sundial

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.

tacit laurel
#

If you're targetting iphone, I think that's against appstore policy

mystic sundial
#

I am not. Windows desktop. Not even pip, just distributing on GitHub.

tacit laurel
#

ah, ok then

#

If you're asking about plugins, having a gitignored folder for users to submit their code should work fine.

mystic sundial
#

Is this concept of treating that folder as a package, and using some kind of reflection tools to get a list of existing modules? Or maybe doing a directory listing? I'm not sure if there's a "normal" way to solve this problem.

tacit laurel
#

directory listing will work

mystic sundial
#

So let's suppose I know that the plugins directory contains corvus03.py and corvus04.py, and I know each defines a class called decorator.

If I were doing this by hand, I would probably do
import plugins
D1 = plugins.corvus03.decorator()
D2 = plugins.corvus04.decorator()

But since I don't know what will exist when coding, is there a good way to go from

known_modules = [ 'corvus03', 'corvus04' ]

To a list of instantiated decorator classes?

tacit laurel
#

You should use importlib

#

alternatively, only import the __init__.py

mystic sundial
#

Isn't import part of importlib? Or are you suggesting something other than import?

tacit laurel
#

importlib.import_module("custom." + pluginname).decorator

mystic sundial
#

AH! I see, thank you!

languid vigilBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, 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.