[Tutor] Calling a Method with a Reserved Name

Alex Ezell aezell at gmail.com
Wed Oct 24 23:39:06 CEST 2007


Oops, meant to send to the list. Sorry, Kent.

> > > >>> I am working on building a SOAP client. Unfortunately, one of the
> > > >>> methods the SOAP server provides is named "import." The SOAP server is
> > > >>> written in PHP.
> > > >>>
> > > >>> So, my problem is that Python really doesn't like me using the word
> > > >>> "import" to call the SOAP method. The call should look something like
> > > >>> this:
> > > >>>
> > > >>> self.call_response = self.soap.import(self.soap_auth, file_name,
> > > >>> import_groups, soap_flags)
> > > >>>
> > > >>> Is there any way to call this method despite it's name being a reserved word.
> > > >> You could try introspection:
> > > >>
> > > >> importFunc = getattr(self.soap, 'import')
> > > >> self.call_response = importFunc(self.soap_auth, file_name,
> > > >> import_groups, soap_flags)
> > > >
> > > > Thanks Kent. I tried it and it seem like importFunc is now something
> > > > like 'import.__str__'. I could maybe do some string operations to just
> > > > get import out of that, but is there something I could do with
> > > > getattr() for that reference to come back the way I need.
> > >
> > > Hmm, with a quick look at the code for SOAPpy (v 0.11.6) I don't see why
> > > the getattr() method would not work. Can you show the code you tried and
> > > why you think the result was a string?
> > >
> > > BTW pulling 'import' out of the string won't help; you need the import
> > > *function*.
> >
> > Got ya on the string bit. That was actually my fault. I think I am the
> > victim of my own poorly written exception handling method. Or at
> > least, I can't correctly read the errors that it tells me. :)
> >
> > The introspection bit you offered seems to work fine. The error is now
> > within the call to the SOAP server.
> >
> > Sorry for getting everyone confused. I'm off to ask my fellow
> > developer if the SOAP server really does what it says does since she
> > wrote it :)
> >
> > /alex

Heh, I am still having problems with this. This is the whole method:

def importer(self, file_name, import_groups, import_flags):
       soap_flags = self.dict_to_key_value(import_flags)
       try:
           # TODO figure out how to call this soap method with reserved name
           self.call_response = self.soap.import(self.soap_auth,
file_name, import_groups, soap_flags)
       except Exception, e:
           method_name = sys._getframe().f_code.co_name
           method_args = '(%s,%s,%s)'
%(file_name,str(import_groups),str(import_flags))
           self.handle_exception(method_name + method_args,e)
           raise
       return self.call_response

I tried to replace the import() call with these two lines:
importFunc = getattr(self.soap, 'import')
self.call_response = self.soap.importFunc(self.soap_auth, file_name,
import_groups, soap_flags)

and this was the error:

AttributeError: importFunc

module body       in sync_members.py at line 143
function main     in sync_members.py at line 138
function sforce_to_members        in sync_members.py at line 80
function do_import        in sync_members.py at line 70
function importer         in emma_ws.py at line 84
function __getattr__      in WSDL.py at line 96

Thanks again for working with me this far. I am certainly on the very
precipitous edge of my Python "knowledge."

/alex


More information about the Tutor mailing list