Search code examples
pythonsoappy

what is the type of the variable


I am a very newbie in Python I have the following code:

from SOAPpy import WSDL

fichier_wsdl = 'http://geocoder.us/dist/eg/clients/GeoCoder.wsdl'
wsdl = WSDL.Proxy(fichier_wsdl)
callInfo = wsdl.methods['geocode']
ss = wsdl.geocode('1600 Pennsylvania Ave, Washington, DC.')
print(ss)

The result is:

IMPORT:  http://schemas.xmlsoap.org/soap/encoding/
no schemaLocation attribute in import
<<class 'SOAPpy.Types.typedArrayType'> results at 21824752>: [<SOAPpy.Types.structType item at 21818984>: {'city': 'Washington', 'prefix': '', 'suffix': 'NW', 'zip': 20502, 'number': 1600, 'long': -77.037684, 'state': 'DC', 'street': 'Pennsylvania', 'lat': 38.898748, 'type': 'Ave'}]

and I try to understand what type has my ss variable (the print(type(ss)) get SOAPpy.Types.typedArrayType which is not very clear for me)? And how to have a simple variable, for the city or the street?


Solution

  • Let's reformat that output for readability:

    <<class 'SOAPpy.Types.typedArrayType'> results at 21824752>:
    
    [<SOAPpy.Types.structType item at 21818984>:
         {'city': 'Washington', 'prefix': '', 'suffix': 'NW', 'zip': 20502, 'number': 1600,
          'long': -77.037684, 'state': 'DC', 'street': 'Pennsylvania', 'lat': 38.898748,
          'type': 'Ave'
         }
     ]
    

    It's telling you what type your variable is: SOAPpy.Types.typedArrayType ... try reading the SOAPpy docs to understand that (I'm a SOAPpy non-user, not even a newbie).

    What you really want to know is how to use that result. Looks to me like if you do answer_dict = ss[0], you can access the fields like this:

    print answer_dict['city'] should produce Washington etc

    so you can do

    city = answer_dict['city']
    street = answer_dict['street']
    # et cetera
    

    Note that ss with the fancy type looks like it acts like a list ... if your query has multiple answers (check len(ss)), you will need to iterate over the list:

    for answer_dict in ss:
        process_each_answer(answer_dict) # substitute your code here