def default(self, ln): # Forward line starts with ! to system shell if ln.startswith('!'): os.system(ln[1:]) return c, arg, line = self.parseline(ln) # Check and run command aliases if c in self.__aliases: func = getattr(self, 'do_' + self.__aliases[c]) if func: return func(arg) # See if there is any handler for integer or floating point values if hasattr(self, 'int_handler'): try: return getattr(self, 'int_handler')(int(ln, 0)) except ValueError: pass if hasattr(self, 'float_handler'): try: return getattr(self, 'float_handler')(float(ln)) except ValueError: pass try: # See if this is a simple expression that can be evaluated print '= %s' % str(eval_expr.eval_expr(ln)) except: print_red('Unknown command: "%s". Try to type "help"' % ln)
def default(self, ln): # Forward line starts with ! to system shell if ln.startswith('!'): os.system(ln[1:]) return c, arg, line = self.parseline(ln) # Check and run command aliases if c in self.__aliases: func = getattr(self, 'do_'+self.__aliases[c]) if func: return func(arg) # See if there is any handler for integer or floating point values if hasattr(self, 'int_handler'): try: return getattr(self, 'int_handler')(int(ln, 0)) except ValueError: pass if hasattr(self, 'float_handler'): try: return getattr(self, 'float_handler')(float(ln)) except ValueError: pass try: # See if this is a simple expression that can be evaluated print '= %s'%str(eval_expr.eval_expr(ln)) except: print_red('Unknown command: "%s". Try to type "help"'%ln)
def calculate(self, query): """read and eval an expression in string. Return modified string back with the result.""" # remove stuff from RHS of = query = re.sub(r' =.*$', '', query) # substitute commands and variables here. processed = self.process_commands(query) processed = self.substitute_variables(processed) # eval ans = eval_expr(processed) # store this ans. self.VARS["_"] = str(ans) self.VARS["@ans"] = str(ans) if query == str(ans): return "<{0:g}>".format(ans) else: return "<{0} = {1:g}>".format(query, ans)