*** empty log message ***
[pli.git] / pli / pattern / mixin / mapping.py
blob78aadf1d02b5ac840637271d197ac08bf9a6b045
1 #=======================================================================
3 __version__ = '''0.1.01'''
4 __sub_version__ = '''20040820025303'''
5 __copyright__ = '''(c) Alex A. Naanou 2003'''
8 #-----------------------------------------------------------------------
10 _marker = 'This is a marker string... (WARNING: do not use this in any way!)'
13 #-----------------------------------------------------------------------
14 class AbstractMapping(object):
15 '''
16 '''
17 pass
20 #-----------------------------------------------------------------------
21 #--------------------------------------------------------BasicMapping---
22 class BasicMapping(AbstractMapping):
23 '''
24 this defines the basic mapping interface.
25 '''
26 # root methods:
27 def __getitem__(self, key):
28 '''
29 '''
30 #raise NotImplementedError, 'root method __getitem__ not implemented.'
31 return super(BasicMapping, self).__getitem__(key)
32 def __setitem__(self, key, value):
33 '''
34 '''
35 #raise NotImplementedError, 'root method __setitem__ not implemented.'
36 return super(BasicMapping, self).__setitem__(key, value)
37 def __delitem__(self, key):
38 '''
39 '''
40 #raise NotImplementedError, 'root method __delitem__ not implemented.'
41 return super(BasicMapping, self).__delitem__(key)
42 def __iter__(self):
43 '''
44 '''
45 #raise NotImplementedError, 'root method __iter__ not implemented.'
46 return super(BasicMapping, self).__iter__()
48 # 2nd generation methods:
49 def __contains__(self, key):
50 '''
51 '''
52 try:
53 self[key]
54 return True
55 except KeyError:
56 return False
58 def __len__(self):
59 '''
60 '''
61 return len([k for k in self])
63 ## # do we need these here???
64 ## def __reduce__(self):
65 ## '''
66 ## '''
67 ## pass
68 ## def __reduce_ex__(self):
69 ## '''
70 ## '''
71 ## pass
73 ## def __repr__(self):
74 ## '''
75 ## '''
76 ## pass
77 ## def __str__(self):
78 ## '''
79 ## '''
80 ## pass
82 ## def __hash__(self):
83 ## '''
84 ## '''
85 ## pass
88 #---------------------------------------------------BasicMappingProxy---
89 # NOTE: this is slower than a direct proxy...
90 class BasicMappingProxy(AbstractMapping):
91 '''
92 '''
93 __source_attr__ = '__source__'
95 #__source__ = {}
97 # root methods:
98 def __getitem__(self, key):
99 '''
101 return getattr(self, self.__source_attr__)[key]
102 def __setitem__(self, key, value):
105 getattr(self, self.__source_attr__)[key] = value
106 def __delitem__(self, key):
109 del getattr(self, self.__source_attr__)[key]
110 def __iter__(self):
113 return getattr(self, self.__source_attr__).__iter__()
115 # 2nd generation methods:
116 def __contains__(self, key):
119 return key in getattr(self, self.__source_attr__)
121 def __len__(self):
124 return len(getattr(self, self.__source_attr__))
127 #---------------------------------------------------ComparableMapping---
128 ##!!! REVIZE !!!##
129 class ComparableMapping(BasicMapping):
131 this defines the basic comparability interface for the basic mapping.
133 # 2nd generation methods:
134 def __cmp__(self, other):
137 ##!!! ugly !!!##
138 return cmp(dict([ (k, self[k]) for k in self ]), dict([ (k, other[k]) for k in other ]))
140 def __eq__(self, other):
143 return cmp(self, other) == 0
144 def __ne__(self, other):
147 return not self == other
149 ##!!!
150 ## def __ge__(self, other):
151 ## '''
152 ## '''
153 ## pass
154 ## def __gt__(self, other):
155 ## '''
156 ## '''
157 ## pass
158 ## def __le__(self, other):
159 ## '''
160 ## '''
161 ## pass
162 ## def __lt__(self, other):
163 ## '''
164 ## '''
165 ## pass
168 #------------------------------------------MappingWithIteratorMethods---
169 class MappingWithIteratorMethods(BasicMapping):
171 this defines the mapping iterators.
173 # 2nd generation methods:
174 def iterkeys(self):
177 for k in self:
178 yield k
179 def itervalues(self):
182 for k in self:
183 yield self[k]
184 def iteritems(self):
187 for k in self:
188 yield k, self[k]
191 #-----------------------------------------MappingWithListConstructors---
192 class MappingWithListConstructors(BasicMapping):
194 this defines the mapping list constructors.
196 # 2nd generation methods:
197 def keys(self):
200 return list(self)
201 def values(self):
204 return [ self[k] for k in self ]
205 def items(self):
208 return [ (k, self[k]) for k in self ]
211 #-----------------------------------------------MappingWithGetMethods---
212 class MappingWithGetMethods(BasicMapping):
214 this defines the get and setdefault methods.
216 # 2nd generation methods:
217 def get(self, key, default=_marker):
220 if key in self:
221 return self[key]
222 elif default == _marker:
223 raise KeyError, (len(self) == 0 and 'get(): dictionary is empty' or '%s' % key)
224 return default
225 def setdefault(self, key, default=None):
228 if key not in self:
229 self[key] = default
230 return self.get(key, default)
233 #--------------------------------------------MappingWithModifyMethods---
234 class MappingWithModifyMethods(BasicMapping):
236 this defines the mapping in-place modification methods.
238 # 2nd generation methods:
239 def pop(self, key, default=_marker):
242 if key in self:
243 o = self[key]
244 del self[key]
245 return o
246 elif default == _marker:
247 raise KeyError, (len(self) == 0 and 'pop(): dictionary is empty' or '%s' % key)
248 return default
249 def popitem(self):
252 # fake loop!! :))
253 for k in self:
254 return k, self[k]
255 raise KeyError, 'popitem(): dictionary is empty'
257 def clear(self):
260 for k in self:
261 del self[k]
262 def update(self, data):
265 for k in data:
266 self[k] = data[k]
269 #--------------------------------------------MappingWithModifyMethods---
270 class MappingWithMappingConstructors(BasicMapping):
272 this defines mapping methods that create new mappings.
274 # 2nd generation methods:
275 def copy(self):
278 o = self.__class__()
279 for k in self:
280 o[k] = self[k]
281 return o
282 def fromkeys(self, keys, value=None):
285 o = self.__class__()
286 for k in keys:
287 o[k] = value
288 return o
291 #----------------------------------------------MappingWithTestMethods---
292 class MappingWithTestMethods(BasicMapping):
294 this defines test/predicate methods.
296 # 2nd generation methods:
297 def has_key(self, key):
300 return key in self
303 #--------------------------------------------------MappingWithMethods---
304 # Q: should this be split into finer-grained classes???
305 class MappingWithMethods(MappingWithIteratorMethods,
306 MappingWithListConstructors,
307 MappingWithGetMethods,
308 MappingWithModifyMethods,
309 MappingWithMappingConstructors,
310 MappingWithTestMethods):
312 this defines the mapping interface methods (as in dict).
314 pass
317 #-------------------------------------------------------------Mapping---
318 class Mapping(MappingWithMethods, ComparableMapping):
320 this defines the basic complete mapping.
322 pass
325 #------------------------------------------------------------DictLike---
326 class DictLike(Mapping):
328 this defines a dict like.
330 def __init__(self, *pargs, **nargs):
333 parg_len = len(pargs)
334 if parg_len not in (0, 1):
335 raise TypeError, '%s expected at most 1 arguments (got %s).' % (self.__class__, len(pargs))
336 elif parg_len == 1:
337 # seq...
338 seq = pargs[0]
339 try:
340 for i, o in enumerate(seq):
341 e = list(o)
342 if len(e) != 2:
343 raise TypeError, 'length of element %s must be 2 (got: %s).' % (i, len(e))
344 self[e[0]] = e[1]
345 except:
346 raise TypeError, 'can\'t convert element #%s of sequence to list.' % i
347 if len(nargs) > 0:
348 # mapping...
349 for k in nargs:
350 self[k] = nargs[k]
354 #=======================================================================
355 # vim:set ts=4 sw=4 nowrap :