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