R: [mod_python] problem in file download

Manera, Villiam vmanera at manord.com
Fri Jun 7 14:32:13 EST 2002


Wow!!!!! publisher.py patched with this function work fine!!!!

class File:
    """ Like a file, but also has headers and filename
    """
    def __init__(self, field):
        # steal all the file-like methods
        #for m in field.file.__methods__:
        #for m in dir(field.file):    #dont' work
        for m in self.sim_methods(field.file):
            self.__dict__[m] = getattr(field.file, m)

        self.headers = field.headers
        self.filename = field.filename
    
    def sim_methods(self,obj):
        from types import BuiltinMethodType   #parte aggiunta
        return filter(lambda s, t=obj:
           type(getattr(t, s)) == BuiltinMethodType, dir(obj))


thanks all
Villiam
-----Messaggio originale-----
Da: John D. Garberson [mailto:john.garberson at nexos.com]
Inviato: venerdì 7 giugno 2002 10.54
A: vmanera at manord.com
Cc: Mod_python at modpython.org
Oggetto: Re: [mod_python] problem in file download


Hi,

You wrote a few days back:

> 
> Hello,
> 
> I'm trying to download a file:
> 
....
....
....
a = getattr(file, name)
> 
> AttributeError: 'file' object has no attribute '__methods__'
> 
> 
> does anyone have any idea to solve this?
> 
> Villiam



Having to downgrade seems a bit extreme [;-)

I don't know much about the publisher.py module, never having used it.
But if you do this import:

------------------------------------------------------------------------

from types import BuiltinMethodType

------------------------------------------------------------------------

then this function:

------------------------------------------------------------------------

def sim_methods(obj):
    return filter(lambda s, t=obj:
           type(getattr(t, s)) == BuiltinMethodType,
                                             dir(obj))

--------------------------------------------------------------------------

works under both 2.0 and 2.2.1 (I don't have a 2.1 any more) and delivers
the list of methods that it seems you want, looking at the publisher.py
source.  If you're not a fan of the functional programming approach, then
a list comprehension:

-----------------------------------------------------------------------

def sim_methods(obj):
    return [m for m in dir(obj)
                   if type(getattr(obj, m)) == BuiltinMethodType]

-----------------------------------------------------------------------

works just as well.

You could just replace the 'for m in field.file.__methods__:'

with

'for m in sim_methods(field.file):'

Hope this helps.

John


     John Garberson                             nexos ag 
     john.garberson at nexos.com                   Frobenstrasse 66 
                                                CH-4053 Basel, Switzerland
                                                Tel. 41 61 283 55 03
                                                Fax  41 61 283 55 01

_______________________________________________
Mod_python mailing list
Mod_python at modpython.org
http://www.modpython.org/mailman/listinfo/mod_python



More information about the Mod_python mailing list