Package PyFoam :: Package Basics :: Module DataStructures
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.Basics.DataStructures

  1  """Data structures in Foam-Files that can't be directly represented by Python-Structures""" 
  2   
  3  from __future__ import division 
  4   
  5  import PyFoam.Basics.FoamFileGenerator 
  6   
  7  from copy import deepcopy 
  8  import string,math 
  9  import re 
 10   
 11  from PyFoam.ThirdParty.six import integer_types,PY3,iteritems 
 12   
 13  if PY3: 
14 - def cmp(a,b):
15 if a<b: 16 return -1 17 elif a==b: 18 return 0 19 else: 20 return 1
21
22 -class FoamDataType(object):
23 - def __repr__(self):
24 return "'"+str(self)+"'"
25
26 - def __eq__(self,other):
27 """Implementation to make __cmp__ work again in Python3 28 29 Implementing this method means that these objects are not hashable. 30 But that is OK 31 """ 32 return self.__cmp__(other)==0
33
34 - def __lt__(self,other):
35 "Implementation to make __cmp__ work again in Python3" 36 return self.__cmp__(other)<0
37
38 - def __ne__(self,other):
39 return self.__cmp__(other)!=0
40
41 - def __gt__(self,other):
42 return self.__cmp__(other)>0
43
44 - def __ge__(self,other):
45 return self.__cmp__(other)>=0
46
47 - def __le__(self,other):
48 return self.__cmp__(other)<=0
49
50 -class Field(FoamDataType):
51 - def __init__(self,val,name=None):
52 self.val=val 53 self.name=name 54 if type(val) in[list,UnparsedList,BinaryList]: 55 self.uniform=False 56 elif self.name==None: 57 self.uniform=True 58 else: 59 raise TypeError("Type",type(val),"of value",val,"can not be used to determine uniformity")
60
61 - def __str__(self):
62 result="" 63 if self.uniform: 64 result+="uniform " 65 else: 66 result+="nonuniform " 67 if self.name: 68 result+=self.name+" " 69 70 result+=str(PyFoam.Basics.FoamFileGenerator.FoamFileGenerator(self.val, 71 longListThreshold=-1)) 72 return result
73
74 - def __cmp__(self,other):
75 if other==None or type(other)!=Field: 76 return 1 77 if self.uniform!=other.uniform: 78 return cmp(self.uniform,other.uniform) 79 elif self.name!=other.name: 80 return cmp(self.name,other.name) 81 else: 82 return cmp(self.val,other.val)
83
84 - def __getitem__(self,key):
85 assert(not self.uniform) 86 return self.val[key]
87
88 - def __setitem__(self,key,value):
89 assert(not self.uniform) 90 self.val[key]=value
91
92 - def isUniform(self):
93 return self.uniform
94
95 - def isBinary(self):
96 return type(self.val)==BinaryList
97
98 - def binaryString(self):
99 return "nonuniform "+self.name+" <BINARY DATA>"
100
101 - def value(self):
102 return self.val
103
104 - def setUniform(self,data):
105 self.val=data 106 self.uniform=True 107 self.name=None
108
109 -class Dimension(FoamDataType):
110 - def __init__(self,*dims):
111 assert(len(dims)==7) 112 self.dims=list(dims)
113
114 - def __str__(self):
115 result="[ " 116 for v in self.dims: 117 result+=str(v)+" " 118 result+="]" 119 return result
120
121 - def __cmp__(self,other):
122 if other==None: 123 return 1 124 return cmp(self.dims,other.dims)
125
126 - def __getitem__(self,key):
127 return self.dims[key]
128
129 - def __setitem__(self,key,value):
130 self.dims[key]=value
131
132 -class FixedLength(FoamDataType):
133 - def __init__(self,vals):
134 self.vals=vals[:]
135
136 - def __str__(self):
137 return "("+" ".join(["%g"%v for v in self.vals])+")"
138
139 - def __cmp__(self,other):
140 if other==None or not issubclass(type(other),FixedLength): 141 return 1 142 return cmp(self.vals,other.vals)
143
144 - def __getitem__(self,key):
145 return self.vals[key]
146
147 - def __setitem__(self,key,value):
148 self.vals[key]=value
149
150 - def __len__(self):
151 return len(self.vals)
152
153 -class Vector(FixedLength):
154 - def __init__(self,x,y,z):
155 FixedLength.__init__(self,[x,y,z])
156
157 - def __add__(self,y):
158 x=self 159 if type(y)==Vector: 160 return Vector(x[0]+y[0],x[1]+y[1],x[2]+y[2]) 161 elif type(y) in integer_types+(float,): 162 return Vector(x[0]+y,x[1]+y,x[2]+y) 163 else: 164 return NotImplemented
165
166 - def __radd__(self,y):
167 x=self 168 if type(y) in integer_types+(float,): 169 return Vector(x[0]+y,x[1]+y,x[2]+y) 170 else: 171 return NotImplemented
172
173 - def __sub__(self,y):
174 x=self 175 if type(y)==Vector: 176 return Vector(x[0]-y[0],x[1]-y[1],x[2]-y[2]) 177 elif type(y) in integer_types+(float,): 178 return Vector(x[0]-y,x[1]-y,x[2]-y) 179 else: 180 return NotImplemented
181
182 - def __rsub__(self,y):
183 x=self 184 if type(y) in integer_types+(float,): 185 return Vector(y-x[0],y-x[1],y-x[2]) 186 else: 187 return NotImplemented
188
189 - def __mul__(self,y):
190 x=self 191 if type(y)==Vector: 192 return Vector(x[0]*y[0],x[1]*y[1],x[2]*y[2]) 193 elif type(y) in integer_types+(float,): 194 return Vector(x[0]*y,x[1]*y,x[2]*y) 195 else: 196 return NotImplemented
197
198 - def __rmul__(self,y):
199 x=self 200 if type(y) in integer_types+(float,): 201 return Vector(y*x[0],y*x[1],y*x[2]) 202 else: 203 return NotImplemented
204
205 - def __div__(self,y):
206 x=self 207 if type(y)==Vector: 208 return Vector(x[0]/y[0],x[1]/y[1],x[2]/y[2]) 209 elif type(y) in integer_types+(float,): 210 return Vector(x[0]/y,x[1]/y,x[2]/y) 211 else: 212 return NotImplemented
213
214 - def __truediv__(self,y):
215 return self.__div__(y)
216
217 - def __xor__(self,y):
218 x=self 219 if type(y)==Vector: 220 return Vector(x[1]*y[2]-x[2]*y[1], 221 x[2]*y[0]-x[0]*y[2], 222 x[0]*y[1]-x[1]*y[0]) 223 else: 224 return NotImplemented
225
226 - def __abs__(self):
227 x=self 228 return math.sqrt(x[0]*x[0]+x[1]*x[1]+x[2]*x[2])
229
230 - def __neg__(self):
231 x=self 232 return Vector(-x[0],-x[1],-x[2])
233
234 - def __pos__(self):
235 x=self 236 return Vector( x[0], x[1], x[2])
237
238 -class Tensor(FixedLength):
239 - def __init__(self,v1,v2,v3,v4,v5,v6,v7,v8,v9):
240 FixedLength.__init__(self,[v1,v2,v3,v4,v5,v6,v7,v8,v9])
241
242 -class SymmTensor(FixedLength):
243 - def __init__(self,v1,v2,v3,v4,v5,v6):
244 FixedLength.__init__(self,[v1,v2,v3,v4,v5,v6])
245
246 -class DictRedirection(object):
247 """This class is in charge of handling redirections to other directories"""
248 - def __init__(self,fullCopy,reference,name):
249 self._fullCopy=fullCopy 250 self._reference=reference 251 self._name=name
252
253 - def useAsRedirect(self):
254 self._fullCopy=None
255
256 - def getContent(self):
257 result=self._fullCopy 258 self._fullCopy=None 259 return result
260
261 - def __call__(self):
262 return self._reference
263
264 - def __str__(self):
265 return "$"+self._name
266
267 - def __float__(self):
268 return float(self._reference)
269
270 -class DictProxy(dict):
271 """A class that acts like a dictionary, but preserves the order 272 of the entries. Used to beautify the output""" 273
274 - def __init__(self):
275 dict.__init__(self) 276 self._order=[] 277 self._decoration={} 278 self._regex=[] 279 self._redirects=[]
280
281 - def __setitem__(self,key,value):
282 isRegex=False 283 if type(key)==str: 284 if key[0]=='"' and key[-1]=='"': 285 isRegex=True 286 if isRegex: 287 exp=re.compile(key[1:-1]) 288 self._regex=[(key,exp,value)]+self._regex 289 else: 290 dict.__setitem__(self,key,value) 291 if key not in self._order or isRegex: 292 self._order.append(key)
293
294 - def __getitem__(self,key):
295 try: 296 return dict.__getitem__(self,key) 297 except KeyError: 298 for k,e,v in self._regex: 299 if e.match(key): 300 return v 301 for r in self._redirects: 302 try: 303 return r()[key] 304 except KeyError: 305 pass 306 307 raise KeyError(key)
308
309 - def __delitem__(self,key):
310 dict.__delitem__(self,key) 311 self._order.remove(key) 312 if key in self._decoration: 313 del self._decoration[key]
314
315 - def __deepcopy__(self,memo):
316 new=DictProxy() 317 for k in self._order: 318 if type(k)==DictRedirection: 319 new.addRedirection(k) 320 else: 321 try: 322 new[k]=deepcopy(self[k],memo) 323 except KeyError: 324 new[k]=deepcopy(self.getRegexpValue(k),memo) 325 326 return new
327
328 - def __contains__(self,key):
329 if dict.__contains__(self,key): 330 return True 331 else: 332 for k,e,v in self._regex: 333 if e.match(key): 334 return True 335 for r in self._redirects: 336 if key in r(): 337 return True 338 339 return False
340
341 - def keys(self):
342 return [x for x in self._order if type(x)!=DictRedirection]
343
344 - def __str__(self):
345 first=True 346 result="{" 347 for k in self.keys(): 348 v=self[k] 349 if first: 350 first=False 351 else: 352 result+=", " 353 result+="%s: %s" % (repr(k),repr(v)) 354 result+="}" 355 return result
356
357 - def addDecoration(self,key,text):
358 if key in self: 359 if key not in self._decoration: 360 self._decoration[key]="" 361 self._decoration[key]+=text
362
363 - def getDecoration(self,key):
364 if key in self._decoration: 365 return " \t"+self._decoration[key] 366 else: 367 return ""
368
369 - def getRegexpValue(self,key):
370 for k,e,v in self._regex: 371 if k==key: 372 return v 373 raise KeyError(key)
374
375 - def addRedirection(self,redir):
376 self._order.append(redir) 377 redir.useAsRedirect() 378 self._redirects.append(redir)
379
380 -class TupleProxy(list):
381 """Enables Tuples to be manipulated""" 382
383 - def __init__(self,tup=()):
384 list.__init__(self,tup)
385
386 -class Unparsed(object):
387 """A class that encapsulates an unparsed string""" 388
389 - def __init__(self,data):
390 self.data=data
391
392 - def __str__(self):
393 return self.data
394
395 - def __hash__(self):
396 return hash(self.data)
397
398 - def __lt__(self,other):
399 return self.data<other.data
400
401 -class BinaryBlob(Unparsed):
402 """Represents a part of the file with binary data in it"""
403 - def __init__(self,data):
404 Unparsed.__init__(self,data)
405
406 -class Codestream(str):
407 """A class that encapsulates an codestream string""" 408
409 - def __str__(self):
410 return "#{" + str.__str__(self) + "#}"
411
412 -class UnparsedList(object):
413 """A class that encapsulates a list that was not parsed for 414 performance reasons""" 415
416 - def __init__(self,lngth,data):
417 self.data=data 418 self.length=lngth
419
420 - def __len__(self):
421 return self.length
422
423 - def __cmp__(self,other):
424 return cmp(self.data,other.data)
425
426 - def __eq__(self,other):
427 return self.data==other.data
428
429 - def __lt__(self,other):
430 return self.data<other.data
431
432 -class BinaryList(UnparsedList):
433 """A class that represents a list that is saved as binary data""" 434
435 - def __init__(self,lngth,data):
436 UnparsedList.__init__(self,lngth,data)
437 438 # Should work with Python3 and Python2 439