root/galaxy-central/lib/galaxy/util/odict.py

リビジョン 2, 2.1 KB (コミッタ: hatakeyama, 14 年 前)

import galaxy-central

行番号 
1"""
2Ordered dictionary implementation.
3"""
4
5from UserDict import UserDict
6
7class odict(UserDict):
8    """
9    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/107747
10
11    This dictionary class extends UserDict to record the order in which items are
12    added. Calling keys(), values(), items(), etc. will return results in this
13    order.
14
15    I've added iterkeys, itervalues, iteritems
16    """
17    def __init__(self, dict = None):
18        self._keys = []
19        UserDict.__init__(self, dict)
20
21    def __delitem__(self, key):
22        UserDict.__delitem__(self, key)
23        self._keys.remove(key)
24
25    def __setitem__(self, key, item):
26        UserDict.__setitem__(self, key, item)
27        if key not in self._keys: self._keys.append(key)
28
29    def clear(self):
30        UserDict.clear(self)
31        self._keys = []
32
33    def copy(self):
34        new = odict()
35        new.update( self )
36        return new
37
38    def items(self):
39        return zip(self._keys, self.values())
40
41    def keys(self):
42        return self._keys[:]
43
44    def popitem(self):
45        try:
46            key = self._keys[-1]
47        except IndexError:
48            raise KeyError('dictionary is empty')
49
50        val = self[key]
51        del self[key]
52
53        return (key, val)
54
55    def setdefault(self, key, failobj = None):
56        if key not in self._keys: self._keys.append(key)
57        return UserDict.setdefault(self, key, failobj)
58
59    def update(self, dict):
60        UserDict.update(self, dict)
61        for key in dict.keys():
62            if key not in self._keys: self._keys.append(key)
63
64    def update(self, dict):
65        for (key,val) in dict.items():
66            self.__setitem__(key,val)
67
68    def values(self):
69        return map(self.get, self._keys)
70
71    def iterkeys(self):
72        return iter( self._keys )
73
74    def itervalues(self):
75        for key in self._keys:
76            yield self.get(key)
77
78    def iteritems(self):
79        for key in self._keys:
80            yield key, self.get(key)       
81
82    def __iter__( self ):
83        for key in self._keys:
84            yield key
85   
86    def reverse( self ):
87        self._keys.reverse()
88
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。