Skip to content

Commit dbea219

Browse files
author
allendowney
committed
Bug fixed in structshape.py
1 parent ac71da6 commit dbea219

2 files changed

Lines changed: 101 additions & 38 deletions

File tree

‎python2/Makefile‎

Lines changed: 79 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,56 @@ PYDOC = pydoc
33
DEST = /home/downey/public_html/greent/thinkpython
44

55
pydoc:
6-
$(PYDOC) -w Gui
7-
$(PYDOC) -w World
8-
$(PYDOC) -w TurtleWorld
9-
$(PYDOC) -w CellWorld
10-
$(PYDOC) -w TurmiteWorld
116
$(PYDOC) -w AmoebaWorld
12-
$(PYDOC) -w Sync
7+
$(PYDOC) -w CellWorld
8+
$(PYDOC) -w color_list
9+
$(PYDOC) -w Gui
1310
$(PYDOC) -w Lumpy
11+
$(PYDOC) -w structshape
12+
$(PYDOC) -w Sync
13+
$(PYDOC) -w TurmiteWorld
14+
$(PYDOC) -w TurtleWorld
15+
$(PYDOC) -w World
1416
rsync -a *.html $(DEST)
1517

16-
FILES = __init__.py \
17-
AmoebaWorld.py mutex.py turtle_code.py\
18-
CellWorld.py Sync.py TurtleWorld.py World.py\
19-
Gui.py Lumpy.py TurmiteWorld.py \
20-
danger.gif words.txt color_list.py structshape.py \
21-
AmoebaWorld_test.py Lumpy_test.py TurmiteWorld_test.py \
22-
CellWorld_test.py structshape_test.py TurtleWorld_test.py \
23-
Gui_test.py Sync_test.py World_test.py
18+
FILES = AmoebaWorld.py \
19+
CellWorld.py \
20+
color_list.py \
21+
Gui.py \
22+
lumpy_example1.py \
23+
lumpy_example2.py \
24+
lumpy_example3.py \
25+
Lumpy.py \
26+
mutex.py \
27+
structshape.py \
28+
Sync.py \
29+
TurmiteWorld.py \
30+
turtle_code.py \
31+
TurtleWorld.py \
32+
World.py \
33+
danger.gif \
34+
words.txt \
35+
36+
TESTS = AmoebaWorld_test.py \
37+
CellWorld_test.py \
38+
Gui_test.py \
39+
Lumpy_test.py \
40+
structshape_test.py \
41+
Sync_test.py \
42+
TurmiteWorld_test.py \
43+
TurtleWorld_test.py \
44+
World_test.py \
2445

25-
TESTS = Gui_test.py Lumpy_test.py TurmiteWorld_test.py \
26-
AmoebaWorld_test.py World_test.py TurtleWorld_test.py \
27-
Sync_test.py CellWorld_test.py structshape_test.py
46+
SCRIPTS =
2847

29-
SCRIPTS = lumpy_example1.py lumpy_example2.py lumpy_example3.py \
48+
DOCS = AmoebaWorld.html \
49+
CellWorld.html \
50+
Gui.html \
51+
Lumpy.html \
52+
Sync.html \
53+
TurmiteWorld.html \
54+
TurtleWorld.html \
55+
World.html \
3056

3157
FLAGS = -wn -x import
3258

@@ -36,48 +62,68 @@ color:
3662
rsync color_list.py /home/downey/public_html/greent/thinkpython/code
3763
rsync color_list.py /home/downey/public_html/color
3864

65+
# cd ~/swampy/trunk
66+
# edit Makefile and update versions
67+
# make all
68+
# make zip
69+
# make zipdoc
70+
# make distrib
71+
3972
python3:
4073
cp $(FILES) ../python3
4174
cp sync_code/*.py ../python3/sync_code
42-
cd ../python3; 2to3 $(FLAGS) Gui.py
43-
cd ../python3; 2to3 $(FLAGS) World.py
44-
cd ../python3; 2to3 $(FLAGS) TurtleWorld.py
45-
cd ../python3; 2to3 $(FLAGS) CellWorld.py
46-
cd ../python3; 2to3 $(FLAGS) TurmiteWorld.py
4775
cd ../python3; 2to3 $(FLAGS) AmoebaWorld.py
76+
cd ../python3; 2to3 $(FLAGS) CellWorld.py
77+
cd ../python3; 2to3 $(FLAGS) color_list.py
78+
cd ../python3; 2to3 $(FLAGS) Gui.py
4879
cd ../python3; 2to3 $(FLAGS) Lumpy.py
80+
cd ../python3; 2to3 $(FLAGS) structshape.py
4981
cd ../python3; 2to3 $(FLAGS) Sync.py
50-
cd ../python3; 2to3 $(FLAGS) color_list.py
82+
cd ../python3; 2to3 $(FLAGS) TurmiteWorld.py
83+
cd ../python3; 2to3 $(FLAGS) TurtleWorld.py
84+
cd ../python3; 2to3 $(FLAGS) World.py
85+
5186

52-
cd ../python3; 2to3 $(FLAGS) Gui_test.py
53-
cd ../python3; 2to3 $(FLAGS) World_test.py
54-
cd ../python3; 2to3 $(FLAGS) TurtleWorld_test.py
55-
cd ../python3; 2to3 $(FLAGS) CellWorld_test.py
56-
cd ../python3; 2to3 $(FLAGS) TurmiteWorld_test.py
5787
cd ../python3; 2to3 $(FLAGS) AmoebaWorld_test.py
88+
cd ../python3; 2to3 $(FLAGS) CellWorld_test.py
89+
cd ../python3; 2to3 $(FLAGS) Gui_test.py
5890
cd ../python3; 2to3 $(FLAGS) Lumpy_test.py
59-
cd ../python3; 2to3 $(FLAGS) Sync_test.py
6091
cd ../python3; 2to3 $(FLAGS) structshape_test.py
61-
62-
PACKAGE = swampy
92+
cd ../python3; 2to3 $(FLAGS) TurmiteWorld_test.py
93+
cd ../python3; 2to3 $(FLAGS) TurtleWorld_test.py
94+
cd ../python3; 2to3 $(FLAGS) Sync_test.py
95+
cd ../python3; 2to3 $(FLAGS) World_test.py
6396

6497
# to update Swampy on PyPI
6598
# cd ~/swampy/trunk/python2/swampy
6699
# emacs CHANGES.txt setup.py
100+
# update the version number below
101+
# maybe also emacs MANIFEST
67102
# cd back to this directory
68103
# make package
69104
# make dist
70105

106+
# cd ~/downey/public_html/swampy
107+
# make install.html point to new version
108+
# cd ~/public_html; sh back
109+
# cd ~/public_html/greent; sh back
110+
111+
PACKAGE = swampy
112+
ZIP = swampy-2.1.5.zip
113+
71114
package:
72115
rsync $(FILES) $(PACKAGE)/swampy
73116
rsync $(TESTS) $(PACKAGE)/test
74117
rsync $(SCRIPTS) $(PACKAGE)/bin
118+
cd $(PACKAGE); zip -r $(ZIP) swampy
75119

76120
install:
77121
cd $(PACKAGE); sudo python setup.py install
78122

79123
dist:
80-
cd $(PACKAGE); python setup.py sdist upload
124+
cd $(PACKAGE); rsync $(ZIP) $(DEST)
125+
cd $(PACKAGE); python setup.py sdist --formats gztar,zip upload
126+
cd /home/downey/public_html/greent; sh back
81127

82128
clean:
83129
rm *~ *.pyc

‎python2/structshape.py‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,29 @@
1212
1313
"""
1414

15-
def structshape(ds):
15+
def structshape(ds, pending=None):
1616
"""Returns a string that describes the shape of a data structure.
1717
1818
ds: any Python object
1919
2020
Returns: string
2121
"""
22+
if pending is None:
23+
pending = set()
24+
25+
if id(ds) in pending:
26+
return 'infinity'
27+
28+
pending = pending | set([id(ds)])
29+
2230
typename = type(ds).__name__
2331

2432
# handle sequences
25-
sequence = (list, tuple, set, type(iter('')))
33+
sequence = (list, tuple, set, frozenset, type(iter('')))
2634
if isinstance(ds, sequence):
2735
t = []
2836
for i, x in enumerate(ds):
29-
t.append(structshape(x))
37+
t.append(structshape(x, pending))
3038
rep = '%s of %s' % (typename, listrep(t))
3139
return rep
3240

@@ -35,8 +43,8 @@ def structshape(ds):
3543
keys = set()
3644
vals = set()
3745
for k, v in ds.items():
38-
keys.add(structshape(k))
39-
vals.add(structshape(v))
46+
keys.add(structshape(k, pending))
47+
vals.add(structshape(v, pending))
4048
rep = '%s of %d %s->%s' % (typename, len(ds),
4149
setrep(keys), setrep(vals))
4250
return rep
@@ -56,6 +64,9 @@ def listrep(t):
5664
5765
Returns: string
5866
"""
67+
if len(t) == 0:
68+
return 'empty'
69+
5970
current = t[0]
6071
count = 0
6172
res = []
@@ -104,6 +115,8 @@ def append(res, typestr, count):
104115

105116

106117
if __name__ == '__main__':
118+
t = []
119+
print structshape(t)
107120

108121
t = [1,2,3]
109122
print structshape(t)
@@ -131,3 +144,7 @@ class Point:
131144

132145
it = iter('abc')
133146
print structshape(it)
147+
148+
t = []
149+
t.append(t)
150+
print structshape(t)

0 commit comments

Comments
 (0)