Advertisement
jensyao

Untitled

Feb 8th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.61 KB | None | 0 0
  1. from __future__ import print_function
  2.  
  3. class calcerr(ValueError):
  4.     '''mistake'''
  5. class oper2:
  6.     def __init__(self,func,num_args=2):
  7.         self.func=func
  8.         self.num_args=num_args
  9. class calc:
  10.     def __init__(self):
  11.         import operator as oper
  12.         self.oper_list = {'+':oper2(oper.add),
  13.                      "-":oper2(oper.sub),
  14.                      "*":oper2(oper.mul),
  15.                      "/":oper2(oper.truediv),
  16.                      "^":oper2(oper.pow),
  17.                      "%":oper2(oper.mod),
  18.                      "abs":oper2(oper.abs,num_args=1),
  19.                     }
  20.         self.eval_q=[]
  21.     def clear_q(self):
  22.         self.eval_q=[]
  23.     def print_q(self):
  24.         print(self.eval_q)
  25.     def add_custom(self,filename):
  26.         import json
  27.         with open(filename) as file:
  28.             data=json.loads(file.read())
  29.         import math
  30.         for key,module in data.items():
  31.             if hasattr(math, module):
  32.                 self.oper_list[key] = oper2(getattr(math, module), num_args=1)
  33.     def addto_q(self,val):
  34.         self.eval_q.append(self.convert_user(val))
  35.         if self.isoperation(self.eval_q[-1]):
  36.             result = self.eval_q.pop()
  37.             num_args = self.oper_list[result].num_args
  38.             if len(self.eval_q)<num_args:
  39.                 raise calcerr('not enough val')
  40.             args = []
  41.             for i in range(num_args):
  42.                 args.append(self.eval_q.pop())
  43.             result = calc.perf_oper(result,args)
  44.             self.eval_q.append(result)
  45.     def print_oper(self):
  46.         print('operations list:')
  47.         for x in self.oper_list.keys():
  48.             print(x,)
  49.         print('')
  50.        
  51.     def isoperation(self,val):
  52.          if val in self.oper_list.keys():
  53.             return True
  54.          else:
  55.             return False
  56.  
  57.     def convert_user(self,value):
  58.         if self.isfloat(value):
  59.             return float(value)
  60.         elif self.isoperation(value):
  61.             return value
  62.         else:
  63.             raise calcerr('invalid')
  64.    
  65.     def perf_oper(self,op,val):
  66.         if self.isoperation(op):
  67.             return self.oper_list[op].func(*val)
  68.         return op
  69.     @staticmethod
  70.     def isfloat(value):
  71.         try:
  72.             tmp = float(value)
  73.             return True
  74.         except ValueError:
  75.             return False
  76.  
  77. print("calc")
  78. calc =calc()
  79. calc.add_custom("operators.json")
  80. calc.print_oper()
  81. while True:
  82.     user = input("")
  83.     if user == 'q':
  84.         break
  85.     try:
  86.         calc.addto_q(user)
  87.     except calcerr as Err:
  88.         print('error', Err)
  89.     calc.print_q()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement