Skip to content

Commit cf236fd

Browse files
author
CindeeM
committed
RF added contains_only to reduce repetition, simplify code based on @stefanv suggestions
1 parent e4c9f40 commit cf236fd

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

brainx/tests/test_util.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#-----------------------------------------------------------------------------
1919

2020
def make_testlists():
21+
""" setup some data for the conversion Tests
22+
eg lislist_to_listset"""
2123
listlist = [[0,1,2],[3,4,5],[6,7,8,9]]
2224
dictset = {val:set(item) for val, item in enumerate(listlist)}
2325
listset = [set(item) for item in listlist]
@@ -28,6 +30,7 @@ def test_dictset_to_listset():
2830
new_list_set = util.dictset_to_listset(dictset)
2931
npt.assert_equal(new_list_set, listset)
3032
with npt.assert_raises(ValueError):
33+
# catch bad type
3134
tmp = util.dictset_to_listset(listset)
3235

3336
def test_listset_to_dictset():
@@ -41,13 +44,21 @@ def test_listset_to_dictset():
4144
def test_no_repeats_in_listlist():
4245
jnk = [[0,1,2],[3,4,5]] # all unique
4346
nt.assert_true(util._no_repeats_in_listlist(jnk))
44-
jnk = [[0,1,2], [0,1,2]]
47+
jnk = [[0,1,2], [0,1,2]] # repeats
4548
nt.assert_false(util._no_repeats_in_listlist(jnk))
4649
with npt.assert_raises(ValueError):
4750
util._no_repeats_in_listlist({0:0})
4851
with npt.assert_raises(ValueError):
4952
util._no_repeats_in_listlist([set([0,1,2])])
5053

54+
def test_contains_only():
55+
listlist, listset, dictset = make_testlists()
56+
nt.assert_true(util._contains_only(listlist, list))
57+
nt.assert_true(util._contains_only(listset, set))
58+
nt.assert_true(util._contains_only(dictset, set))
59+
nt.assert_false(util._contains_only([1,2,3], set))
60+
61+
5162
def test_listlist_to_listset():
5263
listlist, listset, _ = make_testlists()
5364
new_listset = util.listlist_to_listset(listlist)

brainx/util.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
def dictset_to_listset(dict_set):
1717
""" converts a dict of sets to a list of sets
1818
for converting partition.community objects"""
19-
if (type(dict_set) == type({}) \
20-
and all(type(x) == type(set()) for x in dict_set.values())):
19+
if isinstance(dict_set, dict) \
20+
and _contains_only(dict_set, set):
2121
return dict_set.values()
2222

2323
raise ValueError('{0} is not a dict of sets'.format(dict_set))
@@ -26,20 +26,29 @@ def listset_to_dictset(list_set):
2626
""" converts a list of sets to a dict of sets
2727
for converting partition.community objects"""
2828
## check input is dict of sets
29-
if (type(list_set)==type([]) and \
30-
all(type(x) == type(set()) for x in list_set)):
29+
if isinstance(list_set, list) and \
30+
_contains_only(list_set, set):
3131
return {val: value for val, value in enumerate(list_set)}
3232
raise ValueError('{0} is not a list of sets'.format(list_set))
3333

3434
def _no_repeats_in_listlist(list_list):
3535
""" checks for duplicates in list of lists
3636
returns True or False"""
37-
if type(list_list) == type([]) and \
38-
all(type(x) == type([]) for x in list_list):
37+
if isinstance(list_list, list) and \
38+
_contains_only(list_list, list):
3939
allitems = [item for sublist in list_list for item in sublist]
40-
return sorted(allitems) == [x for x in sorted(set(allitems))]
40+
return len(allitems) == len(set(allitems))
4141
raise ValueError('{0} is not a list of lists'.format(list_list))
4242

43+
def _contains_only(container, type):
44+
"""check that contents of a container are all fo the same type"""
45+
try:
46+
# dict
47+
return all(isinstance(s, type) for s in container.values())
48+
except:
49+
# others
50+
return all(isinstance(s,type) for s in container)
51+
4352
def listlist_to_listset(list_list):
4453
""" converts list of lists to a list of sets (with check)
4554
for converting partition.community objects"""

0 commit comments

Comments
 (0)