def fill_time(self, config): """Fills time with data in input dictionary Fills time with the configuration dictionary. Args: config(dict): Dictionary with time information Notes: Time is a compulsory attribute """ time_key = blockNames.FileParams.time try: time_str = config[time_key] except KeyError as err: error = f"Missing key: {time_key} in\n{config}" logging.exception(error) raise Exception(error) from err self.time = parser.parse_time(time_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_totaltime9(self): parsed_time = parser.parse_time("1hr30mn30seg") self.assertEqual(parsed_time, 90.5)
def test_parse_totaltime5(self): parsed_time = parser.parse_time("1h30min30s") self.assertEqual(parsed_time, 90.5)
def test_parse_totaltime3(self): parsed_time = parser.parse_time("30s") self.assertEqual(parsed_time, 0.5)
def test_parse_totaltime2(self): parsed_time = parser.parse_time("1m") self.assertEqual(parsed_time, 1)
def test_parse_totaltime1(self): parsed_time = parser.parse_time("1h") self.assertEqual(parsed_time, 60)