From: Zac Medico Date: Tue, 8 Dec 2009 21:06:39 +0000 (-0000) Subject: Add OrderedDict class. X-Git-Tag: v2.2_rc57~14 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=0a993bf71d0e4ff87dd85b7226e1662c70a53e08;p=portage.git Add OrderedDict class. svn path=/main/trunk/; revision=14967 --- 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