1 #=======================================================================
3 __version__
= '''0.1.07'''
4 __sub_version__
= '''20041116002755'''
5 __copyright__
= '''(c) Alex A. Naanou 2003'''
8 this module defines a number of utilities and objects to assist advanced
9 usage of standard python types.
12 #-----------------------------------------------------------------------
14 import pli
.pattern
.mixin
.mapping
as mapping
17 #-----------------------------------------------------------------------
18 # TODO create a logic proxy, with adapters....
19 # UNION(*p), INTERSECTION(*n), ...
21 #------------------------------------------------------------_Compare---
22 class _Compare(object):
25 def __init__(self
, eq
):
27 def __cmp__(self
, other
):
29 def __eq__(self
, other
):
31 def __ne__(self
, other
):
33 def __gt__(self
, other
):
35 def __ge__(self
, other
):
37 def __lt__(self
, other
):
39 def __le__(self
, other
):
42 #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
43 # this will compare to any value as equel (almost oposite to None)
44 Any
= ANY
= _Compare(0)
46 #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
47 # this is bigger than any value...
50 #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
51 # this is smaller than any value...
52 MINIMUM
= _Compare(-1)
56 #-----------------------------------------------------------------------
57 # this is to define basic pattern combination mathematics.
59 # Pab = Pa | Pb -- Pab is a pattern that will match either Pa
70 class Pattern(object):
74 def __eq__(self
, other
):
78 def __ne__(self
, other
):
82 def __gt__(self
, other
):
86 def __ge__(self
, other
):
90 def __lt__(self
, other
):
94 def __le__(self
, other
):
102 #--------------------------------------------------------------oftype---
103 class oftype(Pattern
):
105 this will create an object that can be used as a predicate to test type,
106 and it will copare True to objects of that type.
108 def __init__(self
, *types
, **n
):
113 self
.__doc
__ = n
['doc']
114 def __call__(self
, other
):
116 test if the the other object object is of type.
118 return isinstance(other
, self
._types
) is True
120 def __ne__(self
, other
):
121 return not isinstance(other
, self
._types
) is True
122 def __gt__(self
, other
):
124 def __ge__(self
, other
):
126 def __lt__(self
, other
):
128 def __le__(self
, other
):
131 #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
132 # simple type predicates...
134 isfloat
= oftype(float)
135 iscomplex
= oftype(complex)
138 isunicode
= oftype(unicode)
140 islist
= oftype(list)
141 istuple
= oftype(tuple)
142 isdict
= oftype(dict)
144 #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
145 # general type groups predicates...
146 isnumber
= oftype(int, float, complex)
147 isstring
= oftype(str, unicode)
148 issequence
= oftype(list, tuple)
150 isiterabletype
= oftype(str, unicode, list, tuple, dict)
154 #-----------------------------------------------------------------------
155 #-------------------------------------------------------dictcopyunite---
156 def dictcopyunite(*members
):
165 #-----------------------------------------------------------DictUnion---
166 ##class DictUnion(mapping.MappingWithMethods):
167 class DictUnion(mapping
.Mapping
):
169 this is a dict like object, that acts as a union of its members but
170 without modifieng its members in any way.
172 this is similar to a sequential update of all members, but retains
173 the container information for each item, and does not have the
174 overhead of creating a new resulting dict.
176 NOTE: because of the nature of dicts, the later added members (unite)
177 have higher priority than the former.
178 NOTE: the members added in one call (be it __init__, unite or tailunite)
179 have descending priority -- first highest last lowest.
180 NOTE: due to ints nature this object is *live*, e.g. it emidiatly
181 reflects all modifications to its members as they are modified.
185 def __init__(self
, *members
):
188 members
= list(members
)
190 self
._members
= tuple(members
)
191 def __getitem__(self
, name
):
194 for m
in self
._members
:
197 raise KeyError, 'key "%s" is not present in any of the members.' % name
198 def __setitem__(self
, name
, value
):
203 raise TypeError, 'can\'t add values to a dict union object.'
204 def __delitem__(self
, name
):
207 raise TypeError, 'can\'t delete values from a dict union object.'
208 def __contains__(self
, name
):
211 for m
in self
._members
:
219 for m
in self
._members
:
224 # the dict union specific interface...
226 def unite(self
, *others
):
228 add members to the union object.
230 NOTE: this method will add members to the top of the pririty
233 others
= list(others
)
235 self
._members
= tuple(others
) + self
._members
237 def tailunite(self
, *others
):
239 this is the same as unite but adds low priority members (to the
240 botom of the priority stack).
242 others
= list(others
)
244 self
._members
= self
._members
+ tuple(others
)
245 def memberindex(self
, obj
):
248 return list(self
._members
).index(obj
)
249 def removemember(self
, obj
):
252 if obj
in self
._members
:
253 self
._members
= tuple(list(self
._members
).remove(obj
))
255 raise TypeError, '%s does not contain %s as a member.' % (self
, obj
)
261 def popmember(self
, index
=0):
264 NOTE: this by default will remove the highest priority member.
266 m
= list(self
._members
)
268 self
._members
= tuple(m
)
270 def itermembers(self
):
273 for m
in self
._members
:
275 def getcontainerof(self
, name
):
278 for m
in self
._members
:
281 raise KeyError, '%s does not contain "%s"' % (self
, name
)
282 def getallcontainersof(self
, name
):
286 for m
in self
._members
:
290 raise KeyError, '%s does not contain "%s"' % (self
, name
)
294 this will return a dict copy of the DictUnion object.
296 return dict(self
.items())
299 #-------------------------------------------------------DictTypeUnion---
300 # WARNING: this is not done!
301 class DictTypeUnion(DictUnion
, dict):
303 this is a diriviation from dict that can contain oun data.
309 #-----------------------------------------------------------ListUnion---
310 ##def ListUnion(list):
316 #-------------------------------------------------------DictIntersect---
317 ##class DictIntersect(object):
324 #=======================================================================
325 # vim:set ts=4 sw=4 nowrap :