[Tutor] Tutor Digest, Vol 61, Issue 3

Lie Ryan lie.1296 at gmail.com
Mon Mar 2 09:12:49 CET 2009


Daniele wrote:
>> From: W W <srilyk at gmail.com>
>> Subject: Re: [Tutor] modular program
> 
>>> Where can I find some "best practices" for writing modular programs?
>>> I thought about a txt file containing function calls that my program will
>>> parse and execute in order, or is it better just to execute every .py file
>>> in a certain "module" folder (I don't like this as modules could need to be
>>> executed in different moments)?
> 
>> You can pretty much take a look at any of the modules in your python
>> library directory. In the case of my linux box, that's
>> /usr/lib/python2.5/
> 
> I'm sorry, I've realized I didn't explain my needs at all.
> I was a little influenced by drupal's definition of modules, which is
> completely different from python's.
> With module here I meant plug-in or extension: a piece of code written
> by someone else that can be easily (and automaticallly) integrated
> into my program.
> My program must provide the posibility to be extended without editing
> its code, just like mozilla's add-ons.

It heavily depends on the design of the main program. The simplest way
to have a plug-in system is probably having user writes python modules
which your program will import and call a certain agreed function.

for example

mainprogram.py:
um_name = raw_input('Enter usermodule's filename: ')
um = __import__(um_name)
um.run()

usermodule.py:

def run():
    # do initializations required to install the module



More information about the Tutor mailing list