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):
20 #-----------------------------------------------------------------------
21 #--------------------------------------------------------BasicMapping---
22 class BasicMapping(AbstractMapping
):
24 this defines the basic mapping interface.
27 def __getitem__(self
, key
):
30 #raise NotImplementedError, 'root method __getitem__ not implemented.'
31 return super(BasicMapping
, self
).__getitem
__(key
)
32 def __setitem__(self
, key
, value
):
35 #raise NotImplementedError, 'root method __setitem__ not implemented.'
36 return super(BasicMapping
, self
).__setitem
__(key
, value
)
37 def __delitem__(self
, key
):
40 #raise NotImplementedError, 'root method __delitem__ not implemented.'
41 return super(BasicMapping
, self
).__delitem
__(key
)
45 #raise NotImplementedError, 'root method __iter__ not implemented.'
46 return super(BasicMapping
, self
).__iter
__()
48 # 2nd generation methods:
49 def __contains__(self
, key
):
61 return len([k
for k
in self
])
63 ## # do we need these here???
64 ## def __reduce__(self):
68 ## def __reduce_ex__(self):
73 ## def __repr__(self):
82 ## def __hash__(self):
88 #---------------------------------------------------BasicMappingProxy---
89 # NOTE: this is slower than a direct proxy...
90 class BasicMappingProxy(AbstractMapping
):
93 __source_attr__
= '__source__'
98 def __getitem__(self
, key
):
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
]
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
__)
124 return len(getattr(self
, self
.__source
_attr
__))
127 #---------------------------------------------------ComparableMapping---
129 class ComparableMapping(BasicMapping
):
131 this defines the basic comparability interface for the basic mapping.
133 # 2nd generation methods:
134 def __cmp__(self
, other
):
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
150 ## def __ge__(self, other):
154 ## def __gt__(self, other):
158 ## def __le__(self, other):
162 ## def __lt__(self, other):
168 #------------------------------------------MappingWithIteratorMethods---
169 class MappingWithIteratorMethods(BasicMapping
):
171 this defines the mapping iterators.
173 # 2nd generation methods:
179 def itervalues(self
):
191 #-----------------------------------------MappingWithListConstructors---
192 class MappingWithListConstructors(BasicMapping
):
194 this defines the mapping list constructors.
196 # 2nd generation methods:
204 return [ self
[k
] for k
in 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
):
222 elif default
== _marker
:
223 raise KeyError, (len(self
) == 0 and 'get(): dictionary is empty' or '%s' % key
)
225 def setdefault(self
, key
, default
=None):
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
):
246 elif default
== _marker
:
247 raise KeyError, (len(self
) == 0 and 'pop(): dictionary is empty' or '%s' % key
)
255 raise KeyError, 'popitem(): dictionary is empty'
262 def update(self
, data
):
269 #--------------------------------------------MappingWithModifyMethods---
270 class MappingWithMappingConstructors(BasicMapping
):
272 this defines mapping methods that create new mappings.
274 # 2nd generation methods:
282 def fromkeys(self
, keys
, value
=None):
291 #----------------------------------------------MappingWithTestMethods---
292 class MappingWithTestMethods(BasicMapping
):
294 this defines test/predicate methods.
296 # 2nd generation methods:
297 def has_key(self
, key
):
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).
317 #-------------------------------------------------------------Mapping---
318 class Mapping(MappingWithMethods
, ComparableMapping
):
320 this defines the basic complete mapping.
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
))
340 for i
, o
in enumerate(seq
):
343 raise TypeError, 'length of element %s must be 2 (got: %s).' % (i
, len(e
))
346 raise TypeError, 'can\'t convert element #%s of sequence to list.' % i
354 #=======================================================================
355 # vim:set ts=4 sw=4 nowrap :