For a program of this size, you really don't need to put your command functions in separate files, but I think this is a good organization. And good practice when you write a program with thousands of lines of code. :)
One way to do this is to create a SkypeBot base class without any command methods, and then import the command methods from your plugins directory and add them to the class. It is easy to add new attributes to an existing class, and it does not matter if the new attributes are properties or methods, the syntax for adding them is identical. (With a bit of work, you can even add new attributes to an instance, so you can have several instances, each of which has its own set of commands. But I think that is not necessary here, since a program using the SkypeBot class will usually only create one instance).
So, we can break your question into two parts:
- How to add methods to an existing class.
- How to import these methods from other source files.
As I said, 1) easy. 2) also quite simple, but I have never done this before, so I had to do a little research and testing, and I canโt promise that what I did is the best practice, but it works. :)
I don't know much about Skype, and I donโt have this Skype4Py module, and, as you said, the code above is not a complete program, so I wrote a fairly simple code to illustrate the process of adding plug-in methods from separate files to an existing class.
The name of the main program is "plugin_demo.py". To keep something neat, he lives in his own "plugintest /" directory, which you must create somewhere in your Python path (for example, where you usually keep your Python programs). This path must be specified in the PYTHONPATH environment variable.
"plugintest /" has the following structure:
plugintest/ __init__.py plugin_demo.py plugins/ __init__.py add.py multiply.py
The __init__.py files are used by the Python import machine to report that the directory contains a Python package, see 6.4. Packages in Python docs for more details.
Here are the contents of these files. First, the files that go into "plugintest /":
__ __ INIT. RU
__all__ = ['plugin_demo', 'plugins'] from plugintest import *
plugin_demo.py
#! /usr/bin/env python
And now the contents of the "plugintest / plugins /" directory:
__ __ INIT. RU
__all__ = ['add', 'multiply'] from plugintest.plugins import *
add.py
#A method for the Test class of plugin_demo.py def add(self, m): self.data = [m + i for i in self.data]
multiply.py
#A method for the Test class of plugin_demo.py def multiply(self, m): self.data = [m * i for i in self.data]
If you cd to a directory containing the "plugintest /" folder, you can start it using
python plugintest/plugin_demo.py
and if you cd on "plugintest /"
python plugin_demo.py
Also, in the interpreter (or other Python program) you should be able to do
import plugintest
and then run main() function "plugin_demo.py" with
plugintest.plugin_demo.main()
Other common options are from ... import ... etc. should also work as expected.
The function in plugin_demo.py that does the magic of adding imported methods to the Test class is add_plugins() . When it starts, it prints the name of the method, its module and its function. This may be useful during development, but you are likely to comment on some of these print statements after the program runs correctly.
I hope this helps, and if you have any questions, please feel free to ask.