Skip to content

Commit 84ceac6

Browse files
author
KitN
committed
more work with oop
1 parent 0f7175e commit 84ceac6

3 files changed

Lines changed: 150 additions & 1 deletion

File tree

code/BadKangaroo.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def __init__(self, name, contents=[]):
2929
contents: initial pouch contents.
3030
"""
3131
self.name = name
32-
self.pouch_contents = contents
32+
self.pouch_contents = [i for i in contents]
3333

3434
def __str__(self):
3535
"""Return a string representaion of this Kangaroo.
@@ -45,6 +45,7 @@ def put_in_pouch(self, item):
4545
4646
item: object to be added
4747
"""
48+
print(f'Adding {item} to a pouch')
4849
self.pouch_contents.append(item)
4950

5051

@@ -55,6 +56,7 @@ def put_in_pouch(self, item):
5556
kanga.put_in_pouch(roo)
5657

5758
print(kanga)
59+
print(roo)
5860

5961
# If you run this program as is, it seems to work.
6062
# To see the problem, trying printing roo.

exersizes/Kangaroo.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# For ThinkPython Chapter 17 Exersize 2
2+
3+
class Kangaroo():
4+
def __init__(self):
5+
self.pouch_contents = []
6+
7+
def __str__(self):
8+
return "Pouch: " + str(self.pouch_contents)
9+
10+
def put_in_pouch(self, thing):
11+
self.pouch_contents += [thing]
12+
13+
14+
def main():
15+
kanga = Kangaroo()
16+
roo = Kangaroo()
17+
roo.put_in_pouch('skittle')
18+
kanga.put_in_pouch(roo)
19+
print(kanga)
20+
print(roo)
21+
22+
if __name__=='__main__':
23+
main()

exersizes/Time2.py

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
seconds: int of seconds since midnight
24+
"""
25+
self.seconds = 0
26+
self.seconds += hour*60*60
27+
self.seconds += minute*60
28+
self.seconds += second
29+
30+
def __str__(self):
31+
"""Returns a string representation of the time."""
32+
second = self.seconds % 60
33+
minute = self.seconds % 3600 // 60
34+
hour = self.seconds // 3600
35+
return '%.2d:%.2d:%.2d' % (hour, minute, second)
36+
37+
def print_time(self):
38+
"""Prints a string representation of the time."""
39+
print(str(self))
40+
41+
def time_to_int(self):
42+
"""Computes the number of seconds since midnight."""
43+
return self.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+
midnight = 24*60*60
77+
if self.seconds < 0:
78+
return False
79+
if self.seconds >= midnight:
80+
return False
81+
return True
82+
83+
84+
def int_to_time(seconds):
85+
"""Makes a new Time object.
86+
87+
seconds: int seconds since midnight.
88+
"""
89+
minutes, second = divmod(seconds, 60)
90+
hour, minute = divmod(minutes, 60)
91+
time = Time(hour, minute, second)
92+
return time
93+
94+
95+
def main():
96+
start = Time(9, 45, 00)
97+
start.print_time()
98+
99+
end = start.increment(1337)
100+
#end = start.increment(1337, 460)
101+
end.print_time()
102+
103+
print('Is end after start?')
104+
print(end.is_after(start))
105+
106+
print('Using __str__')
107+
print(start, end)
108+
109+
start = Time(9, 45)
110+
duration = Time(1, 35)
111+
print(start + duration)
112+
print(start + 1337)
113+
print(1337 + start)
114+
115+
print('Example of polymorphism')
116+
t1 = Time(7, 43)
117+
t2 = Time(7, 41)
118+
t3 = Time(7, 37)
119+
total = sum([t1, t2, t3])
120+
print(total)
121+
122+
123+
if __name__ == '__main__':
124+
main()

0 commit comments

Comments
 (0)