from dataclasses import dataclass
class InfoMessage:
"""Информационное сообщение о тренировке."""
def __init__(self,
training_type,
duration,
distance,
speed,
calories,
):
self.training_type = training_type
self.duration = duration
self.distance = distance
self.speed = speed
self.calories = calories
def get_message(self) -> str:
return (f'Тип тренировки: {self.training_type}; '
f'Длительность: {self.duration:.3f} ч.; '
f'Дистанция: {self.distance:.3f} км; '
f'Ср. скорость: {self.speed:.3f} км/ч; '
f'Потрачено ккал: {self.calories:.3f}.')
class Training:
"""Базовый класс тренировки."""
LEN_STEP = 0.65
M_IN_KM = 1000
K1 = 0.035 # Коэффициент 1 для подсчета формулы калорий
K2 = 0.029 # Коэффициент 2 для подсчета в формуле калорий
CONVERT_H_TO_MIN = 60
def __init__(self,
action: int,
duration: float,
weight: float,
):
self.action = action
self.duration = duration
self.weight = weight
def get_distance(self) -> float: # результат в км
"""Получить дистанцию в км."""
dist_in_km = self.action * self.LEN_STEP / self.M_IN_KM
return dist_in_km
def get_mean_speed(self) -> float: # результат в км/ч
"""Получить среднюю скорость движения."""
mean_speed = self.get_distance() / self.duration
return mean_speed
def get_spent_calories(self) -> float:
"""Получить количество затраченных калорий."""
pass
def show_training_info(self) -> InfoMessage:
"""Вернуть информационное сообщение о выполненной тренировке."""
training_type = self.__class__.__name__
duration = self.duration
distance = self.get_distance()
speed = self.get_mean_speed()
calories = self.get_spent_calories()
return InfoMessage(training_type, duration, distance, speed, calories)
class Running(Training):
"""Тренировка: бег."""
CONVERT_H_TO_SEC = 60
CALORIES_MEAN_SPEED_MULTIPLIER = 18
CALORIES_MEAN_SPEED_SHIFT = 1.79
def __init__(self,
action: int,
duration: float,
weight: float,
):
super().__init__(action,
duration,
weight,
)
def get_spent_calories(self) -> float:
total_calories = (
self.CALORIES_MEAN_SPEED_MULTIPLIER * self.get_mean_speed()
+ self.CALORIES_MEAN_SPEED_SHIFT
) * self.weight / self.M_IN_KM * (
self.duration * self.CONVERT_H_TO_SEC
)
return total_calories
class SportsWalking(Training):
CONVERT_SPEED_TO_M = 0.278
CONVERT_HEIGHT = 100
K1 = 0.035 # Коэффициент 1 для подсчета формулы калорий
K2 = 0.029 # Коэффициент 2 для подсчета в формуле калорий
def __init__(self,
action: int,
duration: float,
weight: float,
height,
):
super().__init__(action,
duration,
weight,
)
self.height = height
def get_spent_calories(self) -> float:
speed_in_mps = self.get_mean_speed() * self.CONVERT_SPEED_TO_M
height_in_m = self.height / 100
duration_in_min = self.duration * self.CONVERT_H_TO_MIN
spent_calories = (
self.K1 * self.weight + (
speed_in_mps ** 2 / height_in_m
) * self.K2 * self.weight
) * duration_in_min
return spent_calories
class Swimming(Training):
"""Тренировка: плавание."""
LEN_STEP = 1.38
COEFF_SWM_1 = 1.1
COEFF_SWM_2 = 2
def __init__(self,
action: int,
duration: float,
weight: float,
length_pool,
count_pool,
):
super().__init__(action,
duration,
weight,
)
self.length_pool = length_pool
self.count_pool = count_pool
def get_mean_speed(self) -> float:
"""Переопределение метода средней скорости для плавания."""
mean_speed_swimming = (
self.length_pool * self.count_pool / self.M_IN_KM / self.duration
)
return mean_speed_swimming
def get_spent_calories(self) -> float:
"""Переопределение расчёта израсходованных калорий."""
total_calories = (
self.get_mean_speed() + self.COEFF_SWM_1
) * self.COEFF_SWM_2 * self.weight * self.duration
return total_calories
def read_package(workout_type: str,
data: list,
) -> Training:
"""Прочитать данные полученные от датчиков."""
trainings = {
'SWM': Swimming,
'RUN': Running,
'WLK': SportsWalking,
}
get_training_class = trainings.get(workout_type)
training_object = get_training_class(*data)
return training_object
def main(training: Training) -> None:
"""Главная функция."""
info = training.show_training_info()
print(info.get_message())
if __name__ == '__main__':
packages = [
('SWM', [720, 1, 80, 25, 40]),
('RUN', [15000, 1, 75]),
('WLK', [9000, 1, 75, 180]),
]
for workout_type, data in packages:
training = read_package(workout_type, data)
main(training)