source: git/old_modgen/openmathserver/cd.py @ 75f460

spielwiese
Last change on this file since 75f460 was 75f460, checked in by Hans Schoenemann <hannes@…>, 9 years ago
format
  • Property mode set to 100644
File size: 1.3 KB
Line 
1from objects import OMSymbol
2from omexceptions import *
3class 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        try:
9            return (self.name == other.name) and (self.base == other.base)
10        except:
11            return False
12    def __hash__(self):
13        return hash((self.name,self.base))
14
15class OMCDImplementation(object):
16    def __init__(self,cd):
17        self.cd = cd
18        self.implementations = {}
19    def __getitem__(self, name):
20        return self.implementations[name]
21    def __setitem__(self,name,value):
22        self.implementations[name] = value
23    def implement(self,symbolname, func):
24        symbol=OMSymbol(symbolname,self.cd)
25        impl = ImplementedOMSymbol(symbol,func)
26        self[symbol]=impl
27
28
29
30class ImplementedOMSymbol(OMSymbol):
31    def __init__(self,symbol, func):
32        super(ImplementedOMSymbol,self).__init__(symbol.name, symbol.cd)
33        self.implementation = func
34    def __str__(self):
35        return "ImplementedOMS("+self.name+", " + self.cd.name +")"
36    def __call__(self, context, *args):
37        try:
38            erg=self.implementation(context,*args)
39        except KeyError:
40             raise EvaluationFailedError
41        return context.package(erg)
Note: See TracBrowser for help on using the repository browser.