-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime1.py
77 lines (64 loc) · 2.06 KB
/
time1.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
from typing import List, Optional, NoReturn, TypeVar
class Time():
def __init__(self, hour:int, minute:int):
#debemos comprobar que el horario dado tenga sentido
if hour < 0 or hour >= 24:
raise ValueError('Hours in the day must be 0-23')
if minute < 0 or minute >= 60:
raise ValueError('Minutes in an hour must be 0-59')
self.__hour: int = hour
self.__minute: int = minute
@property
def hour(self):
return self.__hour
def hourSetter(self, value: int):
#comprobamos primero que sea corrrecto
if value < 0 or value >= 24:
raise ValueError('Hours in the day must be 0-23')
self.__hour = value
@property
def minute(self):
return self.__minute
def minuteSetter(self, value: int):
#comprobamos primero que sea correcto
if value < 0 or value >= 60:
raise ValueError('Minutes in an hour must be 0-59')
self.__minute = value
def __str__(self):
if self.__hour < 10:
if self.__minute < 10:
return f'0{self.__hour}:0{self.__minute}'
else:
return f'0{self.__hour}:{self.__minute}'
else:
if self.__minute < 10:
return f'{self.__hour}:0{self.__minute}'
else:
return f'{self.__hour}:{self.__minute}'
def __eq__(self, other):
return self.__minute == other.minute and self.__hour == other.hour
def __lt__(self, other):
return (self.__hour < other.hour) or (self.__hour == other.hour and self.__minute < other.minute)
def test():
#lo probamos con un código de ejemplo
hora1: Time = Time(3, 46)
hora2: Time = Time(13, 47)
hora3: Time = Time(3, 47)
hora4: Time = Time(13, 2)
hora5: Time = Time(6, 0)
print(hora1) #debe ser '03:46'
print(hora4) #debe ser '13:02'
print(hora5) #debe ser '06:00'
print(hora1 < hora2) #debe ser True
print(hora1 < hora3) #debe ser True
print(hora2 == hora3) #debe ser False
#debe dar error y saltar la excepcion
print()
try:
hora6: Time = Time(123, 6)
except:
print('ERROR. Hours in the day must be 0-23')
try:
hora7: Time = Time(12, -4)
except:
print('ERROR. Minutes in an hour must be 0-59')