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

Keerati Inochanon unselfishly at gmail.com
Sat Apr 2 21:39:55 EST 2005


Hi,

Thank you very much for the fast response. I was trying to get a
reference to the object based on name, as I found in the Dive Into
Python tutorial:
http://www.diveintopython.org/file_handling/more_on_modules.html.

It seems that using globals() will also a give similar result. I think
that will work too. Thank you very much! I appreciate it.

Best regards,
Keerati Inochanon

On Apr 2, 2005 8:30 PM, Graham Dumpleton <grahamd at dscpl.com.au> wrote:
> 
> 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