~ubuntu-branches/debian/wheezy/phatch/wheezy

« back to all changes in this revision

Viewing changes to phatch/core/lib/odict.py

  • Committer: Bazaar Package Importer
  • Author(s): Emilio Pozuelo Monfort
  • Date: 2008-02-13 23:48:47 UTC
  • Revision ID: james.westby@ubuntu.com-20080213234847-mp6vc4y88a9rz5qz
Tags: upstream-0.1
ImportĀ upstreamĀ versionĀ 0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
 
 
3
# Copyright (C) 2007-2008 www.stani.be
 
4
#
 
5
# This program is free software: you can redistribute it and/or modify
 
6
# it under the terms of the GNU General Public License as published by
 
7
# the Free Software Foundation, either version 3 of the License, or
 
8
# (at your option) any later version.
 
9
#
 
10
# This program is distributed in the hope that it will be useful,
 
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
# GNU General Public License for more details.
 
14
 
15
# You should have received a copy of the GNU General Public License
 
16
# along with this program.  If not, see http://www.gnu.org/licenses/
 
17
 
 
18
class odict(dict):
 
19
    """This one uses generators in places where it can, and plays nicely 
 
20
    with deepcopy."""
 
21
 
 
22
    def __init__(self, d={}):
 
23
        self._keys = d.keys()
 
24
        dict.__init__(self, d)
 
25
 
 
26
    def __delitem__(self, key):
 
27
        dict.__delitem__(self, key)
 
28
        self._keys.remove(key)
 
29
 
 
30
    def __setitem__(self, key, item):
 
31
        dict.__setitem__(self, key, item)
 
32
        # a peculiar sharp edge from copy.deepcopy
 
33
        # we'll have our set item called without __init__
 
34
        if not hasattr(self, '_keys'):
 
35
            self._keys = [key,]
 
36
        if key not in self._keys:
 
37
            self._keys.append(key)
 
38
 
 
39
    def clear(self):
 
40
        dict.clear(self)
 
41
        self._keys = []
 
42
 
 
43
    def items(self):
 
44
        for i in self._keys:
 
45
            yield i, self[i]
 
46
 
 
47
    def keys(self):
 
48
        return self._keys
 
49
 
 
50
    def popitem(self):
 
51
        if len(self._keys) == 0:
 
52
            raise KeyError('dictionary is empty')
 
53
        else:
 
54
            key = self._keys[-1]
 
55
            val = self[key]
 
56
            del self[key]
 
57
            return key, val
 
58
 
 
59
    def setdefault(self, key, failobj = None):
 
60
        dict.setdefault(self, key, failobj)
 
61
        if key not in self._keys:
 
62
            self._keys.append(key)
 
63
 
 
64
    def update(self, d):
 
65
        for key in d.keys():
 
66
            if not self.has_key(key):
 
67
                self._keys.append(key)
 
68
        dict.update(self, d)
 
69
 
 
70
    def values(self):
 
71
        for i in self._keys:
 
72
            yield self[i]
 
73
 
 
74
    def move(self, key, index):
 
75
 
 
76
        """ Move the specified to key to *before* the specified index. """
 
77
 
 
78
        try:
 
79
            cur = self._keys.index(key)
 
80
        except ValueError:
 
81
            raise KeyError(key)
 
82
        self._keys.insert(index, key)
 
83
        # this may have shifted the position of cur, if it is after index
 
84
        if cur >= index: cur = cur + 1
 
85
        del self._keys[cur]
 
86
 
 
87
    def index(self, key):
 
88
        if not self.has_key(key):
 
89
            raise KeyError(key)
 
90
        return self._keys.index(key)
 
91
    
 
92
if __name__ == '__main__':
 
93
    od = odict()
 
 
b'\\ No newline at end of file'