Proper way of handling "plug-in" methods

Franck PEREZ franck.perez at gmail.com
Mon Jan 8 06:49:15 EST 2007


All,

My application deals with strings formatting. I have built-in methods
but I also expect the user to add its methods in its own .py files
(some sort of plugin methods, all user methods should be exposed in my
application).

Here is the structure I have thought of :

formatting.py
_formattingDict = {} #a dict composed of all available methods, both
builtin and user
def expose(...) : #adds a method to the dict

builtinformatting.py
import myapp.formatting.expose
@expose
def builtinMethod(inputStr) : return someOutput

/home/user/.myapp/customformatting.py
import myapp.formatting.expose
@expose
def customMethod(inputStr) : return someOutput

model.py
#References all the methods, both builtin and custom
execfile("builtinformatting.py")
execfile("/home/user/.myapp/customformatting.py")

Expected result after the execfile : formatting._formattingDict
contains the 2 methods builtinMethod and customMethod

Is this a proper way of structuring my application ? Do you see
anything better ?

Moreover, I dislike execfile("builtinformatting.py") since classic
import would do the job. However I first wanted a united method for
both builtin and custom scripts since it is the same logic for me.

Thanks very much in advance for your advice.
Franck



More information about the Python-list mailing list