~romaimperator/keryx/devel

« back to all changes in this revision

Viewing changes to minideblib/OrderedDict.py

  • Committer: mac9416
  • Date: 2009-08-14 23:24:01 UTC
  • mto: This revision was merged to the branch mainline in revision 5.
  • Revision ID: mac9416@keryxproject.org-20090814232401-5udsmhz96in0lkdz
Added PackageListManager to dpkg.py

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# OrderedDict.py
2
 
#
3
 
# This class functions almost exactly like UserDict.  However, when using
4
 
# the sequence methods, it returns items in the same order in which they
5
 
# were added, instead of some random order.
6
 
#
7
 
# Copyright 2001 Adam Heath <doogie@debian.org>
8
 
#
9
 
# This file is free software; you can redistribute it and/or modify it
10
 
# under the terms of the GNU General Public License as published by
11
 
# the Free Software Foundation; either version 2 of the License, or
12
 
# (at your option) any later version.
13
 
#
14
 
# This program is distributed in the hope that it will be useful, but
15
 
# WITHOUT ANY WARRANTY; without even the implied warranty of
16
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 
# General Public License for more details.
18
 
#
19
 
# You should have received a copy of the GNU General Public License
20
 
# along with this program; if not, write to the Free Software
21
 
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22
 
 
23
 
from UserDict import UserDict
24
 
 
25
 
class OrderedDict(UserDict):
26
 
    __order=[]
27
 
 
28
 
    def __init__(self, dict=None):
29
 
        UserDict.__init__(self)
30
 
        self.__order=[]
31
 
        if dict is not None and dict.__class__ is not None:
32
 
            self.update(dict)
33
 
 
34
 
    def __cmp__(self, dict):
35
 
        if isinstance(dict, OrderedDict):
36
 
            ret=cmp(self.__order, dict.__order)
37
 
            if not ret:
38
 
                ret=UserDict.__cmp__(self, dict)
39
 
            return ret
40
 
        else:
41
 
            return UserDict.__cmp__(self, dict)
42
 
 
43
 
    def __setitem__(self, key, value):
44
 
        if not self.has_key(key):
45
 
            self.__order.append(key)
46
 
        UserDict.__setitem__(self, key, value)
47
 
 
48
 
    def __delitem__(self, key):
49
 
        if self.has_key(key):
50
 
            del self.__order[self.__order.index(key)]
51
 
        UserDict.__delitem__(self, key)
52
 
 
53
 
    def clear(self):
54
 
        self.__order=[]
55
 
        UserDict.clear(self)
56
 
 
57
 
    def copy(self):
58
 
        if self.__class__ is OrderedDict:
59
 
            return OrderedDict(self)
60
 
        import copy
61
 
        return copy.copy(self)
62
 
 
63
 
    def keys(self):
64
 
        return self.__order
65
 
 
66
 
    def items(self):
67
 
        return map(lambda x, self=self: (x, self.__getitem__(x)), self.__order)
68
 
 
69
 
    def values(self):
70
 
        return map(lambda x, self=self: self.__getitem__(x), self.__order)
71
 
 
72
 
    def update(self, dict):
73
 
        for k, v in dict.items():
74
 
            self.__setitem__(k, v)
75
 
 
76
 
# vim:ts=4:sw=4:et: