Skip to content

Commit f77df2d

Browse files
Replaced two lines repeated twice in function genmatrix with a function call, which is intended to be identical in effect, but provides an informative log message.
With this commit, the behaviour of the test_hierarchical.py test set remains identical to python-cluster 1.4.1 under both Python2.7 (which passes)_and Python3.5 (which fails with a KeyError thrown).
1 parent 28db316 commit f77df2d

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

cluster/matrix.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,30 @@
2222

2323
logger = logging.getLogger(__name__)
2424

25+
def _encapsulate_item_for_combinfunc(item):
26+
"""
27+
This function has been extracted in order to
28+
make Github issue #28 easier to investigate.
29+
It replaces the following two lines of code,
30+
which occur twice in method genmatrix, just
31+
before the invocation of combinfunc.
32+
if not hasattr(item, '__iter__') or isinstance(item, tuple):
33+
item = [item]
34+
Logging has been added to the original two lines
35+
and shows that the behaviour of this snippet
36+
has changed between Python2.7 and Python3.5.
37+
"""
38+
encapsulated_item = None
39+
if not hasattr(item, '__iter__') or isinstance(item, tuple):
40+
encapsulated_item = [item]
41+
else:
42+
encapsulated_item = item
43+
logging.debug(
44+
"item class:%s encapsulated as:%s ",
45+
item.__class__, encapsulated_item.__class__
46+
)
47+
return encapsulated_item
48+
2549

2650
class Matrix(object):
2751
"""
@@ -123,10 +147,17 @@ def genmatrix(self, num_processes=1):
123147
num_tasks_completed += 1
124148
else:
125149
# Otherwise do it here, in line
150+
"""
126151
if not hasattr(item, '__iter__') or isinstance(item, tuple):
127152
item = [item]
128153
if not hasattr(item2, '__iter__') or isinstance(item2, tuple):
129154
item2 = [item2]
155+
"""
156+
# See the comment in function _encapsulate_item_for_combinfunc
157+
# for details of why the lines above have been replaced
158+
# by function invocations
159+
item = _encapsulate_item_for_combinfunc(item)
160+
item2 = _encapsulate_item_for_combinfunc(item2)
130161
row[col_index] = self.combinfunc(item, item2)
131162

132163
if self.symmetric:

0 commit comments

Comments
 (0)