[mod_python] KeyError Exception when doing sys.modules[class.__module__] (vampire?)

Graham Dumpleton grahamd at dscpl.com.au
Sat Apr 2 20:30:36 EST 2005


On 03/04/2005, at 11:10 AM, Keerati Inochanon wrote:

> def processUpload(filename, directory):
>   "Process the user-uploaded files"
>
>   def getFileHandler(filename, 
> module=sys.modules[FileHandler.__module__]):
>     "Return the file handler class according to the file extension"
>
>     subclass = "%sFileHandler" % 
> os.path.splitext(filename)[1].upper()[1:]
>     return hasattr(module, subclass) and getattr(module, subclass) or
> FileHandler
>
>   getFileHandler(filename)(filename, directory)

How about using "globals()" instead of trying to get access to the 
module.

def processUpload(filename, directory):
   "Process the user-uploaded files"

   def getFileHandler(filename):
     "Return the file handler class according to the file extension"

     subclass = "%sFileHandler" % 
os.path.splitext(filename)[1].upper()[1:]
     return globals().get(subclass,FileHandler)

   getFileHandler(filename)(filename, directory)

Think I worked out what you were trying to do.



More information about the Mod_python mailing list