0

I'm using the python-google-places script but I'm facing a problem when integrating the code into a function called with two parameters: "MyLocation" and "MyType".

"MyLocation" works well but "MyType" doesn't work.

Code that works (without a variable for "MyType")

# Configuration de l'encodage (a mettre dans tous les scripts)
# encoding: utf-8

####################################
### CLASS
#################################### 
def ExtractGoogleHotspots(Location,MyType):
        from googleplaces import GooglePlaces, types, lang
        YOUR_API_KEY = 'XXXXXXX'
        google_places = GooglePlaces(YOUR_API_KEY)
        # You may prefer to use the text_search API, instead.
        query_result = google_places.nearby_search(
            location=Location, #Location could be a name or coordinates in english format : 4.04827,9.70428
            keyword='',
            radius=1000,#Radius in meters (max = 50 000)
            types=[types.TYPE_PHARMACY] #Specify the Type
            ) 

        for place in query_result.places:
        # Returned places from a query are place summaries.
            Name = place.name
            Name = Name.encode('utf-8')#Encodage en UTF-8 obligatoire pour éviter les erreurs : "UnicodeEncodeError: 'ascii' codec can't encode character..."
            Latitude = place.geo_location['lat']
            Longitude = place.geo_location['lng']
            # The following method has to make a further API call.
            place.get_details()
            Details = place.details
            Address = place.formatted_address
            Address = Address.encode('utf-8') 
            Phone_International = place.international_phone_number
            Website = place.website

        Result = str(MyType) + ";" + Name + ";" + Address + ";" + str(Phone_International) + ";" + str(Latitude) + ";" + str(Longitude)
+ ";" + str(Website)
        print Result

####################################
### Script
####################################

Location = "4.04827,9.70428"
MyType = "DOES_NOT_WORK"
ExtractGoogleHotspots(Location, MyType)

Code that doesn't work:

    # Configuration de l'encodage (a mettre dans tous les scripts)
    # encoding: utf-8

    ####################################
    ### CLASS
    #################################### 

def ExtractGoogleHotspots(Location,Type):
            from googleplaces import GooglePlaces, types, lang
            YOUR_API_KEY = 'XXXXXXX'
            google_places = GooglePlaces(YOUR_API_KEY)
            MyType = "[types.TYPE_"+Type+"]"
            # You may prefer to use the text_search API, instead.
            query_result = google_places.nearby_search(
                location=Location, #Location could be a name or coordinates in english format : 4.04827,9.70428
                keyword='',
                radius=1000,#Radius in meters (max = 50 000)
                types=MyType #Specify the Type
                ) 

            for place in query_result.places:
            # Returned places from a query are place summaries.
                Name = place.name
                Name = Name.encode('utf-8')#Encodage en UTF-8 obligatoire pour éviter les erreurs : "UnicodeEncodeError: 'ascii' codec can't encode character..."
                Latitude = place.geo_location['lat']
                Longitude = place.geo_location['lng']
                # The following method has to make a further API call.
                place.get_details()
                Details = place.details
                Address = place.formatted_address
                Address = Address.encode('utf-8') 
                Phone_International = place.international_phone_number
                Website = place.website

                Result = str(MyType) + ";" + Name + ";" + Address + ";" + str(Phone_International) + ";" + str(Latitude) + ";" + str(Longitude)
        + ";" + str(Website)
                print Result

        ####################################
        ### Script
        ####################################

        Location = "4.04827,9.70428"
        MyType = "PHARMACY"
        ExtractGoogleHotspots(Location, MyType)

How to resolve the problem to have a variable to define the Types part?

1
  • 1
    If your edit answers the question and the code works, then you should post below instead of updating the question. Now it is not clear what you are asking. Commented Aug 10, 2016 at 23:00

1 Answer 1

1

To concatenate the type name with TYPE_ in take it from types you can use getattr

MyTypes = [ getattr(types, 'TYPE_' + Type.upper() ]
Sign up to request clarification or add additional context in comments.

16 Comments

Thanks Marco. I tried but I have the following Error: UnboundLocalError: local variable 'types' referenced before assignment
@neonepthys are you importing the google library as in your first example?
I'm a newbie in coding and I don't get your point. I modified the code with the one you have posted and it did not work
Can I see the full code of your first example (the one you said that works)? (do not include your API Key!!!)
See below the full code.Your might have to install the Python-Google-script: pip install python-google-places
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.