|
| 1 | +"""This module contains a code example related to |
| 2 | +
|
| 3 | +Think Python, 2nd Edition |
| 4 | +by Allen Downey |
| 5 | +http://thinkpython2.com |
| 6 | +
|
| 7 | +Copyright 2015 Allen Downey |
| 8 | +
|
| 9 | +License: http://creativecommons.org/licenses/by/4.0/ |
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import print_function, division |
| 13 | + |
| 14 | + |
| 15 | +class Time: |
| 16 | + """Represents the time of day. |
| 17 | +
|
| 18 | + attributes: hour, minute, second |
| 19 | + """ |
| 20 | + def __init__(self, hour=0, minute=0, second=0): |
| 21 | + """Initializes a time object. |
| 22 | +
|
| 23 | + hour: int |
| 24 | + minute: int |
| 25 | + second: int or float |
| 26 | + """ |
| 27 | + self.hour = hour |
| 28 | + self.minute = minute |
| 29 | + self.second = second |
| 30 | + |
| 31 | + def __str__(self): |
| 32 | + """Returns a string representation of the time.""" |
| 33 | + return '%.2d:%.2d:%.2d' % (self.hour, self.minute, self.second) |
| 34 | + |
| 35 | + def print_time(self): |
| 36 | + """Prints a string representation of the time.""" |
| 37 | + print(str(self)) |
| 38 | + |
| 39 | + def time_to_int(self): |
| 40 | + """Computes the number of seconds since midnight.""" |
| 41 | + minutes = self.hour * 60 + self.minute |
| 42 | + seconds = minutes * 60 + self.second |
| 43 | + return seconds |
| 44 | + |
| 45 | + def is_after(self, other): |
| 46 | + """Returns True if t1 is after t2; false otherwise.""" |
| 47 | + return self.time_to_int() > other.time_to_int() |
| 48 | + |
| 49 | + def __add__(self, other): |
| 50 | + """Adds two Time objects or a Time object and a number. |
| 51 | +
|
| 52 | + other: Time object or number of seconds |
| 53 | + """ |
| 54 | + if isinstance(other, Time): |
| 55 | + return self.add_time(other) |
| 56 | + else: |
| 57 | + return self.increment(other) |
| 58 | + |
| 59 | + def __radd__(self, other): |
| 60 | + """Adds two Time objects or a Time object and a number.""" |
| 61 | + return self.__add__(other) |
| 62 | + |
| 63 | + def add_time(self, other): |
| 64 | + """Adds two time objects.""" |
| 65 | + assert self.is_valid() and other.is_valid() |
| 66 | + seconds = self.time_to_int() + other.time_to_int() |
| 67 | + return int_to_time(seconds) |
| 68 | + |
| 69 | + def increment(self, seconds): |
| 70 | + """Returns a new Time that is the sum of this time and seconds.""" |
| 71 | + seconds += self.time_to_int() |
| 72 | + return int_to_time(seconds) |
| 73 | + |
| 74 | + def is_valid(self): |
| 75 | + """Checks whether a Time object satisfies the invariants.""" |
| 76 | + if self.hour < 0 or self.minute < 0 or self.second < 0: |
| 77 | + return False |
| 78 | + if self.minute >= 60 or self.second >= 60: |
| 79 | + return False |
| 80 | + return True |
| 81 | + |
| 82 | + |
| 83 | +def int_to_time(seconds): |
| 84 | + """Makes a new Time object. |
| 85 | +
|
| 86 | + seconds: int seconds since midnight. |
| 87 | + """ |
| 88 | + minutes, second = divmod(seconds, 60) |
| 89 | + hour, minute = divmod(minutes, 60) |
| 90 | + time = Time(hour, minute, second) |
| 91 | + return time |
| 92 | + |
| 93 | + |
| 94 | +def main(): |
| 95 | + start = Time(9, 45, 00) |
| 96 | + start.print_time() |
| 97 | + |
| 98 | + end = start.increment(1337) |
| 99 | + #end = start.increment(1337, 460) |
| 100 | + end.print_time() |
| 101 | + |
| 102 | + print('Is end after start?') |
| 103 | + print(end.is_after(start)) |
| 104 | + |
| 105 | + print('Using __str__') |
| 106 | + print(start, end) |
| 107 | + |
| 108 | + start = Time(9, 45) |
| 109 | + duration = Time(1, 35) |
| 110 | + print(start + duration) |
| 111 | + print(start + 1337) |
| 112 | + print(1337 + start) |
| 113 | + |
| 114 | + print('Example of polymorphism') |
| 115 | + t1 = Time(7, 43) |
| 116 | + t2 = Time(7, 41) |
| 117 | + t3 = Time(7, 37) |
| 118 | + total = sum([t1, t2, t3]) |
| 119 | + print(total) |
| 120 | + |
| 121 | + |
| 122 | +if __name__ == '__main__': |
| 123 | + main() |
0 commit comments