Skip to content

Commit 6332997

Browse files
committed
PEP8 fixes for test script.
1 parent 71b8388 commit 6332997

File tree

1 file changed

+66
-40
lines changed

1 file changed

+66
-40
lines changed

clusterTests.py

Lines changed: 66 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from difflib import SequenceMatcher
2020
import unittest
2121

22+
2223
def compare_list(x, y):
2324
"""
2425
Compare lists by content. Ordering does not matter.
@@ -41,18 +42,26 @@ def compare_list(x, y):
4142

4243

4344
class HClusterSmallListTestCase(unittest.TestCase):
44-
" Test for Bug #1516204 "
45+
"""
46+
Test for Bug #1516204
47+
"""
4548

4649
def testClusterLen1(self):
47-
"Testing if hierarchical clustering a set of length 1 returns a set of length 1"
48-
cl = HierarchicalClustering([876], lambda x,y: abs(x-y))
50+
"""
51+
Testing if hierarchical clustering a set of length 1 returns a set of
52+
length 1
53+
"""
54+
cl = HierarchicalClustering([876], lambda x, y: abs(x - y))
4955
self.assertEqual([876], cl.getlevel(40))
5056

5157
def testClusterLen0(self):
52-
"Testing if hierarchical clustering an empty list returns an empty list"
53-
cl = HierarchicalClustering([], lambda x,y: abs(x-y))
58+
"""
59+
Testing if hierarchical clustering an empty list returns an empty list
60+
"""
61+
cl = HierarchicalClustering([], lambda x, y: abs(x - y))
5462
self.assertEqual([], cl.getlevel(40))
5563

64+
5665
class HClusterIntegerTestCase(unittest.TestCase):
5766

5867
def setUp(self):
@@ -61,28 +70,30 @@ def setUp(self):
6170

6271
def testCluster(self):
6372
"Basic Hierarchical Clustering test with integers"
64-
cl = HierarchicalClustering(self.__data, lambda x,y: abs(x-y))
73+
cl = HierarchicalClustering(self.__data, lambda x, y: abs(x - y))
6574
cl.cluster()
66-
self.assertEqual( [
75+
self.assertEqual([
6776
[24],
6877
[84, 124, 131, 134],
6978
[336, 365, 365, 365, 398, 391],
7079
[940, 956, 971],
7180
[791],
7281
[835],
7382
[676],
74-
[518, 564, 542]
75-
],
83+
[518, 564, 542]],
7684
cl.getlevel(40))
7785

86+
7887
class HClusterStringTestCase(unittest.TestCase):
7988

8089
def sim(self, x, y):
8190
sm = SequenceMatcher(lambda x: x in ". -", x, y)
82-
return 1-sm.ratio()
91+
return 1 - sm.ratio()
8392

8493
def setUp(self):
85-
self.__data = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut elit. Phasellus consequat ultricies mi. Sed congue leo at neque. Nullam.". split()
94+
self.__data = ("Lorem ipsum dolor sit amet, consectetuer adipiscing "
95+
"elit. Ut elit. Phasellus consequat ultricies mi. Sed congue "
96+
"leo at neque. Nullam."). split()
8697

8798
def testDataTypes(self):
8899
"Test for bug #?"
@@ -108,8 +119,8 @@ def testCluster(self):
108119
['ipsum'],
109120
['adipiscing']
110121
],
111-
cl.getlevel(0.5)
112-
)
122+
cl.getlevel(0.5))
123+
113124

114125
class KClusterSmallListTestCase(unittest.TestCase):
115126

@@ -125,66 +136,81 @@ def testClusterLen0(self):
125136
self.assertEqual([], cl.getclusters(2))
126137
self.assertEqual([], cl.getclusters(7))
127138

139+
128140
class KCluster2DTestCase(unittest.TestCase):
129141

130142
def testClusterCount(self):
131143
"Test that asking for less than 2 clusters raises an error"
132-
cl = KMeansClustering([876, 123, 344, 676], distance=lambda x,y: abs(x-y))
144+
cl = KMeansClustering([876, 123, 344, 676],
145+
distance=lambda x, y: abs(x - y))
133146
self.assertRaises(ClusteringError, cl.getclusters, 0)
134147
self.assertRaises(ClusteringError, cl.getclusters, 1)
135148

136149
def testNonsenseCluster(self):
137-
"Test that asking for more clusters than data-items available raises an error"
138-
cl = KMeansClustering([876, 123], distance=lambda x,y: abs(x-y))
150+
"""
151+
Test that asking for more clusters than data-items available raises an
152+
error
153+
"""
154+
cl = KMeansClustering([876, 123], distance=lambda x, y: abs(x - y))
139155
self.assertRaises(ClusteringError, cl.getclusters, 5)
140156

141157
def testUniformLength(self):
142-
"Test if there is an item in the cluster that has a different cardinality"
143-
data = [ (1,5), (2,5), (2,6), (3,4), (3,5), (3,6,7), (7,3), (8,1), (8,2), (8), (9,2), (9,3) ]
158+
"""
159+
Test if there is an item in the cluster that has a different
160+
cardinality
161+
"""
162+
data = [(1, 5), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6, 7), (7, 3), (8,
163+
1), (8, 2), (8), (9, 2), (9, 3)]
144164
self.assertRaises(ValueError, KMeansClustering, data)
145165

146166
def testPointDoubling(self):
147167
"test for bug #1604868"
148-
data = [ (18,13), (15, 12), (17,12), (18,12), (19,12), (16,11), (18, 11),
149-
(19,10), (0,0), (1, 4), (1,2), (2,3), (4,1), (4,3), (5,2), (6,1)]
168+
data = [(18, 13), (15, 12), (17, 12), (18, 12), (19, 12), (16, 11),
169+
(18, 11), (19, 10), (0, 0), (1, 4), (1, 2), (2, 3), (4, 1),
170+
(4, 3), (5, 2), (6, 1)]
150171
cl = KMeansClustering(data)
151172
clusters = cl.getclusters(2)
152-
expected = [[(18,13), (15, 12), (17,12), (18,12), (19,12), (16,11), (18, 11), (19,10)],
153-
[(0,0), (1, 4), (1,2), (2,3), (4,1), (5,2), (6,1), (4,3)]]
154-
self.assertTrue( compare_list(
173+
expected = [[(18, 13), (15, 12), (17, 12), (18, 12), (19, 12), (16,
174+
11), (18, 11), (19, 10)], [(0, 0), (1, 4), (1, 2), (2, 3), (4, 1),
175+
(5, 2), (6, 1), (4, 3)]]
176+
self.assertTrue(compare_list(
155177
clusters,
156-
expected ),
178+
expected),
157179
"Elements differ!\n%s\n%s" % (clusters, expected))
158180

159181
def testClustering(self):
160182
"Basic clustering test"
161-
data = [ (8,2), (7,3), (2,6), (3,5), (3,6), (1,5), (8,1), (3,4), (8,3), (9,2), (2,5), (9,3) ]
183+
data = [(8, 2), (7, 3), (2, 6), (3, 5), (3, 6), (1, 5), (8, 1), (3,
184+
4), (8, 3), (9, 2), (2, 5), (9, 3)]
162185
cl = KMeansClustering(data)
163-
clusters = cl.getclusters(2)
164186
self.assertEqual(
165187
cl.getclusters(2),
166188
[[(8, 2), (8, 1), (8, 3), (7, 3), (9, 2), (9, 3)],
167189
[(3, 5), (1, 5), (3, 4), (2, 6), (2, 5), (3, 6)]])
168190

191+
169192
class KClusterSFBugs(unittest.TestCase):
170193

171194
def testLostFunctionReference(self):
172195
"test for bug #1727558"
173-
cl = KMeansClustering([(1,1), (20,40), (20,41)], lambda x,y:x+y)
196+
cl = KMeansClustering([(1, 1), (20, 40), (20, 41)],
197+
lambda x, y: x + y)
174198
clusters = cl.getclusters(3)
175-
expected = [(1,1), (20,40), (20,41)]
176-
self.assertTrue( compare_list(
199+
expected = [(1, 1), (20, 40), (20, 41)]
200+
self.assertTrue(compare_list(
177201
clusters,
178-
expected ),
202+
expected),
179203
"Elements differ!\n%s\n%s" % (clusters, expected))
180204

181-
unittest.TextTestRunner(verbosity=2).run(
182-
unittest.TestSuite((
183-
unittest.makeSuite(HClusterSmallListTestCase),
184-
unittest.makeSuite(HClusterIntegerTestCase),
185-
unittest.makeSuite(HClusterStringTestCase),
186-
unittest.makeSuite(KClusterSmallListTestCase),
187-
unittest.makeSuite(KCluster2DTestCase),
188-
unittest.makeSuite(KClusterSFBugs),
189-
))
190-
)
205+
206+
if __name__ == '__main__':
207+
unittest.TextTestRunner(verbosity=2).run(
208+
unittest.TestSuite((
209+
unittest.makeSuite(HClusterSmallListTestCase),
210+
unittest.makeSuite(HClusterIntegerTestCase),
211+
unittest.makeSuite(HClusterStringTestCase),
212+
unittest.makeSuite(KClusterSmallListTestCase),
213+
unittest.makeSuite(KCluster2DTestCase),
214+
unittest.makeSuite(KClusterSFBugs),
215+
))
216+
)

0 commit comments

Comments
 (0)