Skip to content

Commit 79d61d9

Browse files
committed
Updating code
1 parent 79ae7d0 commit 79d61d9

10 files changed

Lines changed: 223 additions & 203 deletions

code/BadKangaroo.py

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
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/
110
"""
211

3-
This program is part of an exercise in
4-
Think Python: An Introduction to Software Design
5-
Allen B. Downey
12+
from __future__ import print_function, division
13+
14+
"""
615
716
WARNING: this program contains a NASTY bug. I put
817
it there on purpose as a debugging exercise, but
@@ -11,33 +20,43 @@
1120
"""
1221

1322
class Kangaroo(object):
14-
"""a Kangaroo is a marsupial"""
23+
"""A Kangaroo is a marsupial."""
1524

16-
def __init__(self, contents=[]):
17-
"""initialize the pouch contents; the default value is
18-
an empty list"""
25+
def __init__(self, name, contents=[]):
26+
"""Initialize the pouch contents.
27+
28+
name: string
29+
contents: initial pouch contents.
30+
"""
31+
self.name = name
1932
self.pouch_contents = contents
2033

2134
def __str__(self):
22-
"""return a string representaion of this Kangaroo and
23-
the contents of the pouch, with one item per line"""
24-
t = [ object.__str__(self) + ' with pouch contents:' ]
35+
"""Return a string representaion of this Kangaroo.
36+
"""
37+
t = [ self.name + ' has pouch contents:' ]
2538
for obj in self.pouch_contents:
2639
s = ' ' + object.__str__(obj)
2740
t.append(s)
2841
return '\n'.join(t)
2942

3043
def put_in_pouch(self, item):
31-
"""add a new item to the pouch contents"""
44+
"""Adds a new item to the pouch contents.
45+
46+
item: object to be added
47+
"""
3248
self.pouch_contents.append(item)
3349

34-
kanga = Kangaroo()
35-
roo = Kangaroo()
50+
51+
kanga = Kangaroo('Kanga')
52+
roo = Kangaroo('Roo')
3653
kanga.put_in_pouch('wallet')
3754
kanga.put_in_pouch('car keys')
3855
kanga.put_in_pouch(roo)
3956

40-
print kanga
57+
print(kanga)
4158

4259
# If you run this program as is, it seems to work.
4360
# To see the problem, trying printing roo.
61+
62+
# Hint: to find the problem try running pylint.

code/GoodKangaroo.py

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
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/
110
"""
211

3-
This program is part of an exercise in
4-
Think Python: An Introduction to Software Design
5-
Allen B. Downey
12+
from __future__ import print_function, division
13+
14+
"""
615
7-
This program explains and corrects a bug in BadKangaroo.py.
8-
Before reading this, you should try to debug BadKangaroo.
16+
WARNING: this program contains a NASTY bug. I put
17+
it there on purpose as a debugging exercise, but
18+
you DO NOT want to emulate this example!
919
1020
"""
1121

1222
class Kangaroo(object):
13-
"""a Kangaroo is a marsupial"""
23+
"""A Kangaroo is a marsupial."""
1424

15-
def __init__(self, contents=[]):
25+
def __init__(self, name, contents=[]):
26+
"""Initialize the pouch contents.
27+
28+
name: string
29+
contents: initial pouch contents.
30+
"""
1631
# The problem is the default value for contents.
1732
# Default values get evaluated ONCE, when the function
1833
# is defined; they don't get evaluated again when the
@@ -23,48 +38,60 @@ def __init__(self, contents=[]):
2338
# an empty list.
2439

2540
# After that, every Kangaroo that gets the default
26-
# value get a reference to THE SAME list. If any
41+
# value gets a reference to THE SAME list. If any
2742
# Kangaroo modifies this shared list, they all see
2843
# the change.
2944

3045
# The next version of __init__ shows an idiomatic way
3146
# to avoid this problem.
47+
self.name = name
3248
self.pouch_contents = contents
3349

34-
def __init__(self, contents=None):
50+
def __init__(self, name, contents=None):
51+
"""Initialize the pouch contents.
52+
53+
name: string
54+
contents: initial pouch contents.
55+
"""
3556
# In this version, the default value is None. When
3657
# __init__ runs, it checks the value of contents and,
3758
# if necessary, creates a new empty list. That way,
38-
# every Kangaroo that gets the default value get a
59+
# every Kangaroo that gets the default value gets a
3960
# reference to a different list.
4061

4162
# As a general rule, you should avoid using a mutable
4263
# object as a default value, unless you really know
4364
# what you are doing.
65+
self.name = name
4466
if contents == None:
4567
contents = []
4668
self.pouch_contents = contents
4769

4870
def __str__(self):
49-
"""return a string representation of this Kangaroo and
50-
the contents of the pouch, with one item per line"""
51-
t = [ object.__str__(self) + ' with pouch contents:' ]
71+
"""Return a string representaion of this Kangaroo.
72+
"""
73+
t = [ self.name + ' has pouch contents:' ]
5274
for obj in self.pouch_contents:
5375
s = ' ' + object.__str__(obj)
5476
t.append(s)
5577
return '\n'.join(t)
5678

5779
def put_in_pouch(self, item):
58-
"""add a new item to the pouch contents"""
80+
"""Adds a new item to the pouch contents.
81+
82+
item: object to be added
83+
"""
5984
self.pouch_contents.append(item)
6085

61-
kanga = Kangaroo()
62-
roo = Kangaroo()
86+
87+
kanga = Kangaroo('Kanga')
88+
roo = Kangaroo('Roo')
6389
kanga.put_in_pouch('wallet')
6490
kanga.put_in_pouch('car keys')
6591
kanga.put_in_pouch(roo)
6692

67-
print kanga
68-
print ''
93+
print(kanga)
94+
print(roo)
6995

70-
print roo
96+
# If you run this program as is, it seems to work.
97+
# To see the problem, trying printing roo.

code/ackermann.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
"""This module contains code from
2-
Think Python by Allen B. Downey
3-
http://thinkpython.com
1+
"""This module contains a code example related to
42
5-
Copyright 2012 Allen B. Downey
6-
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
3+
Think Python, 2nd Edition
4+
by Allen Downey
5+
http://thinkpython2.com
76
7+
Copyright 2015 Allen Downey
8+
9+
License: http://creativecommons.org/licenses/by/4.0/
810
"""
911

12+
from __future__ import print_function, division
13+
14+
1015
def ackermann(m, n):
1116
"""Computes the Ackermann function A(m, n)
1217
@@ -20,4 +25,5 @@ def ackermann(m, n):
2025
return ackermann(m-1, 1)
2126
return ackermann(m-1, ackermann(m, n-1))
2227

23-
print ackermann(3, 4)
28+
29+
print(ackermann(3, 4))

code/ackermann_memo.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
"""This module contains code from
2-
Think Python by Allen B. Downey
3-
http://thinkpython.com
1+
"""This module contains a code example related to
42
5-
Copyright 2012 Allen B. Downey
6-
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
3+
Think Python, 2nd Edition
4+
by Allen Downey
5+
http://thinkpython2.com
76
7+
Copyright 2015 Allen Downey
8+
9+
License: http://creativecommons.org/licenses/by/4.0/
810
"""
911

12+
from __future__ import print_function, division
13+
1014
cache = {}
1115

1216
def ackermann(m, n):
@@ -26,5 +30,6 @@ def ackermann(m, n):
2630
cache[m, n] = ackermann(m-1, ackermann(m, n-1))
2731
return cache[m, n]
2832

29-
print ackermann(3, 4)
30-
print ackermann(3, 6)
33+
34+
print(ackermann(3, 4))
35+
print(ackermann(3, 6))

code/anagram_db.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
1-
"""This module contains code from
2-
Think Python by Allen B. Downey
3-
http://thinkpython.com
1+
"""This module contains a code example related to
42
5-
Copyright 2012 Allen B. Downey
6-
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
3+
Think Python, 2nd Edition
4+
by Allen Downey
5+
http://thinkpython2.com
76
7+
Copyright 2015 Allen Downey
8+
9+
License: http://creativecommons.org/licenses/by/4.0/
810
"""
911

12+
from __future__ import print_function, division
13+
1014
import shelve
1115
import sys
1216

13-
from anagram_sets import *
17+
from anagram_sets import all_anagrams, signature
1418

1519

16-
def store_anagrams(filename, ad):
17-
"""Stores the anagrams in ad in a shelf.
20+
def store_anagrams(filename, anagram_map):
21+
"""Stores the anagrams from a dictionary in a shelf.
1822
1923
filename: string file name of shelf
20-
ad: dictionary that maps strings to list of anagrams
24+
anagram_map: dictionary that maps strings to list of anagrams
2125
"""
2226
shelf = shelve.open(filename, 'c')
2327

24-
for word, word_list in ad.iteritems():
25-
shelf[word] = word_list
28+
for word, word_list in anagram_map.items():
29+
shelf[word] = word_list
2630

2731
shelf.close()
2832

@@ -41,13 +45,12 @@ def read_anagrams(filename, word):
4145
return []
4246

4347

44-
def main(name, command='store'):
45-
if command == 'store':
46-
ad = all_anagrams('words.txt')
47-
store_anagrams('anagrams.db', ad)
48+
def main(script, command='make_db'):
49+
if command == 'make_db':
50+
anagram_map = all_anagrams('words.txt')
51+
store_anagrams('anagrams.db', anagram_map)
4852
else:
49-
print read_anagrams('anagrams.db', command)
50-
53+
print(read_anagrams('anagrams.db', command))
5154

5255

5356
if __name__ == '__main__':

0 commit comments

Comments
 (0)