forked from pdeitel/PythonFundamentalsLiveLessons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimewithproperties.py
executable file
·83 lines (67 loc) · 2.86 KB
/
timewithproperties.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
# timewithproperties.py
"""Class Time with read-write properties."""
class Time:
"""Class Time with read-write properties."""
def __init__(self, hour=0, minute=0, second=0):
"""Initialize each attribute."""
self.hour = hour # 0-23
self.minute = minute # 0-59
self.second = second # 0-59
@property
def hour(self):
"""Return the hour."""
return self._hour
@hour.setter
def hour(self, hour):
"""Set the hour."""
if not (0 <= hour < 24):
raise ValueError(f'Hour ({hour}) must be 0-23')
self._hour = hour
@property
def minute(self):
"""Return the minute."""
return self._minute
@minute.setter
def minute(self, minute):
"""Set the minute."""
if not (0 <= minute < 60):
raise ValueError(f'Minute ({minute}) must be 0-59')
self._minute = minute
@property
def second(self):
"""Return the second."""
return self._second
@second.setter
def second(self, second):
"""Set the second."""
if not (0 <= second < 60):
raise ValueError(f'Second ({second}) must be 0-59')
self._second = second
def set_time(self, hour=0, minute=0, second=0):
"""Set values of hour, minute, and second."""
self.hour = hour
self.minute = minute
self.second = second
def __repr__(self):
"""Return Time string for repr()."""
return (f'Time(hour={self.hour}, minute={self.minute}, ' +
f'second={self.second})')
def __str__(self):
"""Return Time string in 12-hour clock format."""
return (('12' if self.hour in (0, 12) else str(self.hour % 12)) +
f':{self.minute:0>2}:{self.second:0>2}' +
(' AM' if self.hour < 12 else ' PM'))
##########################################################################
# (C) Copyright 2019 by Deitel & Associates, Inc. and #
# Pearson Education, Inc. All Rights Reserved. #
# #
# DISCLAIMER: The authors and publisher of this book have used their #
# best efforts in preparing the book. These efforts include the #
# development, research, and testing of the theories and programs #
# to determine their effectiveness. The authors and publisher make #
# no warranty of any kind, expressed or implied, with regard to these #
# programs or to the documentation contained in these books. The authors #
# and publisher shall not be liable in any event for incidental or #
# consequential damages in connection with, or arising out of, the #
# furnishing, performance, or use of these programs. #
##########################################################################