| 37 | |
| 38 | |
| 39 | class CalculatorHandler: |
| 40 | implements(Calculator.Iface) |
| 41 | |
| 42 | def __init__(self): |
| 43 | self.log = {} |
| 44 | |
| 45 | def ping(self): |
| 46 | print('ping()') |
| 47 | |
| 48 | def add(self, n1, n2): |
| 49 | print('add(%d,%d)' % (n1, n2)) |
| 50 | return n1 + n2 |
| 51 | |
| 52 | def calculate(self, logid, work): |
| 53 | print('calculate(%d, %r)' % (logid, work)) |
| 54 | |
| 55 | if work.op == Operation.ADD: |
| 56 | val = work.num1 + work.num2 |
| 57 | elif work.op == Operation.SUBTRACT: |
| 58 | val = work.num1 - work.num2 |
| 59 | elif work.op == Operation.MULTIPLY: |
| 60 | val = work.num1 * work.num2 |
| 61 | elif work.op == Operation.DIVIDE: |
| 62 | if work.num2 == 0: |
| 63 | raise InvalidOperation(work.op, 'Cannot divide by 0') |
| 64 | val = work.num1 / work.num2 |
| 65 | else: |
| 66 | raise InvalidOperation(work.op, 'Invalid operation') |
| 67 | |
| 68 | log = SharedStruct() |
| 69 | log.key = logid |
| 70 | log.value = '%d' % (val) |
| 71 | self.log[logid] = log |
| 72 | |
| 73 | return val |
| 74 | |
| 75 | def getStruct(self, key): |
| 76 | print('getStruct(%d)' % (key)) |
| 77 | return self.log[key] |
| 78 | |
| 79 | def zip(self): |
| 80 | print('zip()') |
| 81 | |
| 82 | |
| 83 | if __name__ == '__main__': |