Advertisement
jensyao

Last working full calc iteration

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