diff options
author | Sebastian Luther <SebastianLuther@gmx.de> | 2010-09-09 11:13:08 +0200 |
---|---|---|
committer | Zac Medico <zmedico@gentoo.org> | 2010-09-09 04:30:37 -0700 |
commit | 01172c4e623ac34ff51f8b9e8d124fd90c863a45 (patch) | |
tree | a5f48a394b0838d91b49817068b7bbaab20c2a8e | |
parent | 0073dd92b6ab1a3e2734c025f86795308ba19bf4 (diff) | |
download | portage-01172c4e623ac34ff51f8b9e8d124fd90c863a45.tar.gz portage-01172c4e623ac34ff51f8b9e8d124fd90c863a45.tar.bz2 portage-01172c4e623ac34ff51f8b9e8d124fd90c863a45.zip |
Add portage.util.unique_everseen
List unique elements, preserving order. Remember all elements ever seen.
Taken from itertools documentation.
-rw-r--r-- | pym/portage/util/__init__.py | 25 |
1 files changed, 23 insertions, 2 deletions
diff --git a/pym/portage/util/__init__.py b/pym/portage/util/__init__.py index ae1d6febb..2a2597b1a 100644 --- a/pym/portage/util/__init__.py +++ b/pym/portage/util/__init__.py @@ -8,12 +8,13 @@ __all__ = ['apply_permissions', 'apply_recursive_permissions', 'grabdict_package', 'grabfile', 'grabfile_package', 'grablines', 'initialize_logger', 'LazyItemsDict', 'map_dictlist_vals', 'new_protect_filename', 'normalize_path', 'pickle_read', 'stack_dictlist', - 'stack_dicts', 'stack_lists', 'unique_array', 'varexpand', 'write_atomic', - 'writedict', 'writemsg', 'writemsg_level', 'writemsg_stdout'] + 'stack_dicts', 'stack_lists', 'unique_array', 'unique_everseen', 'varexpand', + 'write_atomic', 'writedict', 'writemsg', 'writemsg_level', 'writemsg_stdout'] import codecs from copy import deepcopy import errno +from itertools import ifilterfalse import logging import re import shlex @@ -726,6 +727,26 @@ def unique_array(s): u.append(x) return u +def unique_everseen(iterable, key=None): + """ + List unique elements, preserving order. Remember all elements ever seen. + Taken from itertools documentation. + """ + # unique_everseen('AAAABBBCCDAABBB') --> A B C D + # unique_everseen('ABBCcAD', str.lower) --> A B C D + seen = set() + seen_add = seen.add + if key is None: + for element in ifilterfalse(seen.__contains__, iterable): + seen_add(element) + yield element + else: + for element in iterable: + k = key(element) + if k not in seen: + seen_add(k) + yield element + def apply_permissions(filename, uid=-1, gid=-1, mode=-1, mask=-1, stat_cached=None, follow_links=True): """Apply user, group, and mode bits to a file if the existing bits do not |