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  import FoamFileGenerator 
  4  from copy import deepcopy 
  5  import string 
  6   
7 -class FoamDataType(object):
8 - def __repr__(self):
9 return "'"+str(self)+"'"
10 11
12 -class Field(FoamDataType):
13 - def __init__(self,val,name=None):
14 self.val=val 15 self.name=name 16 if self.name==None: 17 self.uniform=True 18 elif type(val) in[list,UnparsedList]: 19 self.uniform=False
20
21 - def __str__(self):
22 result="" 23 if self.uniform: 24 result+="uniform " 25 else: 26 result+="nonuniform "+self.name+" " 27 result+=str(FoamFileGenerator.FoamFileGenerator(self.val)) 28 return result
29
30 - def __cmp__(self,other):
31 if other==None: 32 return 1 33 if self.uniform!=other.uniform: 34 return cmp(self.uniform,other.uniform) 35 elif self.name!=other.name: 36 return cmp(self.name,other.name) 37 else: 38 return cmp(self.val,other.val)
39
40 - def __getitem__(self,key):
41 assert(not self.uniform) 42 return self.val[key]
43
44 - def __setitem__(self,key,value):
45 assert(not self.uniform) 46 self.val[key]=value
47
48 - def isUniform(self):
49 return self.uniform
50
51 - def value(self):
52 return self.val
53
54 - def setUniform(self,data):
55 self.val=data 56 self.uniform=True 57 self.name=None
58
59 -class Dimension(FoamDataType):
60 - def __init__(self,*dims):
61 assert(len(dims)==7) 62 self.dims=list(dims)
63
64 - def __str__(self):
65 result="[ " 66 for v in self.dims: 67 result+=str(v)+" " 68 result+="]" 69 return result
70
71 - def __cmp__(self,other):
72 if other==None: 73 return 1 74 return cmp(self.dims,other.dims)
75
76 - def __getitem__(self,key):
77 return self.dims[key]
78
79 - def __setitem__(self,key,value):
80 self.dims[key]=value
81
82 -class FixedLength(FoamDataType):
83 - def __init__(self,vals):
84 self.vals=vals[:]
85
86 - def __str__(self):
87 return "("+string.join(map(lambda v:"%g"%v,self.vals))+")"
88
89 - def __cmp__(self,other):
90 if other==None: 91 return 1 92 return cmp(self.vals,other.vals)
93
94 - def __getitem__(self,key):
95 return self.vals[key]
96
97 - def __setitem__(self,key,value):
98 self.vals[key]=value
99
100 -class Vector(FixedLength):
101 - def __init__(self,x,y,z):
102 FixedLength.__init__(self,[x,y,z])
103
104 -class Tensor(FixedLength):
105 - def __init__(self,v1,v2,v3,v4,v5,v6,v7,v8,v9):
106 FixedLength.__init__(self,[v1,v2,v3,v4,v5,v6,v7,v8,v9])
107
108 -class SymmTensor(FixedLength):
109 - def __init__(self,v1,v2,v3,v4,v5,v6):
110 FixedLength.__init__(self,[v1,v2,v3,v4,v5,v6])
111
112 -class DictProxy(dict):
113 """A class that acts like a dictionary, but preserves the order 114 of the entries. Used to beautify the output""" 115
116 - def __init__(self):
117 dict.__init__(self) 118 self._order=[]
119
120 - def __setitem__(self,key,value):
121 dict.__setitem__(self,key,value) 122 if key not in self._order: 123 self._order.append(key)
124
125 - def __delitem__(self,key):
126 dict.__delitem__(self,key) 127 self._order.remove(key)
128
129 - def __deepcopy__(self,memo):
130 new=DictProxy() 131 for k in self._order: 132 new[k]=deepcopy(self[k],memo) 133 return new
134
135 -class TupleProxy(list):
136 """Enables Tuples to be manipulated""" 137
138 - def __init__(self,tup=()):
139 list.__init__(self,tup)
140
141 -class Unparsed(object):
142 """A class that encapsulates an unparsed string""" 143
144 - def __init__(self,data):
145 self.data=data
146
147 - def __str__(self):
148 return self.data
149
150 -class UnparsedList(object):
151 """A class that encapsulates a list that was not parsed for 152 performance reasons""" 153
154 - def __init__(self,lngth,data):
155 self.data=data 156 self.length=lngth
157
158 - def __len__(self):
159 return self.length
160
161 - def __cmp__(self,other):
162 return cmp(self.data,other.data)
163