generated from yandex-praktikum/hw_python_oop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
homework.py
159 lines (123 loc) · 5.1 KB
/
homework.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
from dataclasses import dataclass
@dataclass
class InfoMessage:
"""Информационное сообщение о тренировке."""
training_type: str
duration: float
distance: float
speed: float
calories: float
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: float = 0.65
M_IN_KM: int = 1000
MIN_IN_H: int = 60
def __init__(self,
action: int,
duration: float,
weight: float,
) -> None:
self.action = action
self.duration = duration
self.weight = weight
def get_distance(self) -> float:
"""Получить дистанцию в км."""
return self.action * self.LEN_STEP / self.M_IN_KM
def get_mean_speed(self) -> float:
"""Получить среднюю скорость движения."""
# Получаем дистанцию в километрах
avg_distance = self.get_distance()
# Возвращаем сред.скорость в км\ч
return avg_distance / self.duration
def get_spent_calories(self) -> float:
"""Получить количество затраченных калорий."""
raise NotImplementedError(
(f'Определите get_spent_calories в'
f'{self.__class__.__name__}'))
def show_training_info(self) -> InfoMessage:
"""Вернуть информационное сообщение о выполненной тренировке."""
InfoMessageObject = InfoMessage(
type(self).__name__,
self.duration,
self.get_distance(),
self.get_mean_speed(),
self.get_spent_calories()
)
return InfoMessageObject
class Running(Training):
"""Тренировка: бег."""
CALORIES_MEAN_SPEED_MULTIPLIER: float = 18
CALORIES_MEAN_SPEED_SHIFT: float = 1.79
def get_spent_calories(self) -> float:
return ((self.CALORIES_MEAN_SPEED_MULTIPLIER
* self.get_mean_speed()
+ self.CALORIES_MEAN_SPEED_SHIFT)
* self.weight / self.M_IN_KM * self.duration * self.MIN_IN_H)
class SportsWalking(Training):
"""Тренировка: спортивная ходьба."""
CALORIES_WEIGHT_MULTIPLIER: float = 0.035
CALORIES_SPEED_HEIGHT_MULTIPLIER: float = 0.029
KMH_IN_MSEC: float = 0.278
CM_IN_M: float = 100
def __init__(self, action: int, duration: float,
weight: float, height: float) -> None:
self.height = height
super().__init__(action, duration, weight)
def get_spent_calories(self) -> float:
avg_speed_m_per_sec = (self.get_mean_speed()
* self.KMH_IN_MSEC)
result = (((self.CALORIES_WEIGHT_MULTIPLIER * self.weight
+ (avg_speed_m_per_sec**2 / (self.height / self.CM_IN_M))
* self.CALORIES_SPEED_HEIGHT_MULTIPLIER * self.weight)
* (self.duration * self.MIN_IN_H)))
return result
class Swimming(Training):
"""Тренировка: плавание."""
SPEED_PLUS: float = 1.1
WEIGHT_MULTIPLIER: float = 2
LEN_STEP: float = 1.38
def __init__(self, action: int, duration: float, weight: float,
length_pool: float, count_pool: float) -> None:
super().__init__(action, duration, weight)
self.length_pool = length_pool
self.count_pool = count_pool
def get_mean_speed(self) -> float:
result = (self.length_pool * self.count_pool
/ self.M_IN_KM / self.duration)
return result
def get_spent_calories(self) -> float:
result = ((self.get_mean_speed() + self.SPEED_PLUS)
* self.WEIGHT_MULTIPLIER * self.weight * self.duration)
return result
def read_package(workout_type: str, data: list) -> Training:
"""Прочитать данные полученные от датчиков."""
code_and_class_dict: dict[str, Training] = {
'SWM': Swimming,
'RUN': Running,
'WLK': SportsWalking
}
try:
object = code_and_class_dict[workout_type](*data)
return object
except KeyError:
raise KeyError('Такого типа тренировки нет.')
def main(training: Training) -> str:
"""Главная функция."""
info = training.show_training_info()
print(info.get_message())
if __name__ == '__main__':
packages = [
('SM', [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)