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 - def __len__(self):
101 return len(self.vals)
102
103 -class Vector(FixedLength):
104 - def __init__(self,x,y,z):
105 FixedLength.__init__(self,[x,y,z])
106
107 -class Tensor(FixedLength):
108 - def __init__(self,v1,v2,v3,v4,v5,v6,v7,v8,v9):
109 FixedLength.__init__(self,[v1,v2,v3,v4,v5,v6,v7,v8,v9])
110
111 -class SymmTensor(FixedLength):
112 - def __init__(self,v1,v2,v3,v4,v5,v6):
113 FixedLength.__init__(self,[v1,v2,v3,v4,v5,v6])
114
115 -class DictProxy(dict):
116 """A class that acts like a dictionary, but preserves the order 117 of the entries. Used to beautify the output""" 118
119 - def __init__(self):
120 dict.__init__(self) 121 self._order=[]
122
123 - def __setitem__(self,key,value):
124 dict.__setitem__(self,key,value) 125 if key not in self._order: 126 self._order.append(key)
127
128 - def __delitem__(self,key):
129 dict.__delitem__(self,key) 130 self._order.remove(key)
131
132 - def __deepcopy__(self,memo):
133 new=DictProxy() 134 for k in self._order: 135 new[k]=deepcopy(self[k],memo) 136 return new
137
138 -class TupleProxy(list):
139 """Enables Tuples to be manipulated""" 140
141 - def __init__(self,tup=()):
142 list.__init__(self,tup)
143
144 -class Unparsed(object):
145 """A class that encapsulates an unparsed string""" 146
147 - def __init__(self,data):
148 self.data=data
149
150 - def __str__(self):
151 return self.data
152
153 -class UnparsedList(object):
154 """A class that encapsulates a list that was not parsed for 155 performance reasons""" 156
157 - def __init__(self,lngth,data):
158 self.data=data 159 self.length=lngth
160
161 - def __len__(self):
162 return self.length
163
164 - def __cmp__(self,other):
165 return cmp(self.data,other.data)
166