From 765f6c1ecf2a06720ba32ff26e8bb3c5e6d08a9d Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Thu, 10 Dec 2009 01:03:20 +0000 Subject: [PATCH] Add OrderedDict class. (trunk r14967) svn path=/main/branches/2.1.7/; revision=15001 --- 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 -- 2.26.2