summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Hatch <tim@timhatch.com>2014-11-06 19:41:33 -0800
committerTim Hatch <tim@timhatch.com>2014-11-06 19:41:33 -0800
commit348b35f182e85f950e65a68d69d0f130912e8370 (patch)
treea527e8d5e04e9b5503bc88341135d1f916e29a9b
parentc863ac19ce4d591b93be8b36cb69b04422c8dc9f (diff)
downloadpygments-348b35f182e85f950e65a68d69d0f130912e8370.tar.gz
Add 'duplicates_removed' function (for *_builtins.py generation).
-rw-r--r--pygments/util.py16
-rw-r--r--tests/test_util.py16
2 files changed, 32 insertions, 0 deletions
diff --git a/pygments/util.py b/pygments/util.py
index 8376a67f..1f54c291 100644
--- a/pygments/util.py
+++ b/pygments/util.py
@@ -263,6 +263,22 @@ def format_lines(var_name, seq, raw=False, indent_level=0):
return '\n'.join(lines)
+def duplicates_removed(it, already_seen=()):
+ """
+ Returns a list with duplicates removed from the iterable `it`.
+
+ Order is preserved.
+ """
+ lst = []
+ seen = set()
+ for i in it:
+ if i in seen or i in already_seen:
+ continue
+ lst.append(i)
+ seen.add(i)
+ return lst
+
+
class Future(object):
"""Generic class to defer some work.
diff --git a/tests/test_util.py b/tests/test_util.py
index 7bf03409..7fbbba2d 100644
--- a/tests/test_util.py
+++ b/tests/test_util.py
@@ -141,3 +141,19 @@ class UtilTest(unittest.TestCase):
exec(output, d)
self.assertTrue(isinstance(d['var'], tuple))
self.assertEqual(('cat', 'dog'), d['var'])
+
+ def test_duplicates_removed_seq_types(self):
+ # tuple
+ x = util.duplicates_removed(('a', 'a', 'b'))
+ self.assertSequenceEqual(('a', 'b'), x)
+ # list
+ x = util.duplicates_removed(['a', 'a', 'b'])
+ self.assertSequenceEqual(('a', 'b'), x)
+ # iterator
+ x = util.duplicates_removed(iter(('a', 'a', 'b')))
+ self.assertSequenceEqual(('a', 'b'), x)
+
+ def test_duplicates_removed_nonconsecutive(self):
+ # keeps first
+ x = util.duplicates_removed(('a', 'b', 'a'))
+ self.assertSequenceEqual(('a', 'b'), x)