Reputation: 71
import urllib
from xml.etree.ElementTree import parse
candidates = ['4198', '4168']
daves_latitude = 41.98062
def distance(lat1, lat2):
'Return distance in miles between two lats'
return 69*abs(lat1 - lat2)
def monitor():
u = urllib.urlopen('http://ctabustracker.com/bustime/map/getBusesForRoute.jsp?route=22')
doc = parse(u)
for bus in doc.findall('bus'):
busid = bus.findtext('id')
if busid in candidates:
lat = float(bus.findtext('lat'))
dis = distance(lat, daves_latitude)
print busid, dis, 'miles'
print '-'*10
import time
while True:
monitor()
time.sleep(60)
I did this exercise based on a real life problem. Dave forgets his case in the bus, and he wants to find out wich one of the canditate buses is carrying the lost case. I understand the code, but I can't find out the relationship between the first function and the second one, as
def distance(lat1, lat2):
'Return distance in miles between two lats'
return 69*abs(lat1 - lat2)
I understand what monitor() does but not the relationship between distance() and monitor() and how they interact to show the results, can you please enlighten me ? I am a n00b.
Thanks
Upvotes: 1
Views: 126
Reputation: 158
abs() is the absolute value of a number.
^ little link there for python 3 works the same for python 2.
if your looking for the meaning of absolute value <- that link there shows you a video.
Upvotes: 0
Reputation: 6627
Each degree of latitude is approximately 69 miles (111 kilometers) apart. So, lat1
is a latitude of bus and the second lat2
is a Daves latitude. By requesting this url in monitor
you got latitude of bus, and then, using distance
function, you got the distance in miles.
Upvotes: 1