1 #=======================================================================
3 __version__
= '''0.1.03'''
4 __sub_version__
= '''20080127042744'''
5 __copyright__
= '''(c) Alex A. Naanou 2003'''
8 #-----------------------------------------------------------------------
10 _marker
= 'This is a marker string... (WARNING: do not use this in any way!)'
12 # NOTE: none of the defined here classes will contaminate the namespace
13 # of the object that derrives from any one or combination of
16 #-----------------------------------------------------------------------
17 #-----------------------------------------------------------ismapping---
21 return isinstance(obj
, (AbstractMapping
, dict))
24 #-----------------------------------------------------------------------
25 #-----------------------------------------------------AbstractMapping---
26 class AbstractMapping(object):
32 #-----------------------------------------------------------------------
33 #--------------------------------------------------------BasicMapping---
34 class BasicMapping(AbstractMapping
):
36 this defines the basic mapping interface.
39 def __getitem__(self
, key
):
42 #raise NotImplementedError, 'root method __getitem__ not implemented.'
43 return super(BasicMapping
, self
).__getitem
__(key
)
44 def __setitem__(self
, key
, value
):
47 #raise NotImplementedError, 'root method __setitem__ not implemented.'
48 return super(BasicMapping
, self
).__setitem
__(key
, value
)
49 def __delitem__(self
, key
):
52 #raise NotImplementedError, 'root method __delitem__ not implemented.'
53 return super(BasicMapping
, self
).__delitem
__(key
)
57 #raise NotImplementedError, 'root method __iter__ not implemented.'
58 return super(BasicMapping
, self
).__iter
__()
60 # 2nd generation methods:
61 def __contains__(self
, key
):
73 return len([k
for k
in self
])
75 ## # do we need these here???
76 ## def __reduce__(self):
80 ## def __reduce_ex__(self):
85 ## def __repr__(self):
94 ## def __hash__(self):
100 #---------------------------------------------------BasicMappingProxy---
101 # NOTE: this is slower than a direct proxy...
102 class BasicMappingProxy(AbstractMapping
):
105 __source_attr__
= '__source__'
110 def __getitem__(self
, key
):
113 return getattr(self
, self
.__source
_attr
__)[key
]
114 def __setitem__(self
, key
, value
):
117 getattr(self
, self
.__source
_attr
__)[key
] = value
118 def __delitem__(self
, key
):
121 del getattr(self
, self
.__source
_attr
__)[key
]
125 return getattr(self
, self
.__source
_attr
__).__iter
__()
127 # 2nd generation methods:
128 def __contains__(self
, key
):
131 return key
in getattr(self
, self
.__source
_attr
__)
136 return len(getattr(self
, self
.__source
_attr
__))
139 #---------------------------------------------------ComparableMapping---
140 class ComparableMapping(BasicMapping
):
142 this defines the basic comparability interface for the basic mapping.
144 # 2nd generation methods:
145 ## def __cmp__(self, other):
149 ## if hasattr(other, '__iter__') and hasattr(other, '__getitem__'):
150 ## return cmp(dict([ (k, self[k]) for k in self ]), dict([ (k, other[k]) for k in other ]))
153 def __eq__(self
, other
):
156 if hasattr(other
, '__iter__') and hasattr(other
, '__getitem__'):
157 return dict([ (k
, self
[k
]) for k
in self
]) == dict([ (k
, other
[k
]) for k
in other
])
159 def __ne__(self
, other
):
162 return not self
== other
164 def __gt__(self
, other
):
167 if hasattr(other
, '__iter__') and hasattr(other
, '__getitem__'):
168 return dict([ (k
, self
[k
]) for k
in self
]) > dict([ (k
, other
[k
]) for k
in other
])
170 def __lt__(self
, other
):
173 if hasattr(other
, '__iter__') and hasattr(other
, '__getitem__'):
174 return dict([ (k
, self
[k
]) for k
in self
]) < dict([ (k
, other
[k
]) for k
in other
])
176 def __ge__(self
, other
):
179 if hasattr(other
, '__iter__') and hasattr(other
, '__getitem__'):
180 return dict([ (k
, self
[k
]) for k
in self
]) >= dict([ (k
, other
[k
]) for k
in other
])
182 def __le__(self
, other
):
185 if hasattr(other
, '__iter__') and hasattr(other
, '__getitem__'):
186 return dict([ (k
, self
[k
]) for k
in self
]) <= dict([ (k
, other
[k
]) for k
in other
])
190 #------------------------------------------MappingWithIteratorMethods---
191 class MappingWithIteratorMethods(BasicMapping
):
193 this defines the mapping iterators.
195 # 2nd generation methods:
201 def itervalues(self
):
213 #-----------------------------------------MappingWithListConstructors---
214 class MappingWithListConstructors(BasicMapping
):
216 this defines the mapping list constructors.
218 # 2nd generation methods:
226 return [ self
[k
] for k
in self
]
230 return [ (k
, self
[k
]) for k
in self
]
233 #-----------------------------------------------MappingWithGetMethods---
234 class MappingWithGetMethods(BasicMapping
):
236 this defines the get and setdefault methods.
238 # 2nd generation methods:
239 def get(self
, key
, default
=_marker
):
244 elif default
== _marker
:
245 raise KeyError, (len(self
) == 0 and 'get(): dictionary is empty' or '%s' % key
)
247 def setdefault(self
, key
, default
=None):
252 return self
.get(key
, default
)
255 #--------------------------------------------MappingWithModifyMethods---
256 class MappingWithModifyMethods(BasicMapping
):
258 this defines the mapping in-place modification methods.
260 # 2nd generation methods:
261 def pop(self
, key
, default
=_marker
):
268 elif default
== _marker
:
269 raise KeyError, (len(self
) == 0 and 'pop(): dictionary is empty' or '%s' % key
)
277 raise KeyError, 'popitem(): dictionary is empty'
284 def update(self
, data
):
291 #--------------------------------------------MappingWithModifyMethods---
292 class MappingWithMappingConstructors(BasicMapping
):
294 this defines mapping methods that create new mappings.
296 # 2nd generation methods:
304 def fromkeys(self
, keys
, value
=None):
313 #----------------------------------------------MappingWithTestMethods---
314 class MappingWithTestMethods(BasicMapping
):
316 this defines test/predicate methods.
318 # 2nd generation methods:
319 def has_key(self
, key
):
325 #--------------------------------------------------MappingWithMethods---
326 # Q: should this be split into finer-grained classes???
327 class MappingWithMethods(MappingWithIteratorMethods
,
328 MappingWithListConstructors
,
329 MappingWithGetMethods
,
330 MappingWithModifyMethods
,
331 MappingWithMappingConstructors
,
332 MappingWithTestMethods
):
334 this defines the mapping interface methods (as in dict).
339 #-------------------------------------------------------------Mapping---
340 class Mapping(MappingWithMethods
, ComparableMapping
):
342 this defines the basic complete mapping.
347 #------------------------------------------------------------DictLike---
348 class DictLike(Mapping
):
350 this defines a dict like.
352 def __init__(self
, *pargs
, **nargs
):
355 parg_len
= len(pargs
)
356 if parg_len
not in (0, 1):
357 raise TypeError, '%s expected at most 1 arguments (got %s).' % (self
.__class
__, len(pargs
))
362 for i
, o
in enumerate(seq
):
365 raise TypeError, 'length of element %s must be 2 (got: %s).' % (i
, len(e
))
368 raise TypeError, 'can\'t convert element #%s of sequence to list.' % i
376 #=======================================================================
377 # vim:set ts=4 sw=4 nowrap :