Skip to content

Commit a6ed775

Browse files
author
allendowney
committed
Adding structshape_test.py
1 parent 5e70d0c commit a6ed775

3 files changed

Lines changed: 75 additions & 8 deletions

File tree

‎python2/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ danger.gif words.txt color_list.py structshape.py
2121

2222
TESTS = Gui_test.py Lumpy_test.py TurmiteWorld_test.py \
2323
AmoebaWorld_test.py World_test.py TurtleWorld_test.py \
24-
Sync_test.py CellWorld_test.py
24+
Sync_test.py CellWorld_test.py structshape_test.py
2525

2626
SCRIPTS = lumpy_example1.py lumpy_example2.py lumpy_example3.py \
2727

‎python2/structshape.py‎

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@
1313
"""
1414

1515
def structshape(ds):
16-
"""Returns a string that describes the shape of the given
17-
data structure."""
16+
"""Returns a string that describes the shape of a data structure.
17+
18+
ds: any Python object
19+
20+
Returns: string
21+
"""
1822
typename = type(ds).__name__
1923

2024
# handle sequences
@@ -46,7 +50,12 @@ def structshape(ds):
4650

4751

4852
def listrep(t):
49-
"""return a string representation of a list of type strings"""
53+
"""Returns a string representation of a list of type strings.
54+
55+
t: list of strings
56+
57+
Returns: string
58+
"""
5059
current = t[0]
5160
count = 0
5261
res = []
@@ -60,19 +69,33 @@ def listrep(t):
6069
append(res, current, count)
6170
return setrep(res)
6271

72+
6373
def setrep(s):
64-
"""return a string representation of a set of type strings"""
74+
"""Returns a string representation of a set of type strings.
75+
76+
s: set of strings
77+
78+
Returns: string
79+
"""
6580
rep = ', '.join(s)
6681
if len(s) == 1:
6782
return rep
6883
else:
6984
return '(' + rep + ')'
7085
return
7186

87+
7288
def append(res, typestr, count):
73-
"""res is a list of type strings; this function adds a new
74-
type string that represent (count) instances of type string
75-
(typestr)"""
89+
"""Adds a new element to a list of type strings.
90+
91+
Modifies res.
92+
93+
res: list of type strings
94+
typestr: the new type string
95+
count: how many of the new type there are
96+
97+
Returns: None
98+
"""
7699
if count == 1:
77100
rep = typestr
78101
else:

‎python2/structshape_test.py‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""This module is part of Swampy, a suite of programs available from
2+
allendowney.com/swampy.
3+
4+
Copyright 2011 Allen B. Downey
5+
Distributed under the GNU General Public License at gnu.org/licenses/gpl.html.
6+
"""
7+
8+
import unittest
9+
10+
from structshape import structshape
11+
12+
class Tests(unittest.TestCase):
13+
14+
def test_lumpy(self):
15+
t = [1,2,3]
16+
self.assertEqual(structshape(t), 'list of 3 int')
17+
18+
t2 = [[1,2], [3,4], [5,6]]
19+
self.assertEqual(structshape(t2), 'list of 3 list of 2 int')
20+
21+
t3 = [1, 2, 3, 4.0, '5', '6', [7], [8], 9]
22+
self.assertEqual(structshape(t3), 'list of (3 int, float, 2 str, 2 list of int, int)')
23+
24+
class Point:
25+
"""trivial object type"""
26+
27+
t4 = [Point(), Point()]
28+
self.assertEqual(structshape(t4), 'list of 2 Point')
29+
30+
s = set('abc')
31+
self.assertEqual(structshape(s), 'set of 3 str')
32+
33+
lt = zip(t, s)
34+
self.assertEqual(structshape(lt), 'list of 3 tuple of (int, str)')
35+
36+
d = dict(lt)
37+
self.assertEqual(structshape(d), 'dict of 3 int->str')
38+
39+
it = iter('abc')
40+
self.assertEqual(structshape(it), 'iterator of 3 str')
41+
42+
43+
if __name__ == '__main__':
44+
unittest.main()

0 commit comments

Comments
 (0)