1
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

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, got it now. Things I don't understand yet : what is "abs" ? and why when we create distance() we assign parameters lat1 and lat2 ? Wouldn't it be the same writing lat and daves_latitude instead ?
Yes, you are right about second parameter - in this case it's always same value. For such a basic function as "abs" I would recommend using built-in help function before asking any questions - just type «help('abs')» in python console
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.