Skip to content

Commit 15957f5

Browse files
committed
Move deduplicate function to util.unique
Also do some minor cosmetic refactoring Signed-off-by: Philippe Ombredanne <[email protected]>
1 parent e2bf924 commit 15957f5

File tree

4 files changed

+36
-29
lines changed

4 files changed

+36
-29
lines changed

src/attributecode/gen.py

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,12 @@
2121
import codecs
2222
from collections import OrderedDict
2323
import logging
24-
from posixpath import basename, dirname, exists, join, normpath
25-
import sys
26-
27-
if sys.version_info[0] < 3:
28-
# Python 2
29-
import backports.csv as csv #NOQA
30-
else:
31-
# Python 3
32-
import csv #NOQA
24+
# FIXME: why posipath???
25+
from posixpath import basename
26+
from posixpath import dirname
27+
from posixpath import exists
28+
from posixpath import join
29+
from posixpath import normpath
3330

3431
from attributecode import ERROR
3532
from attributecode import CRITICAL
@@ -38,8 +35,16 @@
3835
from attributecode import model
3936
from attributecode import util
4037
from attributecode.util import add_unc
38+
from attributecode.util import python2
4139
from attributecode.util import to_posix
4240
from attributecode.util import UNC_PREFIX_POSIX
41+
from attributecode.util import unique
42+
43+
44+
if python2:
45+
import backports.csv as csv #NOQA
46+
else:
47+
import csv #NOQA
4348

4449

4550
LOG_FILENAME = 'error.log'
@@ -302,17 +307,7 @@ def generate(location, base_dir, license_notice_text_location=None,
302307
u'%(dump_loc)s '
303308
u'with error: %(emsg)s' % locals())
304309
errors.append(Error(ERROR, msg))
305-
dedup_errors = deduplicate(errors)
306-
return dedup_errors, abouts
310+
unique_errors = unique(errors)
311+
return unique_errors, abouts
307312

308313

309-
def deduplicate(sequence):
310-
"""
311-
Return a list of unique items found in sequence. Preserve the original
312-
sequence order.
313-
"""
314-
deduped = []
315-
for item in sequence:
316-
if item not in deduped:
317-
deduped.append(item)
318-
return deduped

src/attributecode/util.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,21 @@ def format_about_dict_for_json_output(about_dictionary_list):
776776
return json_formatted_list
777777

778778

779+
def unique(sequence):
780+
"""
781+
Return a list of unique items found in sequence. Preserve the original
782+
sequence order.
783+
For example:
784+
>>> unique([1, 5, 3, 5])
785+
[1, 5, 3]
786+
"""
787+
deduped = []
788+
for item in sequence:
789+
if item not in deduped:
790+
deduped.append(item)
791+
return deduped
792+
793+
779794
# FIXME: remove and replace by saneyaml
780795
from collections import Hashable
781796

tests/test_gen.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,3 @@ def test_generate_not_overwrite_original_license_file(self):
192192
u'licenses:\n'
193193
u' - file: this.LICENSE\n')
194194
assert expected == in_mem_result
195-
196-
def test_deduplicate(self):
197-
items = ['a', 'b', 'd', 'b', 'c', 'a']
198-
expected = ['a', 'b', 'd', 'c']
199-
results = gen.deduplicate(items)
200-
assert expected == results

tests/test_util.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -691,5 +691,8 @@ def test_ungroup_licenses(self):
691691
assert expected_lic_file == lic_file
692692
assert expected_lic_url == lic_url
693693

694-
def test_about_files(self):
695-
pass
694+
def test_deduplicate(self):
695+
items = ['a', 'b', 'd', 'b', 'c', 'a']
696+
expected = ['a', 'b', 'd', 'c']
697+
results = util.unique(items)
698+
assert expected == results

0 commit comments

Comments
 (0)