summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2015-12-06 12:32:27 +0000
committerMichael Williamson <mike@zwobble.org>2015-12-06 12:32:59 +0000
commite315a8360b5a312b9b692408473d942de94557d1 (patch)
tree2e71949fb3ad7d33b5a9e1cb60655e0d47246654
parent323aaa3aa2bcc03c9c46b173f33b9cdfac205e74 (diff)
Ensure Result.messages is always unique
-rw-r--r--NEWS4
-rw-r--r--mammoth/lists.py9
-rw-r--r--mammoth/results.py4
-rw-r--r--scripts/mammoth14
-rw-r--r--tests/lists_tests.py13
5 files changed, 31 insertions, 13 deletions
diff --git a/NEWS b/NEWS
index 795fa30..566c6bc 100644
--- a/NEWS
+++ b/NEWS
@@ -1,3 +1,7 @@
+# 0.3.25
+
+* Remove duplicate messages from results.
+
# 0.3.24
* Continue with warning if external images cannot be found.
diff --git a/mammoth/lists.py b/mammoth/lists.py
index cf39b99..6e39ae0 100644
--- a/mammoth/lists.py
+++ b/mammoth/lists.py
@@ -14,6 +14,15 @@ def _collect(values, result):
elif value is not None:
result.append(value)
+def unique(values):
+ output = []
+ seen = set()
+ for value in values:
+ if value not in seen:
+ seen.add(value)
+ output.append(value)
+ return output
+
if sys.version_info[0] == 2:
map = map
diff --git a/mammoth/results.py b/mammoth/results.py
index 9112296..59c3a42 100644
--- a/mammoth/results.py
+++ b/mammoth/results.py
@@ -1,10 +1,12 @@
import collections
+from .lists import unique
+
class Result(object):
def __init__(self, value, messages):
self.value = value
- self.messages = messages
+ self.messages = unique(messages)
def map(self, func):
return Result(func(self.value), self.messages)
diff --git a/scripts/mammoth b/scripts/mammoth
index be7ad10..f4865fb 100644
--- a/scripts/mammoth
+++ b/scripts/mammoth
@@ -34,24 +34,14 @@ def main():
convert_image=convert_image,
output_format=args.output_format,
)
- for message in _unique([message.message for message in result.messages]):
- sys.stderr.write(message)
+ for message in result.messages:
+ sys.stderr.write(message.message)
sys.stderr.write("\n")
with _open_output(output_path) as output:
output.write(result.value)
-def _unique(values):
- output = []
- seen = set()
- for value in values:
- if value not in seen:
- seen.add(value)
- output.append(value)
- return output
-
-
class ImageWriter(object):
def __init__(self, output_dir):
self._output_dir = output_dir
diff --git a/tests/lists_tests.py b/tests/lists_tests.py
new file mode 100644
index 0000000..49d5331
--- /dev/null
+++ b/tests/lists_tests.py
@@ -0,0 +1,13 @@
+from nose.tools import istest, assert_equal
+
+from mammoth.lists import unique
+
+
+@istest
+def unique_of_empty_list_is_empty_list():
+ assert_equal([], unique([]))
+
+
+@istest
+def unique_removes_duplicates_while_preserving_order():
+ assert_equal(["apple", "banana"], unique(["apple", "banana", "apple"]))