def check(x, ks): if not ks: return "empty sequence" if len(ks) > 100000: return "sequence too long." ka = ks.split(',') try: c = Calculator() for k in ka: c.click(k) if c.equal(x): return "passed" else: return "your result " + str( c.v) + " is not equal to " + str(x) + " (max_error = 1e-5)." except CalculatorUnknownKey: return "there're unknown key(s)." except ValueError: return "value error" except ZeroDivisionError: return "float division by zero" except Exception as e: print(e) return "system error"
def test_add(self): """ Test add function from class Calculator """ calcu = Calculator() result = calcu.add(3, 4) self.assertEqual(result, 7)
class CalculatorTest(unittest.TestCase): Calculator = Calculator() def test_add(self): self.assertEqual(4, self.Calculator.add(2, 2)) self.assertEqual(-2, self.Calculator.add(3, -5)) self.assertEqual(5.2, self.Calculator.add(3, 2.2)) def test_multiple(self): self.assertEqual(30, self.Calculator.multiple(5, 6)) self.assertEqual(-30, self.Calculator.multiple(-5, 6)) self.assertEqual(66.6, self.Calculator.multiple(3, 22.2)) def test_subtract(self): self.assertEqual(2, self.Calculator.subtract(4, 2)) self.assertEqual(-2, self.Calculator.subtract(2, 4)) self.assertEqual(6.7, self.Calculator.subtract(10, 3.3)) def test_divide(self): self.assertEqual(3, self.Calculator.divide(9, 3)) self.assertEqual(-3, self.Calculator.divide(-9, 3)) self.assertEqual(3.5, self.Calculator.divide(10.5, 3)) with self.assertRaises(ZeroDivisionError): self.Calculator.divide(2, 0) def test_x_and_y_are_not_numbers(self): self.assertRaises(ValueError, self.Calculator.add, 'two', 'three') self.assertRaises(ValueError, self.Calculator.add, 'two', 3) self.assertRaises(ValueError, self.Calculator.add, 2, 'three')
def process_request(self, message: str) -> str: """Processes HTTP request and returns response.""" parser = HTTPParser() response = HTTPResponse() calc = Calculator() request = parser.parse_request(message) # Invalid request (no expression sent) if (not request["params"] or "expression" not in request["params"]): print("Request is invalid. Missing parameters.") return response.build_response(status=406, data="-1") expression = request["params"]["expression"][0] print("{}Expression received:{} {}".format(bcolors.OKBLUE, bcolors.ENDC, expression)) # Evaluate the expression try: result = calc.evaluate(expression) print("{}Expression valid{}, result = {}".format( bcolors.OKGREEN, bcolors.ENDC, result)) return response.build_response(status=200, data=result) # Send error message if not valid except Exception as exc: print("{}An exception occurred:{} {} ({})".format( bcolors.FAIL, bcolors.ENDC, str(exc), type(exc).__name__)) return response.build_response(status=406, data="-1") return ""
def test_calc_stats(): values = [6, 9, 15, -2, 92, 11] calc = Calculator(values) assert calc.min == -2 assert calc.max == 92 assert calc.len == 6 assert calc.avg == 21.833333333333332
def repl(): calc = Calculator() while True: line = input('> ') try: print(calc.parse(line)) except SyntaxError as e: print(f'Syntax Error: {e.msg}')
def test_add(self, input_a, input_b, expected_result): print("test_add()") print("input_a", input_a) print("input_b", input_b) print("expected_result", expected_result) calc = Calculator() self.assertEqual(calc.add(input_a, input_b), expected_result)
def main(): """ main function realization of calculation of simples operations """ #pass print(__name__) calc = Calculator() print(calc.add())
def test_div(): calc = Calculator() assert calc.divide(5, 5) == 1 assert calc.divide(5, -5) == -1 assert calc.divide(5, 3) == 1 assert calc.divide(5, -3) == -2 assert calc.divide(5, '$') == 'invalid input' assert calc.divide(5, 0) == "Could not calcuate with given input"
def caclulate(): calc = Calculator() data = request.json try: equation = data['to_calculate'] result = calc.calculate(equation) except KeyError: return make_response({"error": "Missing required parameter 'to_caclulate'"}, 400) except: return make_response({"error": "Could not process equation"}, 400) return make_response({'result': result}, 200)
def calc(self, i): self.setWindowTitle("Calculator") if i == "=": formel = self.textLine.text().replace("=", "") clc = Calculator() try: r = clc.worker(formel) except ZeroDivisionError as ident: r = "" self.setWindowTitle("Zero Divison Error") self.textLine.setText(str(r))
def __init__(self, log, id, mode, memOC, coreUC, fanSpeed, steps, powerLimit, nbrOfShares, nbrOfDatapoints, marginInMH, powerCost, dollarPerMHash): # basics self.id = id self.log = log self.existing = False self.calc = Calculator(marginInMH, powerCost, dollarPerMHash) self.nvTool = NVTool(self.log, self.id) # overall Tuning Variables self.maxMemClockFound = False self.minCoreClockFound = False self.minPowerLimitFound = False # HW settings self.fanSpeed = fanSpeed self.memOC = memOC self.coreUC = coreUC self.powerLimit = powerLimit # tuning variables self.mode = mode self.steps = steps self.nbrOfShares = nbrOfShares self.margin = marginInMH self.nbrOfDatapoints = nbrOfDatapoints ## DATA TO CALCULATE ON ## # speed data via mining software self.currentMinerData = [] self.lastMinerData = [] self.overheatingThreshold = 5 self.thermalLimitReached = 0 # hw data of GPU via nvidia-smi self.currentHWData = [] self.lastHWData = [] ## HELPER VARIABLES ## self.dataSummary = [] self.maxSpeed = 0.0 self.defaultPowerLimit = 0.0 self.tuningTries = 0 ## TUNING PROCESS VARIABLES self.requiresRestart = False self.tuningDone = False self.found = self.Initialize()
class CalculatorTest(unittest.TestCase): # init class calculator = Calculator() # test adder def test_add(self): self.assertEqual(4, self.calculator.add(2, 2)) self.assertEqual(3.2, self.calculator.add(1, 2.2)) # test subtracter def test_minus(self): self.assertEqual(2, self.calculator.minus(3, 1)) self.assertEqual(-2, self.calculator.minus(1, 3)) # test mult def test_multiple(self): self.assertEqual(12, self.calculator.multiple(3, 4)) self.assertEqual(13.5, self.calculator.multiple(3, 4.5)) # test div def test_divide(self): self.assertEqual(3, self.calculator.divide(9, 3))
def test_substract(self): #result = Calculator().add(10,5) self.assertEqual(Calculator().minus(10, 5), 5) self.assertEqual(Calculator().minus(100, -5), 105)
#!/usr/bin/env python import codewars_test as test from calc import Calculator test.it("Tests") for key, val in { "127": 127, "2 + 3": 5, "2 - 3 - 4": -5, "10 * 5 / 2": 25, "2 / 2 + 3 * 4 - 6": 7, "2 + 3 * 4 / 3 - 6 / 3 * 3 + 8": 8, "1.1 + 2.2 + 3.3": 6.6, "1.1 * 2.2 * 3.3": 7.986 }.items(): actual = Calculator().evaluate(key) test.expect(isinstance(actual, (float, int)), "Your result should be a number, not: " + str(type(actual))) test.expect( abs(actual - val) < 1e-12, "Expected %s == %s, but got %s" % (key, val, actual))
def test_add(self): print('Test add(self, num1, num2) ') calc = Calculator() result = calc.add(10, 20) self.assertEqual(result, 30, 'Match expected value')
def test_div(self): print('Test div(self, num1, num2) ') calc = Calculator() result = calc.div(20, 5) self.assertEqual(result, 4, 'Match expected value')
def test_mul(self): print('Test mul(self, num1, num2) ') calc = Calculator() result = calc.mul(3, 6) self.assertEqual(result, 18, 'Match expected value')
char = ["+", "-", "/", "*"] newdata = [] cond = True while cond: for ch in char: #print(ch) newdata = data.split(ch) #print(len(newdata)) if len(newdata) == 2: cond = False break #print(newdata) for d in newdata: try: newdata[0] = float(newdata[0]) newdata[1] = float(newdata[1]) except ValueError: print('errror!') sys.exit() break else: c = Calculator(newdata[0], newdata[1]) actionlst = [c.Add(), c.Sub(), c.Mul(), c.Div()] cond = True for i, character in enumerate(char): #print("----") if ch == character: print(actionlst[i]) break
import unittest from calc import Calculator c = Calculator() class TestCalc1(unittest.TestCase): def testIncre(self): a = 19 b = 10 s = 9 sum = c.Incre(a, b) self.assertEquals(s, sum) def testIncre1(self): a = -19 b = 10 s = -29 sum = c.Incre(a, b) self.assertEquals(s, sum) def testIncre2(self): a = 19 b = -10 s = 29 sum = c.Incre(a, b) self.assertEquals(s, sum) def testIncre3(self): a = -19 b = -10 s = -9
def test_adds_single_number(self): calculator = Calculator() calculator.add(1) self.assertEqual(calculator.total, 1)
def process_data(self): calc = Calculator() plotter = Plotter() rpm_data = [] rpm_str = [] for filename in self.file_list: print(filename) _filename = filename.replace(".txt", "").split("/")[-1] day, time, id, iteration = _filename.split('_') df_list, ps_shapes, ps_annos, amp_shapes, amp_annos = calc.get_formatted_data( filename, 10000) filename = filename.replace(".txt", "") time_data = [] shapes = [] annos = [] df = pd.concat(df_list) if myConfig.get("t_c_dc"): time_data.append( plotter.make_graph(df, "time", "current_dc", " {}|{}".format(id, iteration))) time_data.append( plotter.make_graph(df, "time", "eff_curr_dc", " {}|{}".format(id, iteration))) if myConfig.get("t_v_dc"): time_data.append( plotter.make_graph(df, "time", "voltage_dc", " {}|{}".format(id, iteration))) time_data.append( plotter.make_graph(df, "time", "eff_volt_dc", " {}|{}".format(id, iteration))) if myConfig.get("t_c_ac_1"): time_data.append( plotter.make_graph(df, "time", "current_1", " {}|{}".format(id, iteration))) time_data.append( plotter.make_graph(df, "time", "eff_curr_1", " {}|{}".format(id, iteration))) if myConfig.get("t_c_ac_2"): time_data.append( plotter.make_graph(df, "time", "current_2", " {}|{}".format(id, iteration))) time_data.append( plotter.make_graph(df, "time", "eff_curr_2", " {}|{}".format(id, iteration))) if myConfig.get("t_c_ac_3"): time_data.append( plotter.make_graph(df, "time", "current_3", " {}|{}".format(id, iteration))) time_data.append( plotter.make_graph(df, "time", "eff_curr_3", " {}|{}".format(id, iteration))) if myConfig.get("t_v_ac_1"): time_data.append( plotter.make_graph(df, "time", "voltage_1", " {}|{}".format(id, iteration))) time_data.append( plotter.make_graph(df, "time", "eff_volt_1", " {}|{}".format(id, iteration))) if myConfig.get("t_v_ac_2"): time_data.append( plotter.make_graph(df, "time", "voltage_2", " {}|{}".format(id, iteration))) time_data.append( plotter.make_graph(df, "time", "eff_volt_2", " {}|{}".format(id, iteration))) if myConfig.get("t_v_ac_3"): time_data.append( plotter.make_graph(df, "time", "voltage_3", " {}|{}".format(id, iteration))) time_data.append( plotter.make_graph(df, "time", "eff_volt_3", " {}|{}".format(id, iteration))) if myConfig.get("t_rpm"): time_data.append( plotter.make_graph(df, "time", "rpm", " {}|{}".format(id, iteration))) if myConfig.get("t_torque"): time_data.append( plotter.make_graph(df, "time", "torque", " {}|{}".format(id, iteration))) if myConfig.get("t_input"): time_data.append( plotter.make_graph(df, "time", "input", " {}|{}".format(id, iteration))) if myConfig.get("t_output"): time_data.append( plotter.make_graph(df, "time", "output", " {}|{}".format(id, iteration))) if myConfig.get("t_output_dc"): time_data.append( plotter.make_graph(df, "time", "output_dc", " {}|{}".format(id, iteration))) if myConfig.get("t_eff"): time_data.append( plotter.make_graph(df, "time", "efficiency", " {}|{}".format(id, iteration))) if myConfig.get("t_amp"): shapes += amp_shapes annos += amp_annos if myConfig.get("t_ps"): shapes += ps_shapes annos += ps_annos if myConfig.get("t_volt_orig"): time_data.append( plotter.make_graph(df, "time", "_voltage_1", " {}|{}".format(id, iteration))) time_data.append( plotter.make_graph(df, "time", "_voltage_2", " {}|{}".format(id, iteration))) time_data.append( plotter.make_graph(df, "time", "_voltage_3", " {}|{}".format(id, iteration))) if myConfig.get("rpm_c_dc"): rpm_data.append( plotter.make_graph(df, "rpm", "current_dc", " {}|{}".format(id, iteration))) if myConfig.get("rpm_v_dc"): rpm_data.append( plotter.make_graph(df, "rpm", "voltage_dc", " {}|{}".format(id, iteration))) if myConfig.get("rpm_c_ac_1"): rpm_data.append( plotter.make_graph(df, "rpm", "current_1", " {}|{}".format(id, iteration))) if myConfig.get("rpm_c_ac_2"): rpm_data.append( plotter.make_graph(df, "rpm", "current_2", " {}|{}".format(id, iteration))) if myConfig.get("rpm_c_ac_3"): rpm_data.append( plotter.make_graph(df, "rpm", "current_3", " {}|{}".format(id, iteration))) if myConfig.get("rpm_v_ac_1"): rpm_data.append( plotter.make_graph(df, "rpm", "voltage_1", " {}|{}".format(id, iteration))) if myConfig.get("rpm_v_ac_2"): rpm_data.append( plotter.make_graph(df, "rpm", "voltage_2", " {}|{}".format(id, iteration))) if myConfig.get("rpm_v_ac_3"): rpm_data.append( plotter.make_graph(df, "rpm", "voltage_3", " {}|{}".format(id, iteration))) if myConfig.get("rpm_torque"): rpm_data.append( plotter.make_graph(df, "rpm", "torque", " {}|{}".format(id, iteration))) if myConfig.get("rpm_input"): rpm_data.append( plotter.make_graph(df, "rpm", "input", " {}|{}".format(id, iteration))) if myConfig.get("rpm_output"): rpm_data.append( plotter.make_graph(df, "rpm", "output", " {}|{}".format(id, iteration))) if myConfig.get("rpm_output_dc"): time_data.append( plotter.make_graph(df, "time", "output_dc", " {}|{}".format(id, iteration))) if myConfig.get("rpm_eff"): rpm_data.append( plotter.make_graph(df, "rpm", "efficiency", " {}|{}".format(id, iteration))) if time_data: i = 0 htmlname = _filename while True: if glob.glob("graphs/{}.{}".format(htmlname, ".html")): i += 1 htmlname = htmlname.replace("({})".format(i - 1), "") + "({})".format(i) continue break plotter.plot_it(time_data, "graphs/{}.{}".format(htmlname, "html"), shapes, annos) i = 0 excelname = _filename while True: if glob.glob("excels/{}.{}".format(excelname, "xlsx")): i += 1 excelname = excelname.replace("({})".format(i - 1), "") + "({})".format(i) continue break writer = pd.ExcelWriter("excels/{}.{}".format(excelname, "xlsx")) df.to_excel(writer) writer.save() rpm_str.append("{}-{}".format(id, iteration)) if rpm_data: i = 0 rpmname = "RPM " + "_".join(rpm_str) while True: if glob.glob("graphs/{}.".format(rpmname, "html")): i += 1 rpmname = rpmname.replace("({})".format(i - 1), "") + "({})".format(i) continue break plotter.plot_it(rpm_data, "graphs/{}.{}".format(rpmname, "html"), shapes, annos)
def setUp(self): self.C = Calculator()
def test_multi(self): #result = Calculator().add(10,5) self.assertEqual(Calculator().multi(10, 5), 50) self.assertEqual(Calculator().multi(100, -5), -500)
def test_divide_two_numbers(self): calculator = Calculator() calculator.divide(1,0)
def test_div(self): #result = Calculator().add(10,5) self.assertEqual(Calculator().div(10, 5), 2) self.assertEqual(Calculator().div(100, 2), 50)
from calc import Calculator from calc import Advanced_calculator my_calc = Calculator() print(my_calc.add(5,4)) print(my_calc.add(4,6)) print(my_calc.history) print(my_calc.result) my_calc.memory_save() print (my_calc.memory) print(my_calc.add(4,None)) print(my_calc.mul(5,4)) print(my_calc.div(7,6)) print(my_calc.sub(10,6)) print(my_calc.div(7,0)) print(my_calc.history) my_calc.clear_history() print(my_calc.history) my_calc.memory_save() print (my_calc.memory, " add") my_calc.clear_memory() print (my_calc.memory, " clear") print(my_calc.sub(10,15))
def test_add(self): #result = Calculator().add(10,5) self.assertEqual(Calculator().add(10, 5), 15) self.assertEqual(Calculator().add(100, -5), 95)
def setUp(self): self.calc = Calculator()
def test_sub(self): print('Test sub(self, num1, num2) ') calc = Calculator() result = calc.sub(20, 6) self.assertEqual(result, 14, 'Match expected value')