patternpythonMinor
Find my colleagues
Viewed 0 times
colleaguesfindstackoverflow
Problem
I recently finished the Using Databases with Python course. To figure out whether I really understood what was being taught, I build an application which does roughly the same but uses a different formatting.
The idea is to have a list with addresses, look up the corresponding coordinates using the Google Maps API, store name/address/coordinates in a SQLite database, retrieve the data from the database and show the retrieved locations on a map.
To show my colleagues what keeps me busy during the weekends, I inserted their addresses, plotted a map and showed them the might of Python. Of-course, all data here is example data and not the actual dataset. The real set is less than 200 addresses though, so the difference shouldn't matter much. All code is real.
locations.data
locations.js
geoload
The idea is to have a list with addresses, look up the corresponding coordinates using the Google Maps API, store name/address/coordinates in a SQLite database, retrieve the data from the database and show the retrieved locations on a map.
To show my colleagues what keeps me busy during the weekends, I inserted their addresses, plotted a map and showed them the might of Python. Of-course, all data here is example data and not the actual dataset. The real set is less than 200 addresses though, so the difference shouldn't matter much. All code is real.
geoload.py queries the API for all addresses provided in locations.data. The results will be stored in geodata.sqlite. All data previously encountered will be skipped instead of retrieved again to allow multi-stage loading (which is helpful in cases of rate limiting). geodump.py will retrieve the data from geodata.sqlite and put data ready for display in JSON format in locations.js. locations.html will display a map with markers placed on the locations provided. Hovering over the markers will tell who's stationed there.locations.data
Headquarters, Havenmeesterweg 1, Haarlemmermeer
Alice, Spoorstraat 2, Leeuwarden
Bob, Stadskanaal
Charlie, Maastricht
Devon, Woerden
Eddy, P.J. Jongstraat, Lutjebroek
Freddy, Roosendaal
Giles, Almere
Harry, Spoorstraat 4, Winterswijk
Igor, Middelburg
Janine, Terschelling
locations.js
locations = [
['Headquarters',52.3046539,4.7588565],
['Alice',53.1975889,5.8055371],
['Bob',52.9919853,6.9462217],
['Charlie',50.8513682,5.6909725],
['Devon',52.0798287,4.8627239],
['Eddy',52.6979589,5.2007523],
['Freddy',51.535849,4.4653213],
['Giles',52.3507849,5.2647016],
['Harry',51.9698835,6.7204984],
['Igor',51.4987962,3.610998],
['Janine',53.3978747,5.3466786]
];
geoload
Solution
In
In
It just asks for you to do:
I used
Regarding that, a value returned from a db is usually already a string, so there should be no need to cast
For your
geoload.py use with..as to ensure it is closed, even if any code in-between raises an exception:with open("locations.data") as filehandle:
...In
geodump.py, this is a code-smell:for row in cur:
'''
row[0]: target
row[1]: address
row[2]: lat & long
'''It just asks for you to do:
for target, _, data in cur:
...I used
_ because you never use the address here. This still makes sense, even though you do some more processing on it afterwards. Regarding that, a value returned from a db is usually already a string, so there should be no need to cast
target = str(...).For your
output, I would use str.format, which makes it a lot easier to read:output = "['{}',{},{}]".format(target, lat, lng)Code Snippets
with open("locations.data") as filehandle:
...for row in cur:
'''
row[0]: target
row[1]: address
row[2]: lat & long
'''for target, _, data in cur:
...output = "['{}',{},{}]".format(target, lat, lng)Context
StackExchange Code Review Q#148296, answer score: 7
Revisions (0)
No revisions yet.