1 """
2 A backport of UserDict.DictMixin for pre-python-2.4
3 """
4 __all__ = ['DictMixin']
5
6 try:
7 from UserDict import DictMixin
8 except ImportError:
10
11
12
13
14
15
16
17
19 for k in self.keys():
20 yield k
22 try:
23 value = self[key]
24 except KeyError:
25 return False
26 return True
29
30
32 for k in self:
33 yield (k, self[k])
36
37
46 for key in self.keys():
47 del self[key]
49 try:
50 return self[key]
51 except KeyError:
52 self[key] = default
53 return default
54 - def pop(self, key, *args):
55 if len(args) > 1:
56 raise TypeError, "pop expected at most 2 arguments, got "\
57 + repr(1 + len(args))
58 try:
59 value = self[key]
60 except KeyError:
61 if args:
62 return args[0]
63 raise
64 del self[key]
65 return value
67 try:
68 k, v = self.iteritems().next()
69 except StopIteration:
70 raise KeyError, 'container is empty'
71 del self[k]
72 return (k, v)
73 - def update(self, other=None, **kwargs):
74
75 if other is None:
76 pass
77 elif hasattr(other, 'iteritems'):
78 for k, v in other.iteritems():
79 self[k] = v
80 elif hasattr(other, 'keys'):
81 for k in other.keys():
82 self[k] = other[k]
83 else:
84 for k, v in other:
85 self[k] = v
86 if kwargs:
87 self.update(kwargs)
88 - def get(self, key, default=None):
89 try:
90 return self[key]
91 except KeyError:
92 return default
96 if other is None:
97 return 1
98 if isinstance(other, DictMixin):
99 other = dict(other.iteritems())
100 return cmp(dict(self.iteritems()), other)
102 return len(self.keys())
103