Justin Strawn

3D Designer, Photographer, Traveler

Jennifer’s List Python Challenge

,

Overview

There was this great list from Jennifer on the best restaurants to eat at in London. It was just a text list of restaurant names. In practice, we would be out somewhere in Covent Garden and say to ourselves “What’s nearby from Jennifer’s list”? Only to have to search a few one-by-one. I started to add some of them to my map favorites but that was taking so long, I knew there must be another way.

“I bet I can write a 10 line python script to import Jennifer’s list into google maps automatically.”

The Code

https://gist.github.com/justinstrawn/97792cad1811bca1db2e1419f928c0ac

import os, sys, time, math, simplekml, googlemaps
gmaps = googlemaps.Client(key='X')
kml=simplekml.Kml()
for row in open(sys.argv[1],'r').read().strip().split("\n"):
	geocode_results = gmaps.places(row, location=sys.argv[2], radius=1000)["results"]
	for result in geocode_results:
		place_result = gmaps.place(result["place_id"])["result"]
		visc = ([x["long_name"] for x in place_result["address_components"] if "route" in x["types"]] or (place_result.get('vicinity')) or [""])[0]
		p = kml.newpoint(name=result["name"], description=result["name"] + " " + visc+ " \n" + "".join([u"\xA3" for x in range(0, place_result.get("price_level") or 0)]) + " (" + str(place_result.get("rating")) + ") " + str(place_result.get("website")), coords=[result["geometry"]["location"].values()[::-1] ])
kml.save('jenniferslist.kml')

The Result

Most usefully, it can be imported into your google maps favorites, in a separate list that can be toggled on-and-off.

It can also be embedded, linked to, or opened in a variety of programs such as Google Earth.

Conclusion

It was a fun 30-minute exercise that keeps my knowledge fresh, and it created something very useful!

There are several ways it could be argued I didn’t win the bet, and are good opportunities to further develop the code later.

  1. I still had to manually upload the generated file to Google MyMaps
  2. “Lines” should be defined as a maximum of 80 characters, which I exceed at times
  3. I am using libraries such as google maps API and simpleKML which are non-standard python libraries.

Further Reading

If you’re interested in learning python as a beginner, I strongly recommend you play a game called 7 Billion Humans (iOS/Android/Mac/Linux/PC) by Tomorrow Corporation. It is incredibly useful in training the mind to think in the same way I think when making these types of scripts.

If you’re interested in advancing your current python skills to new heights, I highly recommend the Python Challenge. It’s hard, and it gets harder, and it’s such a wonderful and challenging way to teach yourself. You can even ask for help safely on the forums’ no-solutions-allowed section.