From 0a993bf71d0e4ff87dd85b7226e1662c70a53e08 Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Tue, 8 Dec 2009 21:06:39 +0000 Subject: Add OrderedDict class. svn path=/main/trunk/; revision=14967 --- pym/portage/cache/mappings.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/pym/portage/cache/mappings.py b/pym/portage/cache/mappings.py index ed6e19ddd..2e24a7a8a 100644 --- a/pym/portage/cache/mappings.py +++ b/pym/portage/cache/mappings.py @@ -188,6 +188,32 @@ class UserDict(MutableMapping): if sys.hexversion >= 0x3000000: keys = __iter__ +class OrderedDict(UserDict): + + def __init__(self, *args, **kwargs): + self._order = [] + UserDict.__init__(self, *args, **kwargs) + + def __iter__(self): + return iter(self._order) + + def __setitem__(self, key, item): + if key in self: + self._order.remove(key) + UserDict.__setitem__(self, key, item) + self._order.append(key) + + def __delitem__(self, key): + UserDict.__delitem__(self, key) + self._order.remove(key) + + def clear(self): + UserDict.clear(self) + del self._order[:] + + if sys.hexversion >= 0x3000000: + keys = __iter__ + class ProtectedDict(MutableMapping): """ given an initial dict, this wraps that dict storing changes in a secondary dict, protecting -- cgit v1.2.3-1-g7c22