diff --git a/README.md b/README.md index 4df517f..139ff1f 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ Data extracted: - calories burned during workout (as estimated by device) - average, max and min heart rate during workout - average pace during workout + - average altitude during workout ## Installation diff --git a/tcxparser.py b/tcxparser.py index 84cb0d2..ee15323 100644 --- a/tcxparser.py +++ b/tcxparser.py @@ -15,6 +15,9 @@ def __init__(self, tcx_file): def hr_values(self): return [int(x.text) for x in self.root.xpath('//ns:HeartRateBpm/ns:Value', namespaces={'ns': namespace})] + def altitude_points(self): + return [float(x.text) for x in self.root.xpath('//ns:AltitudeMeters', namespaces={'ns': namespace})] + @property def latitude(self): return self.activity.Lap.Track.Trackpoint.Position.LatitudeDegrees.pyval @@ -69,4 +72,9 @@ def pace(self): """Average pace (mm:ss/km for the workout""" secs_per_km = self.duration/(self.distance/1000) return time.strftime('%M:%S', time.gmtime(secs_per_km)) - \ No newline at end of file + + @property + def altitude_avg(self): + """Average altitude for the workout""" + altitude_data = self.altitude_points() + return sum(altitude_data)/len(altitude_data) \ No newline at end of file diff --git a/test_tcxparser.py b/test_tcxparser.py index 913fe9f..987a0d3 100644 --- a/test_tcxparser.py +++ b/test_tcxparser.py @@ -43,6 +43,9 @@ def test_hr_avg(self): def test_pace(self): self.assertEquals(self.tcx.pace, '07:05') + + def test_altitude_avg_is_correct(self): + self.assertAlmostEqual(self.tcx.altitude_avg, 172.020056184) if __name__ == '__main__': unittest.main()