OLD | NEW |
(Empty) | |
| 1 ########################################################################### |
| 2 # |
| 3 # Support code for the 'psyco.compact' type. |
| 4 |
| 5 from __future__ import generators |
| 6 |
| 7 try: |
| 8 from UserDict import DictMixin |
| 9 except ImportError: |
| 10 |
| 11 # backported from Python 2.3 to Python 2.2 |
| 12 class DictMixin: |
| 13 # Mixin defining all dictionary methods for classes that already have |
| 14 # a minimum dictionary interface including getitem, setitem, delitem, |
| 15 # and keys. Without knowledge of the subclass constructor, the mixin |
| 16 # does not define __init__() or copy(). In addition to the four base |
| 17 # methods, progressively more efficiency comes with defining |
| 18 # __contains__(), __iter__(), and iteritems(). |
| 19 |
| 20 # second level definitions support higher levels |
| 21 def __iter__(self): |
| 22 for k in self.keys(): |
| 23 yield k |
| 24 def has_key(self, key): |
| 25 try: |
| 26 value = self[key] |
| 27 except KeyError: |
| 28 return False |
| 29 return True |
| 30 def __contains__(self, key): |
| 31 return self.has_key(key) |
| 32 |
| 33 # third level takes advantage of second level definitions |
| 34 def iteritems(self): |
| 35 for k in self: |
| 36 yield (k, self[k]) |
| 37 def iterkeys(self): |
| 38 return self.__iter__() |
| 39 |
| 40 # fourth level uses definitions from lower levels |
| 41 def itervalues(self): |
| 42 for _, v in self.iteritems(): |
| 43 yield v |
| 44 def values(self): |
| 45 return [v for _, v in self.iteritems()] |
| 46 def items(self): |
| 47 return list(self.iteritems()) |
| 48 def clear(self): |
| 49 for key in self.keys(): |
| 50 del self[key] |
| 51 def setdefault(self, key, default): |
| 52 try: |
| 53 return self[key] |
| 54 except KeyError: |
| 55 self[key] = default |
| 56 return default |
| 57 def pop(self, key, *args): |
| 58 if len(args) > 1: |
| 59 raise TypeError, "pop expected at most 2 arguments, got "\ |
| 60 + repr(1 + len(args)) |
| 61 try: |
| 62 value = self[key] |
| 63 except KeyError: |
| 64 if args: |
| 65 return args[0] |
| 66 raise |
| 67 del self[key] |
| 68 return value |
| 69 def popitem(self): |
| 70 try: |
| 71 k, v = self.iteritems().next() |
| 72 except StopIteration: |
| 73 raise KeyError, 'container is empty' |
| 74 del self[k] |
| 75 return (k, v) |
| 76 def update(self, other): |
| 77 # Make progressively weaker assumptions about "other" |
| 78 if hasattr(other, 'iteritems'): # iteritems saves memory and lookup
s |
| 79 for k, v in other.iteritems(): |
| 80 self[k] = v |
| 81 elif hasattr(other, '__iter__'): # iter saves memory |
| 82 for k in other: |
| 83 self[k] = other[k] |
| 84 else: |
| 85 for k in other.keys(): |
| 86 self[k] = other[k] |
| 87 def get(self, key, default=None): |
| 88 try: |
| 89 return self[key] |
| 90 except KeyError: |
| 91 return default |
| 92 def __repr__(self): |
| 93 return repr(dict(self.iteritems())) |
| 94 def __cmp__(self, other): |
| 95 if other is None: |
| 96 return 1 |
| 97 if isinstance(other, DictMixin): |
| 98 other = dict(other.iteritems()) |
| 99 return cmp(dict(self.iteritems()), other) |
| 100 def __len__(self): |
| 101 return len(self.keys()) |
| 102 |
| 103 ########################################################################### |
| 104 |
| 105 from _psyco import compact |
| 106 |
| 107 |
| 108 class compactdictproxy(DictMixin): |
| 109 |
| 110 def __init__(self, ko): |
| 111 self._ko = ko # compact object of which 'self' is the dict |
| 112 |
| 113 def __getitem__(self, key): |
| 114 return compact.__getslot__(self._ko, key) |
| 115 |
| 116 def __setitem__(self, key, value): |
| 117 compact.__setslot__(self._ko, key, value) |
| 118 |
| 119 def __delitem__(self, key): |
| 120 compact.__delslot__(self._ko, key) |
| 121 |
| 122 def keys(self): |
| 123 return compact.__members__.__get__(self._ko) |
| 124 |
| 125 def clear(self): |
| 126 keys = self.keys() |
| 127 keys.reverse() |
| 128 for key in keys: |
| 129 del self[key] |
| 130 |
| 131 def __repr__(self): |
| 132 keys = ', '.join(self.keys()) |
| 133 return '<compactdictproxy object {%s}>' % (keys,) |
OLD | NEW |