1 #=======================================================================
3 __version__
= '''0.0.03'''
4 __sub_version__
= '''20040512161550'''
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 #-----------------------------------------------------------------------
16 #-----------------------------------------------------------------------
17 #-------------------------------------------------------dictcopyunite---
18 def dictcopyunite(*members
):
27 #-----------------------------------------------------------DictUnion---
28 class DictUnion(object):
30 this is a dict like object, that acts as a union of its members but
31 without modifieng its members in any way.
33 this is similar to a sequential update of all members, but retains
34 the container information for each item, and does not have the
35 overhead of creating a new resulting dict.
37 NOTE: because of the nature of dicts, the later added members (unite)
38 have higher priority than the former.
39 NOTE: the members added in one call (be it __init__, unite or tailunite)
40 have descending priority -- first highest last lowest.
41 NOTE: due to ints nature this object is *live*, e.g. it emidiatly
42 reflects all modifications to its members as they are modified.
46 def __init__(self
, *members
):
49 members
= list(members
)
51 self
._members
= tuple(members
)
52 def __getitem__(self
, name
):
55 for m
in self
._members
:
58 raise KeyError, 'key "%s" is not present in any of the members.' % name
59 ## def __setitem__(self, name, value):
64 ## raise TypeError, 'can\'t add values to a dict union object.'
65 ## def __delitem__(self, name):
68 ## raise TypeError, 'can\'t delete values from a dict union object.'
69 def __contains__(self
, name
):
72 for m
in self
._members
:
80 for m
in self
._members
:
117 ## def __repr__(self):
121 ## def __str__(self):
125 ## def __reduce__(self):
129 ## def __reduce_ex__(self):
140 return list(self
.itervalues())
144 return list(self
.iteritems())
148 return self
.__iter
__()
149 def itervalues(self
):
159 def get(self
, name
, dfl
=None):
168 def has_key(self
, name
):
172 ## def setdefault(self):
176 # the dict union specific interface...
178 def unite(self
, *others
):
180 add members to the union object.
182 NOTE: this method will add members to the top of the pririty
185 others
= list(others
)
187 self
._members
= tuple(others
) + self
._members
189 def tailunite(self
, *others
):
191 this is the same as unite but adds low priority members (to the
192 botom of the priority stack).
194 others
= list(others
)
196 self
._members
= self
._members
+ tuple(others
)
197 def removemember(self
, obj
):
200 if obj
in self
._members
:
201 self
._members
= tuple(list(self
._members
).remove(obj
))
203 raise TypeError, '%s does not contain %s as a member.' % (self
, obj
)
208 def itermembers(self
):
211 for m
in self
._members
:
213 def getcontainerof(self
, name
):
216 for m
in self
._members
:
219 raise KeyError, '%s does not contain "%s"' % (self
, name
)
220 def getallcontainersof(self
, name
):
224 for m
in self
._members
:
228 raise KeyError, '%s does not contain "%s"' % (self
, name
)
232 #-------------------------------------------------------DictTypeUnion---
233 # WARNING: this is not done!
234 class DictTypeUnion(DictUnion
, dict):
236 this is a diriviation from dict that can contain oun data.
242 #-----------------------------------------------------------ListUnion---
243 ##def ListUnion(list):
250 #=======================================================================
251 # vim:set ts=4 sw=4 nowrap :