diff --git a/tcxparser.py b/tcxparser.py index c7e5d09..051252f 100644 --- a/tcxparser.py +++ b/tcxparser.py @@ -40,7 +40,10 @@ def completed_at(self): @property def distance(self): - return self.activity.Lap[-1].Track.Trackpoint[-2].DistanceMeters.pyval + distance_values = self.root.findall('.//ns:DistanceMeters', namespaces={'ns': namespace}) + if distance_values: + return distance_values[-1] + return 0 @property def distance_units(self): diff --git a/test_tcxparser.py b/test_tcxparser.py index 0d354df..646d14f 100644 --- a/test_tcxparser.py +++ b/test_tcxparser.py @@ -1,4 +1,6 @@ +from StringIO import StringIO import unittest + from tcxparser import TCXParser @@ -71,5 +73,30 @@ def test_ascent_is_correct(self): def test_descent_is_correct(self): self.assertAlmostEqual(self.tcx.descent, 166.307128903) + +class BugTest(unittest.TestCase): + + def test_single_trackpoint_in_track_is_ok(self): + "https://github.com/vkurup/python-tcxparser/issues/9" + xml = """ + + + + + + + 5 + + + + + + + """ + tcx_file = StringIO(xml) + tcx = TCXParser(tcx_file) + self.assertEqual(tcx.distance, 5) + + if __name__ == '__main__': unittest.main()