added pli.misc.namespace experimental module...
[pli.git] / pli / logictypes.py
blob375fbb753b453b095c68acce0419a0d6a1cb8169
1 #=======================================================================
3 __version__ = '''0.0.03'''
4 __sub_version__ = '''20040512161550'''
5 __copyright__ = '''(c) Alex A. Naanou 2003'''
7 __doc__ = '''\
8 this module defines a number of utilities and objects to assist advanced
9 usage of standard python types.
10 '''
12 #-----------------------------------------------------------------------
16 #-----------------------------------------------------------------------
17 #-------------------------------------------------------dictcopyunite---
18 def dictcopyunite(*members):
19 '''
20 '''
21 res = {}
22 for m in members:
23 res.update(m)
24 return res
27 #-----------------------------------------------------------DictUnion---
28 class DictUnion(object):
29 '''
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.
43 '''
44 _members = ()
46 def __init__(self, *members):
47 '''
48 '''
49 members = list(members)
50 members.reverse()
51 self._members = tuple(members)
52 def __getitem__(self, name):
53 '''
54 '''
55 for m in self._members:
56 if name in m:
57 return m[name]
58 raise KeyError, 'key "%s" is not present in any of the members.' % name
59 ## def __setitem__(self, name, value):
60 ## '''
61 ## '''
62 ## # find source...
63 ## # set
64 ## raise TypeError, 'can\'t add values to a dict union object.'
65 ## def __delitem__(self, name):
66 ## '''
67 ## '''
68 ## raise TypeError, 'can\'t delete values from a dict union object.'
69 def __contains__(self, name):
70 '''
71 '''
72 for m in self._members:
73 if name in m:
74 return True
75 return False
76 def __iter__(self):
77 '''
78 '''
79 seen = []
80 for m in self._members:
81 for n in m:
82 if n not in seen:
83 seen += [n]
84 yield n
85 ## def __len__(self):
86 ## '''
87 ## '''
88 ## pass
89 ## def __cmp__(self):
90 ## '''
91 ## '''
92 ## pass
93 ## def __eq__(self):
94 ## '''
95 ## '''
96 ## pass
97 ## def __ge__(self):
98 ## '''
99 ## '''
100 ## pass
101 ## def __gt__(self):
102 ## '''
103 ## '''
104 ## pass
105 ## def __le__(self):
106 ## '''
107 ## '''
108 ## pass
109 ## def __lt__(self):
110 ## '''
111 ## '''
112 ## pass
113 ## def __ne__(self):
114 ## '''
115 ## '''
116 ## pass
117 ## def __repr__(self):
118 ## '''
119 ## '''
120 ## pass
121 ## def __str__(self):
122 ## '''
123 ## '''
124 ## pass
125 ## def __reduce__(self):
126 ## '''
127 ## '''
128 ## pass
129 ## def __reduce_ex__(self):
130 ## '''
131 ## '''
132 ## pass
133 def keys(self):
136 return list(self)
137 def values(self):
140 return list(self.itervalues())
141 def items(self):
144 return list(self.iteritems())
145 def iterkeys(self):
148 return self.__iter__()
149 def itervalues(self):
152 for k in self:
153 yield self[k]
154 def iteritems(self):
157 for k in self:
158 yield k, self[k]
159 def get(self, name, dfl=None):
162 try:
163 return self[name]
164 except KeyError:
165 if dfl == None:
166 raise
167 return dfl
168 def has_key(self, name):
171 return name in self
172 ## def setdefault(self):
173 ## '''
174 ## '''
175 ## pass
176 # the dict union specific interface...
177 ##!!! revise...
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
183 stack.
185 others = list(others)
186 others.reverse()
187 self._members = tuple(others) + self._members
188 ##!!! revise...
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)
195 others.reverse()
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))
202 return
203 raise TypeError, '%s does not contain %s as a member.' % (self, obj)
204 def members(self):
207 return self._members
208 def itermembers(self):
211 for m in self._members:
212 yield m
213 def getcontainerof(self, name):
216 for m in self._members:
217 if name in m:
218 return m
219 raise KeyError, '%s does not contain "%s"' % (self, name)
220 def getallcontainersof(self, name):
223 res = []
224 for m in self._members:
225 if name in m:
226 res += [m]
227 if res == []:
228 raise KeyError, '%s does not contain "%s"' % (self, name)
229 return res
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.
238 ##!!!
239 pass
242 #-----------------------------------------------------------ListUnion---
243 ##def ListUnion(list):
244 ## '''
245 ## '''
246 ## pass
250 #=======================================================================
251 # vim:set ts=4 sw=4 nowrap :