Skip to content

Commit

Permalink
Merge branch 'master' of ssh://github.com/KarrLab/wc_utils
Browse files Browse the repository at this point in the history
  • Loading branch information
jonrkarr committed Dec 5, 2018
2 parents de70e84 + 9c57da2 commit b93e09f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
11 changes: 9 additions & 2 deletions tests/util/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
"""

import unittest
from wc_utils.util.list import is_sorted, transpose, difference, det_dedupe, elements_to_str
from wc_utils.util.list import is_sorted, transpose, difference, det_dedupe, det_find_dupes, elements_to_str


class TestTranspose(unittest.TestCase):
class TestListUtilities(unittest.TestCase):

def test_is_sorted(self):
self.assertTrue(is_sorted([1, 2, 3]))
Expand Down Expand Up @@ -43,6 +43,13 @@ def test_det_dedupe(self):
with self.assertRaises(TypeError):
det_dedupe([[]])

def test_det_find_dupes(self):
l = [0, 1, 2, 0, 1, 7, 1]
expected = [0, 1]
self.assertEqual(det_find_dupes(l), expected)
with self.assertRaises(TypeError):
det_find_dupes([[]])

def test_elements_to_str(self):
l = 'a b c'.split()
self.assertEqual(elements_to_str(l), l)
Expand Down
28 changes: 28 additions & 0 deletions wc_utils/util/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,34 @@ def det_dedupe(l):
s.add(e)
return t


def det_find_dupes(l):
""" Deterministically find dupes in an iterable
Returns the duplicates in `l`. That is, returns a new list that contains one instance of
each element that has multiple copies in `l` and orders these instances by their first occurrence in `l`.
Costs O(n), where n is the length of `l`.
Args:
l (:obj:`list`): a list with hashable elements
Returns:
:obj:`list`: a deterministically deduplicated copy of `l`
Raises:
`TypeError` if an element of `l` is an unhashable (mutable) type
"""
counts_to_2 = {}
dupes = []
for e in l:
if e not in counts_to_2:
counts_to_2[e] = 1
elif counts_to_2[e] == 1:
counts_to_2[e] += 1
dupes.append(e)
return dupes


def elements_to_str(l):
""" Convert each element in an iterator to a string representation
Expand Down
4 changes: 2 additions & 2 deletions wc_utils/workbook/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,10 +473,10 @@ def initialize_workbook(self):
return Workbook()

def get_sheet_names(self):
""" Get names of sheets contained within path
""" Get names of files contained within path glob
Returns:
obj:`list` of `str`: list of sheet names
obj:`list` of `str`: list of file names
Raises:
:obj:`ValueError`: if glob does not find any matching files
Expand Down

0 comments on commit b93e09f

Please sign in to comment.