def fill_distance(self, config): """Fills distance with data in input dictionary Fills distance with the configuration dictionary. Args: config(dict): Dictionary with distance information Notes: Distance is a compulsory attribute """ dist_key = blockNames.FileParams.distance try: distance_str = config[dist_key] except KeyError as err: error = f"Missing key: {dist_key} in\n{config}" logging.exception(error) raise Exception(error) from err self.distance = parser.parse_distance(distance_str)
def fill_segment(self, segment_dict): """Fills the segment with the data in the dictionary Fills the segment with the data in the input dictionary. A segment type and a distance must be provided. The rest of parameters are optional but further analysis can be performed when provided (e.g. pace analysis or vspeed analysis). When one of pace or time are given, the other one is computed When both are given, pace is used to compute the time in order to enseure consistency. As expected, if none are given, they are left unfilled Units: distance: km time: sec climb: m pace: sec/km vspeed: m/h inclination: % Args: segment_dict (dict): Dictionary containing segment information Note: Dictionary keys are defined in constants.blockNames """ item_dict = dict((k.lower(), v) for k, v in segment_dict.items()) # If empty, return empty segment if item_dict == {}: return # Parsing compulsory elements # Type type_key = blockNames.FileParams.type try: type_ = item_dict[type_key] except KeyError as err: error = f"Missing key: {type_key} in\n{item_dict}" logging.exception(error) raise Exception(error) from err parsed_type = parser.parse_type(type_) if parsed_type is not None: self.type = parsed_type else: error = f"Unknown type in segment: {item_dict}" logging.exception(error) raise ValueError(error) # Distance str_distance = item_dict[blockNames.FileParams.distance] self.distance = parser.parse_distance(str_distance) # Date try: self.date = parser.parse_date( item_dict[blockNames.FileParams.date]) except KeyError: pass # Repetition try: self.repetition = item_dict[blockNames.FileParams.rep] except KeyError: try: self.repetition = item_dict[blockNames.FileParams.rep_alt] except KeyError: pass # Climb try: self.climb = item_dict[blockNames.FileParams.climb] except KeyError: pass # Inclination (in %) self.inclination = 100 * self.climb / (self.distance * 1000) # BPM try: self.bpm = item_dict[blockNames.FileParams.bpm] except KeyError: pass # Pace and time. # If one is given, the other is computed. # If both are given, pace is used to compute time for consistency try: pace_str = item_dict[blockNames.FileParams.pace] except KeyError: pace_str = None try: time_str = item_dict[blockNames.FileParams.time] except KeyError: time_str = None if pace_str is not None: self.pace = parser.parse_pace(pace_str) self.time = self.distance * self.pace elif time_str is not None: self.time = parser.parse_time(time_str) * 60 self.pace = self.time / self.distance else: parsed_time = None parsed_pace = None if self.time is not None: self.vspeed = int(self.climb * 3600. / self.time) # vspeed in m/h
def test_parse_totaldistance6(self): parsed_dist = parser.parse_distance("9.03") self.assertEqual(parsed_dist, 9.03)
def test_parse_totaldistance4(self): parsed_dist = parser.parse_distance("9km") self.assertEqual(parsed_dist, 9)
def test_parse_totaldistance3(self): parsed_dist = parser.parse_distance("9.8KM") self.assertEqual(parsed_dist, 9.8)
def test_parse_totaldistance2(self): parsed_dist = parser.parse_distance(9.8) self.assertEqual(parsed_dist, 9.8)