def increment(self, seconds: int) -> None: """ Increment the pace by number of seconds - for instructions like 'RP+15' :param seconds: :type seconds: int """ self.time = FirstTime(seconds=self.time.seconds + seconds)
def test_to_string(self): try: time = FirstTime(hours=2, minutes=15, seconds=12) self.assertEqual('2:15:12', str(time)) except ValueError as ex: self.fail(str(ex)) try: time = FirstTime() self.assertEqual('0:00:00', str(time)) except ValueError: self.fail('Should not fail') try: _ = FirstTime(minutes=-2, seconds=120) self.fail('FirstTime should not accept negative values') except ValueError as ex: self.assertEqual('negative values are invalid', str(ex))
def test_to_string(self): try: t1 = FirstTime(hours=2, minutes=15, seconds=12) self.assertEqual('2:15:12', str(t1)) except ValueError as ex: self.fail('Should not fail') try: t2 = FirstTime() self.assertEqual('0:00:00', str(t2)) except ValueError: self.fail('Should not fail') try: t3 = FirstTime(minutes=-2, seconds=120) self.fail('FirstTime should not accept negative values') except ValueError as ex: self.assertEqual('FirstTime.__init__ - does not allow negative values', str(ex))
def __parse_rows(self, rows): for row in rows: times = [] for col in row[0]: seconds = int(col[0].text) minutes = int(col[1].text) hours = int(col[2].text) times.append( FirstTime(seconds=seconds, minutes=minutes, hours=hours)) self.race_times.append(times)
def to_time(self, distance: FirstDistance, unit: str) -> float: """ How much time will take to run a given distance with this pace :param distance: the distance :type distance: FirstDistance :param unit: the desired unit of the result :type unit: str :return: the time value for this unit :rtype: float """ factor = distance.convert_to(unit=self.length_unit) seconds = self.time.total_seconds() * factor result_time = FirstTime(seconds=round(seconds)) return result_time.convert_to(unit=unit)
def __init__(self, minutes=0, seconds=0, length_unit='mile'): """ Constructor :param minutes: :type minutes: int :param seconds: :type seconds: int :param length_unit: :type length_unit: str :return: instance of FirstPace :rtype: FirstPace """ where_am_i = 'FirstPace.__init__' if not FirstDistance.is_valid_unit(length_unit): raise ValueError(where_am_i + ' - "%1s" is not a valid length unit' % length_unit) self.time = FirstTime(minutes=minutes, seconds=seconds) self.length_unit = length_unit
def __init__(self, minutes: int = 0, seconds: int = 0, length_unit: str = 'mile'): """ Constructor :param minutes: :type minutes: int :param seconds: :type seconds: int :param length_unit: :type length_unit: str :return: instance of FirstPace :rtype: FirstPace """ if not FirstDistance.is_valid_unit(unit=length_unit): raise ValueError( '"{}" is not a valid length unit'.format(length_unit)) self.time = FirstTime(minutes=minutes, seconds=seconds) self.length_unit = length_unit
def __parse_segments(self, segment_def, ref_race, pace_unit, segment_paces): where_am_i = 'FirstData.__parse_segments' index = 0 for segment in segment_def: name = segment[0].text segment_type = segment[1].text distance = None duration = None ref_pace_name = None if segment_type == 'DISTANCE': distance = FirstDistance(distance=float(segment[2][0].text), unit=segment[2][1].text) elif segment_type == 'TIME': duration = FirstTime(seconds=int(segment[2][0].text), minutes=int(segment[2][1].text), hours=int(segment[2][2].text)) ref_pace_name = segment[3].text else: # PACE ref_pace_name = segment[2].text self.segments.append( FirstSegment(name=name, distance=distance, duration=duration, ref_pace_name=ref_pace_name)) self.segments_lookup[name] = index index += 1 self.reference_race = ref_race.text dist_unit = pace_unit.text.split()[-1] for line in segment_paces: paces_list = [] first = True index = 0 for value in line.text.split(): if first: paces_list.append(FirstTime.from_string(value)) first = False else: # why incrementing index makes ref_segment.get_type undefined? time_string = '0:' + value ref_segment = self.segments[index] if ref_segment.get_type() == 'distance': cur_time = FirstTime.from_string(time_string) cur_dist = ref_segment.distance paces_list.append( FirstPace.from_time_distance(time=cur_time, distance=cur_dist, unit=dist_unit)) elif ref_segment.get_type() == 'pace': paces_list.append( FirstPace.from_string(time_string + ' ' + pace_unit.text)) else: raise ValueError( where_am_i + ' - Duration segments have already a reference pace' ) index = index + 1 self.segments_paces.append(paces_list)
def test_generate_workouts(self): data_file_path = expanduser( '~') + '/PycharmProjects/first/database/FIRSTregularPlans.xml' data = FirstData(xml_path=data_file_path) ws1 = [0, 2, 5] target_time = data.equivalent_time( time_from=FirstTime(minutes=30), race_index_from=data.race_type_index_by_name('5K'), race_index_to=data.race_type_index_by_name('Marathon')) sf_marathon = FirstRace( race_type=data.get_race_type_by_name('Marathon'), name='San Francisco Marathon', race_date=date(year=2017, month=7, day=23), target_time=target_time) me = FirstRunner(name='Daniel BenDavid', age=56, gender='m', email='*****@*****.**') p1 = FirstPlan(name='My first marathon training plan', weekly_schedule=ws1, race=sf_marathon, runner=me) try: # positive p1.generate_workouts(data=data) # print p1.details(1) self.assertEqual(48, len(p1.workouts)) wo = p1.workouts[0] self.assertEqual('Week 1 Keyrun 1', wo.name) self.assertEqual(3, len(wo.steps)) step = wo.steps[0] self.assertEqual('warmup', step.name) self.assertEqual(0, step.step_id) self.assertEqual('time', step.get_duration_type()) self.assertEqual('0:15:00', str(step.time)) self.assertEqual('0:11:31 min per mile', str(step.pace)) step = wo.steps[1] self.assertEqual('repeat X 3', step.name) self.assertEqual(1, step.step_id) self.assertEqual(3, step.repeat) # repeat self.assertEqual(2, len(step.steps)) substep = step.steps[0] self.assertEqual('1600m', substep.name) self.assertEqual(2, substep.step_id) self.assertEqual('distance', substep.get_duration_type()) self.assertEqual('1600.0 m', str(substep.distance)) self.assertEqual('0:09:26 min per mile', str(substep.pace)) substep = step.steps[1] self.assertEqual('200 m@RI', substep.name) self.assertEqual(3, substep.step_id) self.assertEqual('distance', substep.get_duration_type()) self.assertEqual('200.0 m', str(substep.distance)) self.assertEqual('0:11:31 min per mile', str(substep.pace)) step = wo.steps[2] self.assertEqual('cooldown', step.name) self.assertEqual(4, step.step_id) self.assertEqual('time', step.get_duration_type()) self.assertEqual('0:10:00', str(step.time)) self.assertEqual('0:11:31 min per mile', str(step.pace)) file_name = expanduser( '~/PycharmProjects/first/database/cmp_plan_marathon.tcx') # to_file = open(file_name, 'w') # to_file.write(p1.tcx()) # to_file.close() from_file = open(file_name) cmp_string = from_file.read() from_file.close() self.assertEqual(cmp_string, p1.tcx()) except ValueError as vex: self.fail(str(vex)) except TypeError as tex: self.fail(str(tex)) ws1 = [0, 3, 6] target_time = data.equivalent_time( time_from=FirstTime(minutes=22, seconds=36), race_index_from=data.race_type_index_by_name('5K'), race_index_to=data.race_type_index_by_name('HalfMarathon')) sf_half_marathon = FirstRace( race_type=data.get_race_type_by_name('HalfMarathon'), name='San Francisco Marathon', race_date=date(year=2017, month=7, day=23), target_time=target_time) me = FirstRunner(name='Daniel BenDavid', age=56, gender='m', email='*****@*****.**') p2 = FirstPlan(name='San Francisco half-marathon training plan', weekly_schedule=ws1, race=sf_half_marathon, runner=me) try: # positive p2.generate_workouts(data=data) # print p2.details(1) # print p2.details(2) file_name = expanduser( '~/PycharmProjects/first/database/cmp_plan_half_marathon.tcx') # to_file = open(file_name, 'w') # $ to_file.write(p2.tcx()) # to_file.close() from_file = open(file_name) cmp_string = from_file.read() from_file.close() self.assertEqual(cmp_string, p2.tcx()) except ValueError as vex: self.fail(str(vex)) except TypeError as tex: self.fail(str(tex))
def test_generate_workouts(self): data = FirstData(json_path=Config.DATABASE_JSON) ws1 = [0, 2, 5] target_time = data.equivalent_time(time_from=FirstTime(minutes=30), race_index_from=data.race_type_index_by_name('5K'), race_index_to=data.race_type_index_by_name('Marathon')) sf_marathon = FirstRace(race_type=data.get_race_type_by_name('Marathon'), name='San Francisco Marathon', race_date=date(year=2017, month=7, day=23), target_time=target_time) me = FirstRunner(name='Daniel BenDavid', age=56, gender='m', email='*****@*****.**') p1 = FirstPlan(name='My first marathon training plan', weekly_schedule=ws1, race=sf_marathon, runner=me) try: # positive p1.generate_workouts(data=data) self.assertEqual(48, len(p1.workouts)) wo = p1.workouts[0] self.assertEqual('Week 1 Keyrun 1', wo.name) self.assertEqual(3, len(wo.steps)) step = wo.steps[0] self.assertEqual('warmup', step.name) self.assertEqual(0, step.step_id) self.assertEqual('time', step.get_duration_type()) self.assertEqual('0:15:00', str(step.time)) self.assertEqual('0:11:31 min per mile', str(step.pace)) step = wo.steps[1] self.assertEqual('repeat X 3', step.name) self.assertEqual(1, step.step_id) self.assertEqual(3, step.repeat) # repeat self.assertEqual(2, len(step.steps)) substep = step.steps[0] self.assertEqual('1600m', substep.name) self.assertEqual(2, substep.step_id) self.assertEqual('distance', substep.get_duration_type()) self.assertEqual('1600.0 m', str(substep.distance)) self.assertEqual('0:09:26 min per mile', str(substep.pace)) substep = step.steps[1] self.assertEqual('200 m@RI', substep.name) self.assertEqual(3, substep.step_id) self.assertEqual('distance', substep.get_duration_type()) self.assertEqual('200.0 m', str(substep.distance)) self.assertEqual('0:11:31 min per mile', str(substep.pace)) step = wo.steps[2] self.assertEqual('cooldown', step.name) self.assertEqual(4, step.step_id) self.assertEqual('time', step.get_duration_type()) self.assertEqual('0:10:00', str(step.time)) self.assertEqual('0:11:31 min per mile', str(step.pace)) file_name = 'cmp_plan_marathon.tcx' with open('{}/{}'.format(Config.TEST_RESOURCE_DIR, file_name), 'r') as from_file: cmp_string = from_file.read() self.assertEqual(cmp_string, p1.tcx()) file_name = 'cmp_plan_marathon.json' with open('{}/{}'.format(Config.TEST_RESOURCE_DIR, file_name), 'r') as from_file: cmp_json = json.load(from_file) self.assertEqual(cmp_json, p1.to_json()) file_name = 'cmp_plan_marathon.html' with open('{}/{}'.format(Config.TEST_RESOURCE_DIR, file_name), 'r') as from_file: cmp_html = from_file.read() self.assertEqual(cmp_html, p1.to_html()) except ValueError as vex: self.fail(str(vex)) except TypeError as tex: self.fail(str(tex)) ws1 = [0, 3, 6] target_time = data.equivalent_time(time_from=FirstTime(minutes=22, seconds=36), race_index_from=data.race_type_index_by_name('5K'), race_index_to=data.race_type_index_by_name('HalfMarathon')) sf_half_marathon = FirstRace(race_type=data.get_race_type_by_name('HalfMarathon'), name='San Francisco Marathon', race_date=date(year=2017, month=7, day=23), target_time=target_time) me = FirstRunner(name='Daniel BenDavid', age=56, gender='m', email='*****@*****.**') p2 = FirstPlan(name='San Francisco half-marathon training plan', weekly_schedule=ws1, race=sf_half_marathon, runner=me) try: # positive p2.generate_workouts(data=data) file_name = 'cmp_plan_half_marathon.tcx' from_file = open('{}/{}'.format(Config.TEST_RESOURCE_DIR, file_name)) cmp_string = from_file.read() from_file.close() self.assertEqual(cmp_string, p2.tcx()) except ValueError as vex: self.fail(str(vex)) except TypeError as tex: self.fail(str(tex))