1 | from objects import OMsymbol |
---|
2 | from omexceptions import * |
---|
3 | class OMcd(object): |
---|
4 | def __init__(self,name, base="http://www.openmath.org/cd"): |
---|
5 | self.name=name |
---|
6 | self.base=base |
---|
7 | def __eq__(self, other): |
---|
8 | return (self.name==other.name) and (self.base==other.base) |
---|
9 | def __hash__(self): |
---|
10 | return hash((self.name,self.base)) |
---|
11 | |
---|
12 | class OMcdImplementation(object): |
---|
13 | def __init__(self,cd): |
---|
14 | self.cd=cd |
---|
15 | self.implementations={} |
---|
16 | def __getitem__(self, name): |
---|
17 | return self.implementations[name] |
---|
18 | def __setitem__(self,name,value): |
---|
19 | self.implementations[name]=value |
---|
20 | def implement(self,symbolname, func): |
---|
21 | symbol=OMsymbol(symbolname,self.cd) |
---|
22 | impl=ImplementedOMsymbol(symbol,func) |
---|
23 | self[symbol]=impl |
---|
24 | |
---|
25 | |
---|
26 | |
---|
27 | class ImplementedOMsymbol(OMsymbol): |
---|
28 | def __init__(self,symbol, func): |
---|
29 | super(ImplementedOMsymbol,self).__init__(symbol.name, symbol.cd) |
---|
30 | self.implementation=func |
---|
31 | def __str__(self): |
---|
32 | return "ImplementedOMS("+self.name+", " + self.cd.name +")" |
---|
33 | def __call__(self, context, *args): |
---|
34 | try: |
---|
35 | erg=self.implementation(context,*args) |
---|
36 | except KeyError: |
---|
37 | raise EvaluationFailedError |
---|
38 | return context.package(erg) |
---|