Changeset 2dc9fea in git for modules/openmathserver/objects.py
- Timestamp:
- Jun 23, 2005, 5:15:36 PM (18 years ago)
- Branches:
- (u'spielwiese', '8e0ad00ce244dfd0756200662572aef8402f13d5')
- Children:
- 62b33dbe01f95458b81ecda6e278190e3b66d9be
- Parents:
- a868907fc6dd9b59a0d27bd87056410c92a84223
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
modules/openmathserver/objects.py
ra86890 r2dc9fea 7 7 class XMLAttribute(object): 8 8 def __init__(self, name, value): 9 self.name =name10 self.value =value9 self.name = name 10 self.value = value 11 11 def encode(self, context): 12 12 return "".join([self.name,"=\"",self.value,"\""]) … … 36 36 self.setChildren(children) 37 37 except AttributeError: 38 38 raise UnsupportedOperationError 39 39 def __getBody(self): 40 40 try: … … 58 58 self.setBody(body) 59 59 except AttributeError: 60 60 raise UnsupportedOperationError 61 61 def __getXMLAttributes(self): 62 62 try: … … 86 86 body=property(__getBody,__setBody,__delBody,\ 87 87 "xml body,FIXME: at the moment only char data") 88 XMLAttributes=property(__getXMLAttributes,__setXMLAttributes,__delXMLAttributes,\ 88 XMLAttributes=property(__getXMLAttributes,\ 89 __setXMLAttributes,__delXMLAttributes,\ 89 90 "xml attributes") 90 91 def XMLEncode(self, context): … … 92 93 attr=self.XMLAttributes 93 94 if attr: 94 attrstr =" "+" ".join([a.encode(context) for a in attr])95 attrstr = " "+" ".join([a.encode(context) for a in attr]) 95 96 else: 96 attrstr =""97 opening ="".join(["<", self.XMLtag, attrstr,">"])98 children =self.children97 attrstr = "" 98 opening = "".join(["<", self.XMLtag, attrstr,">"]) 99 children = self.children 99 100 if children: 100 body ="".join([context.XMLEncodeObject(c) for c in children])101 body = "".join([context.XMLEncodeObject(c) for c in children]) 101 102 else: 102 body =self.body103 body = self.body 103 104 if not body: 104 body =""105 assert body !=None106 body =context.XMLEncodeBody(body)107 assert body !=None108 closing ="".join(["</"+self.XMLtag+">"])105 body = "" 106 assert body != None 107 body = context.XMLEncodeBody(body) 108 assert body != None 109 closing = "".join(["</"+self.XMLtag+">"]) 109 110 return "".join([opening,body,closing]) 110 111 class OMObject(OMObjectBase): 111 112 def __init__(self, children): 112 super(OMObject, self).__init__()113 self.children =children113 super(OMObject, self).__init__() 114 self.children = children 114 115 def getChildren(self): 115 116 return self.__children 116 def setChildren(self ,children):117 def setChildren(self ,children): 117 118 self.__children=children 118 XMLtag ="OMOBJ"119 XMLtag = "OMOBJ" 119 120 def evaluate(self, context): 120 121 return OMObject([context.evaluate(c) for c in self.children]) 121 122 class OMVar(OMObjectBase): 122 123 def __init__(self,name): 123 super(OMVar, self).__init__()124 self.name =name125 def evaluate(self, context):124 super(OMVar, self).__init__() 125 self.name = name 126 def evaluate(self, context): 126 127 try: 127 128 return context[self.name] … … 130 131 def __str__(self): 131 132 return "OMV(" + self.name +")" 132 XMLtag ="OMV"133 XMLtag = "OMV" 133 134 def getXMLAttributes(self): 134 135 return [XMLAttribute("name", self.name)] … … 136 137 class OMApply(OMObjectBase): 137 138 def __init__(self, func, args): 138 super(OMApply, self).__init__()139 self.func =func140 self.args =args141 def evaluate(self, context):142 efunc =context.evaluate(self.func)143 eargs =[context.evaluate(a) for a in self.args]139 super(OMApply, self).__init__() 140 self.func = func 141 self.args = args 142 def evaluate(self, context): 143 efunc = context.evaluate(self.func) 144 eargs = [context.evaluate(a) for a in self.args] 144 145 if callable(efunc): 145 146 try: … … 157 158 158 159 class OMSymbol(OMObjectBase): 159 def __init__(self,name,cd =None):160 def __init__(self,name,cd = None): 160 161 super(OMSymbol,self).__init__() 161 self.cd =cd162 self.name =name162 self.cd = cd 163 self.name = name 163 164 def __eq__(self, other): 164 165 try: 165 return bool(other.name ==self.name and self.cd==other.cd)166 return bool(other.name == self.name and self.cd == other.cd) 166 167 except: 167 168 return False 168 169 def __str__(self): 169 return "OMS(" +self.name+", "+self.cd.name + ")"170 return "OMS(" + self.name + ", " + self.cd.name + ")" 170 171 def __hash__(self):# 171 172 return hash((self.name,self.cd.__hash__())) … … 175 176 def getXMLAttributes(self): 176 177 return [XMLAttribute("name", self.name),\ 177 XMLAttribute("cdbase", self.cd.base),\178 XMLAttribute("cd", self.cd.name)]178 XMLAttribute("cdbase", self.cd.base),\ 179 XMLAttribute("cd", self.cd.name)] 179 180 def setXMLAttributes(self): 180 181 raise UnsupportedOperationError 181 182 class SimpleValue(OMObjectBase): 182 def __init__(self, value):183 super(SimpleValue, self).__init__()183 def __init__(self, value): 184 super(SimpleValue, self).__init__() 184 185 if (isinstance(value, str)): 185 value =self.parse(value)186 self.value =value186 value = self.parse(value) 187 self.value = value 187 188 def evaluate(self, context): 188 189 return self … … 195 196 def __init__(self,value): 196 197 if not isinstance(value,int): 197 value =self.parse(value)198 value = self.parse(value) 198 199 super(OMint,self).__init__(value) 199 200 def parse(self,value): … … 209 210 XMLtag="OMI" 210 211 class OMfloat(SimpleValue): 211 def __init__(self, value):212 super(OMfloat, self).__init__(value)212 def __init__(self, value): 213 super(OMfloat, self).__init__(value) 213 214 def parse(self, value): 214 215 """FIXME: Not fully standard compliant, … … 217 218 def __str__(self): 218 219 return "OMfloat("+repr(self.value)+")" 219 XMLtag ="OMF"220 XMLtag = "OMF" 220 221 def getXMLAttributes(self): 221 222 return [XMLAttribute("dec",str(self.value))] … … 225 226 def __str__(self): 226 227 return "OMSTR("+repr(self.value)+")" 227 XMLtag ="OMSTR"228 XMLtag = "OMSTR" 228 229 def getBody(self): 229 230 return self.value … … 232 233 super(OMByteArray,self).__init__(value) 233 234 def __str__(self): 234 return "OMByteArray(" +repr(self.value)+")"235 return "OMByteArray(" + repr(self.value) + ")" 235 236 def parse(self, value): 236 237 return value … … 247 248 return context.XMLEncodeObject(self.ref) 248 249 class OMAttributePair(OMObjectBase): 249 def __init__(self, key, value):250 def __init__(self, key, value): 250 251 super(OMAttributePair,self).__init__() 251 self.key =key252 self.value =value252 self.key = key 253 self.value = value 253 254 def getChildren(self): 254 255 return [self.key, self.value] … … 260 261 def __init__(self, *args): 261 262 super(OMAttribution,self).__init__() 262 self.attr =list(args[:-1])263 self.value =args[-1]263 self.attr = list(args[:-1]) 264 self.value = args[-1] 264 265 def getChildren(self): 265 266 #print type(self.attr) … … 267 268 return self.attr+[self.value] 268 269 def evaluate(self, context): 269 value =copy(self.value)270 value.attributes =copy(value.attributes)270 value = copy(self.value) 271 value.attributes = copy(value.attributes) 271 272 for a in self.attr: 272 273 ae=context.evaluate(a) 273 value.attributes[ae.key] =ae.value274 value.attributes[ae.key] = ae.value 274 275 return value 275 XMLtag ="OMATTR"276 if __name__ =='__main__':276 XMLtag = "OMATTR" 277 if __name__ == '__main__': 277 278 from context import * 278 279 279 280 from binding import * 280 281 281 context =Context()282 context = Context() 282 283 283 284 context.push({}) 284 285 285 context["x"] =OMint(1)286 287 x =OMVar("x")288 289 y =OMVar("y")286 context["x"] = OMint(1) 287 288 x = OMVar("x") 289 290 y = OMVar("y") 290 291 291 292 print context.evaluate(x) … … 293 294 firstArg=OMBinding(lambdasym,[OMVar("x"), OMVar("y")], OMVar("x")) 294 295 #print context.evaluate(firstArg) 295 application =OMApply(firstArg, [x,y])296 application = OMApply(firstArg, [x,y]) 296 297 print context.evaluate(application) 297 application =OMApply(firstArg,[y,x])298 application = OMApply(firstArg, [y,x]) 298 299 print context.evaluate(application) 299 300 import arith1 … … 302 303 #application=OMApply(arith1.plussym,[x]) 303 304 #application=OMApply(arith1.plussym,[x,x]) 304 application =OMApply(OMSymbol("plus",arith1.content),[x,x])305 application = OMApply(OMSymbol("plus",arith1.content),[x, x]) 305 306 306 307 print context.evaluate(application) 307 application=OMApply(OMSymbol("plus",arith1.content),[x, x,x])308 application=OMApply(OMSymbol("plus",arith1.content),[x, x, x]) 308 309 309 310 print context.evaluate(application) 310 i =OMint(22482489)311 i = sOMint(22482489) 311 312 print i.body 312 313 print i.XMLEncode(context)
Note: See TracChangeset
for help on using the changeset viewer.