I'm following the YouTube video Learn Python through Data Hacking and am expanding on the sort of "legacy" inline way by adding classes to learn all the ins and outs of Python OOP. Basically the guy in the video has you grab the Chicago bus line's route, parse it, and find which bus his friend left his suitcase on by comparing lat, long, time, distace, etc.
The XML file I'm working with has a bunch of values like so:
<bus>
<id>1867</id>
<rt>22</rt>
<d>North Bound</d>
<dd>Northbound</dd>
<dn>N</dn>
<lat>41.89167051315307</lat>
<lon>-87.6297836303711</lon>
<pid>5421</pid>
<pd>Northbound</pd>
<run>P258</run>
<fs>Howard</fs>
<op>30090</op>
<dip>8858</dip>
<bid>7323012</bid>
<wid1>0P</wid1>
<wid2>258</wid2>
</bus>
From there, I need to find which bus' latitude are north from his position (defined in the class, moot for this example). From those nodes, I'm creating a Bus
object like so:
class Bus:
# The original XML node
__xml = None
# Our dictionary for properties to shadow a get() function on this object
__tree = {}
def __init__(self, busxml):
self.__xml = busxml
for e in busxml:
self.__tree[e.tag] = busxml.findtext(e.tag)
def gettree(self):
return self.__tree
# Tries to return prop, or returns None
def get(self, prop):
try:
return self.__tree[prop]
except KeyError:
return -1
def getall(self):
return self.__tree
From the "main" file, I'm looping through the values and appending matches based on the lat
node's text value:
# __getRouteData() is the url open and write function, works fine.
# parser is the xml.etree.ElementTree parse class
if self.__getRouteData() == True:
xmlParser = parser(self.__xmlFile)
busses = xmlParser.getnodes()
matches = []
# loop over all
for busTree in busses:
bus = Bus(busTree)
if float(bus.get('lat')) > self.__lat:
matches.append(bus)
print 'appending', bus.get('id')
for bus in matches:
print bus.get('id')
The snag I'm hitting is in the second for
loop above. In the first loop, the output is telling me things are working well. The second one outputs the same value twice. It reminds me of the behavior with Javascript for()
loops where, without a closure, only the last value is acted upon. My output from the console is as follows:
appending 1784
appending 4057
4057
4057
See... it's telling me it's appending unique busses to my matches
list, but when I iterate over the matches
list, its only giving me one bus.
Another snippet that tells me something's funky with the second loop:
print 'Matches', matches
for bus in matches:
print bus.get('id')
# Matches [<busutils.Bus instance at 0xb6f92b8c>, <busutils.Bus instance at 0xb6f92d0c>]
# 4057
# 4057
The output of the list is showing me different hashes (...right?) of the objects in the list, hence saying they're two different objects, and thus have different data, but the loop isn't playing nicely.
Obviously I'm just getting into python, but have experience in Java, Javascript, PHP, etc, so I'm not sure what I'm missing in these simple loops.
Thanks!