diff --git a/handcalcs/handcalcs.py b/handcalcs/handcalcs.py index e7e3a26..0412d12 100644 --- a/handcalcs/handcalcs.py +++ b/handcalcs/handcalcs.py @@ -2089,7 +2089,6 @@ def swap_numeric_calcs( numeric_expression = copy.copy(calculation) functions_on_numeric_expressions = [ insert_parentheses, - swap_custom_symbols, swap_math_funcs, swap_chained_fracs, swap_frac_divs, @@ -2104,7 +2103,7 @@ def swap_numeric_calcs( flatten_deque, ] for function in functions_on_numeric_expressions: - if function is swap_values or function is swap_math_funcs: + if function in (swap_values, swap_math_funcs): numeric_expression = function( numeric_expression, calc_results, **config_options ) diff --git a/test_handcalcs/#test_handcalcs.py b/test_handcalcs/#test_handcalcs.py index c2f6d0b..17fdb87 100644 --- a/test_handcalcs/#test_handcalcs.py +++ b/test_handcalcs/#test_handcalcs.py @@ -17,22 +17,119 @@ CALC_4 = "H_test_test_a = sqrt((B+A+B+C+A+B)/(B+C+B+C+B) +B*A*B*C*B * sqrt(B + G_a_b + B + C + B + A + B + B + A))" CALC_5 = "beta_farb = min(G_a_b, H_test_test_a)" CALC_6 = "Omega_3 = (F_a / D_1)" -CALC_7 = ( - "eta_xy = 0.9 * (B - A)*((2*F_a)/G_a_b - 1)*((1 - H_test_test_a)/beta_farb)**2" -) +CALC_7 = "eta_xy = 0.9 * (B - A)*((2*F_a)/G_a_b - 1)*((1 - H_test_test_a)/beta_farb)**2" CALC_8 = "Psi_45 = (Omega_3 * pi**2 * A * (1/C))) / (2 * B**2) * (eta_xy + sqrt(A**2 + 4*((Omega_3 * G_a_b * beta_farb**2)/(pi**2*A*(1/C)) + G_a_b / A)))" CALC_9 = "if A <= B < C: Delta = (5*A*B**4)/(384*B*C**3); Gamma = 4 * 2" CALC_10 = "elif B > C: Delta = 5**(A/B)" CALC_11 = "C_p_i = 0.5 # Approximate factor" -CALC_1_D = deque(['D_1', '=', 'A', '+', 'B', '+', 'C']) -CALC_2_D = deque(['F_a', '=', 'sqrt', deque([deque(['B', '+', 'A']), '/', deque(['C', '+', 'A']), '+', 'B', '+', 'B', '+', 'C'])]) -CALC_3_D = deque(['G_a_b', '=', deque(['B', '+', 'B', '+', 'A']), '/', deque([deque(['C', '+', 'A', '+', 'A']), '/', deque(['C', '+', 'B', '+', 'sin', deque(['A'])])])]) -CALC_4_D = deque(['H_test_test_a', '=', 'sqrt', deque([deque(['B', '+', 'A', '+', 'B', '+', 'C', '+', 'A', '+', 'B']), '/', deque(['B', '+', 'C', '+', 'B', '+', 'C', '+', 'B']), '+', 'B', '*', 'A', '*', 'B', '*', 'C', '*', 'B', '*', 'sqrt', deque(['B', '+', 'G_a_b', '+', 'B', '+', 'C', '+', 'B', '+', 'A', '+', 'B', '+', 'B', '+', 'A'])])]) -CALC_5_D = deque(['beta_farb', '=', 'min', deque(['G_a_b', ',', 'H_test_test_a'])]) -CALC_6_D = deque(['Omega_3', '=', deque(['F_a', '/', 'D_1'])]) -CALC_7_D = deque(['eta_xy', '=', '0.9', '*', deque(['B', '-', 'A']), '*', deque([deque(['2', '*', 'F_a']), '/', 'G_a_b', '-', '1']), '*', deque([deque(['1', '-', 'H_test_test_a']), '/', 'beta_farb']), '**', '2']) -CALC_8_D = deque(['Psi_45', '=', deque(['Omega_3', '*', 'pi', '**', '2', '*', 'A', '*', deque(['1', '/', 'C'])])]) +CALC_1_D = deque(["D_1", "=", "A", "+", "B", "+", "C"]) +CALC_2_D = deque( + [ + "F_a", + "=", + "sqrt", + deque( + [ + deque(["B", "+", "A"]), + "/", + deque(["C", "+", "A"]), + "+", + "B", + "+", + "B", + "+", + "C", + ] + ), + ] +) +CALC_3_D = deque( + [ + "G_a_b", + "=", + deque(["B", "+", "B", "+", "A"]), + "/", + deque( + [ + deque(["C", "+", "A", "+", "A"]), + "/", + deque(["C", "+", "B", "+", "sin", deque(["A"])]), + ] + ), + ] +) +CALC_4_D = deque( + [ + "H_test_test_a", + "=", + "sqrt", + deque( + [ + deque(["B", "+", "A", "+", "B", "+", "C", "+", "A", "+", "B"]), + "/", + deque(["B", "+", "C", "+", "B", "+", "C", "+", "B"]), + "+", + "B", + "*", + "A", + "*", + "B", + "*", + "C", + "*", + "B", + "*", + "sqrt", + deque( + [ + "B", + "+", + "G_a_b", + "+", + "B", + "+", + "C", + "+", + "B", + "+", + "A", + "+", + "B", + "+", + "B", + "+", + "A", + ] + ), + ] + ), + ] +) +CALC_5_D = deque(["beta_farb", "=", "min", deque(["G_a_b", ",", "H_test_test_a"])]) +CALC_6_D = deque(["Omega_3", "=", deque(["F_a", "/", "D_1"])]) +CALC_7_D = deque( + [ + "eta_xy", + "=", + "0.9", + "*", + deque(["B", "-", "A"]), + "*", + deque([deque(["2", "*", "F_a"]), "/", "G_a_b", "-", "1"]), + "*", + deque([deque(["1", "-", "H_test_test_a"]), "/", "beta_farb"]), + "**", + "2", + ] +) +CALC_8_D = deque( + [ + "Psi_45", + "=", + deque(["Omega_3", "*", "pi", "**", "2", "*", "A", "*", deque(["1", "/", "C"])]), + ] +) CELL_1 = "\n".join([CALC_1, CALC_2, CALC_3, CALC_4]) CELL_2 = "\n".join([CALC_6, CALC_7, CALC_8]) @@ -41,8 +138,10 @@ CELL_5 = "\n".join([CALC_11]) CELL_6 = "#Output\nA\nB\nOmega_3" + def load_calcs(): from math import sqrt, sin, pi + A = 101.01 B = 2002.002 C = 30003.0003 @@ -54,11 +153,11 @@ def load_calcs(): CALC_5 = "beta_farb = min(G_a_b, H_test_test_a)" CALC_6 = "Omega_3 = (F_a / D_1)" CALC_7 = ( - "eta_xy = 0.9 * (B - A)*((2*F_a)/G_a_b - 1)*((1 - H_test_test_a)/beta_farb)**2" + "eta_xy = 0.9 * (B - A)*((2*F_a)/G_a_b - 1)*((1 - H_test_test_a)/beta_farb)**2" ) CALC_8 = "Psi_45 = (Omega_3 * pi**2 * A * (1/C)) / (2 * B**2) * (eta_xy + sqrt(A**2 + 4*((Omega_3 * G_a_b * beta_farb**2)/(pi**2*A*(1/C)) + G_a_b / A)))" CALC_9 = "if A <= B < C: Delta = (5*A*B**4)/(384*B*C**3); Gamma = 4 * 2" - #CALC_10 = "elif B > C: Delta = 5**(A/B)" + # CALC_10 = "elif B > C: Delta = 5**(A/B)" CALC_11 = "C_p_i = 0.5 # Approximate factor" CELL_1 = "\n".join([CALC_1, CALC_2, CALC_3, CALC_4]) @@ -100,59 +199,371 @@ def test_categorize_line(): assert hc.categorize_line(CALC_9, CALC_RESULTS) == results.CALC_9_categorized assert hc.categorize_line(CALC_10, CALC_RESULTS) == results.CALC_10_categorized + def test_categorize_raw_cell(): - assert str(hc.categorize_raw_cell(CELL_1, CALC_RESULTS)) == results.CELL_1_categorized - assert str(hc.categorize_raw_cell(CELL_2, CALC_RESULTS)) == results.CELL_2_categorized - assert str(hc.categorize_raw_cell(CELL_3, CALC_RESULTS)) == results.CELL_3_categorized - assert str(hc.categorize_raw_cell(CELL_4, CALC_RESULTS)) == results.CELL_4_categorized + assert ( + str(hc.categorize_raw_cell(CELL_1, CALC_RESULTS)) == results.CELL_1_categorized + ) + assert ( + str(hc.categorize_raw_cell(CELL_2, CALC_RESULTS)) == results.CELL_2_categorized + ) + assert ( + str(hc.categorize_raw_cell(CELL_3, CALC_RESULTS)) == results.CELL_3_categorized + ) + assert ( + str(hc.categorize_raw_cell(CELL_4, CALC_RESULTS)) == results.CELL_4_categorized + ) + def test_add_result_values_to_line(): - assert hc.add_result_values_to_line(hc.categorize_line(CALC_1, CALC_RESULTS), CALC_RESULTS) == results.CALC_1_w_result - assert hc.add_result_values_to_line(hc.categorize_line(CALC_2, CALC_RESULTS), CALC_RESULTS) == results.CALC_2_w_result - assert hc.add_result_values_to_line(hc.categorize_line(CALC_3, CALC_RESULTS), CALC_RESULTS) == results.CALC_3_w_result - assert hc.add_result_values_to_line(hc.categorize_line(CALC_4, CALC_RESULTS), CALC_RESULTS) == results.CALC_4_w_result - assert hc.add_result_values_to_line(hc.categorize_line(CALC_5, CALC_RESULTS), CALC_RESULTS) == results.CALC_5_w_result - assert hc.add_result_values_to_line(hc.categorize_line(CALC_6, CALC_RESULTS), CALC_RESULTS) == results.CALC_6_w_result - assert hc.add_result_values_to_line(hc.categorize_line(CALC_7, CALC_RESULTS), CALC_RESULTS) == results.CALC_7_w_result - assert hc.add_result_values_to_line(hc.categorize_line(CALC_8, CALC_RESULTS), CALC_RESULTS) == results.CALC_8_w_result - assert hc.add_result_values_to_line(hc.categorize_line(CALC_9, CALC_RESULTS), CALC_RESULTS) == results.CALC_9_w_result - assert hc.add_result_values_to_line(hc.categorize_line(CALC_10, CALC_RESULTS), CALC_RESULTS) == results.CALC_10_w_result + assert ( + hc.add_result_values_to_line( + hc.categorize_line(CALC_1, CALC_RESULTS), CALC_RESULTS + ) + == results.CALC_1_w_result + ) + assert ( + hc.add_result_values_to_line( + hc.categorize_line(CALC_2, CALC_RESULTS), CALC_RESULTS + ) + == results.CALC_2_w_result + ) + assert ( + hc.add_result_values_to_line( + hc.categorize_line(CALC_3, CALC_RESULTS), CALC_RESULTS + ) + == results.CALC_3_w_result + ) + assert ( + hc.add_result_values_to_line( + hc.categorize_line(CALC_4, CALC_RESULTS), CALC_RESULTS + ) + == results.CALC_4_w_result + ) + assert ( + hc.add_result_values_to_line( + hc.categorize_line(CALC_5, CALC_RESULTS), CALC_RESULTS + ) + == results.CALC_5_w_result + ) + assert ( + hc.add_result_values_to_line( + hc.categorize_line(CALC_6, CALC_RESULTS), CALC_RESULTS + ) + == results.CALC_6_w_result + ) + assert ( + hc.add_result_values_to_line( + hc.categorize_line(CALC_7, CALC_RESULTS), CALC_RESULTS + ) + == results.CALC_7_w_result + ) + assert ( + hc.add_result_values_to_line( + hc.categorize_line(CALC_8, CALC_RESULTS), CALC_RESULTS + ) + == results.CALC_8_w_result + ) + assert ( + hc.add_result_values_to_line( + hc.categorize_line(CALC_9, CALC_RESULTS), CALC_RESULTS + ) + == results.CALC_9_w_result + ) + assert ( + hc.add_result_values_to_line( + hc.categorize_line(CALC_10, CALC_RESULTS), CALC_RESULTS + ) + == results.CALC_10_w_result + ) + def test_convert_cell(): - assert sha256(str(hc.convert_cell(hc.categorize_lines(hc.categorize_raw_cell(CELL_1, CALC_RESULTS)))).encode('utf-8')).hexdigest() == '59bf549bc20720192d390989de803e79e1bd39c0062c14f0360ae1b4d4c91c58' - assert sha256(str(hc.convert_cell(hc.categorize_lines(hc.categorize_raw_cell(CELL_2, CALC_RESULTS)))).encode('utf-8')).hexdigest() == '2c6e52d8a380eddbb0c82da8d2eb394635010fcf1e8a28b40dae9f67ac333ad5' - assert sha256(str(hc.convert_cell(hc.categorize_lines(hc.categorize_raw_cell(CELL_3, CALC_RESULTS)))).encode('utf-8')).hexdigest() == 'd78a388ac5289337a7786cc76e54f713de22e23e8dc9fcac055ebcac7eea7b32' - + assert ( + sha256( + str( + hc.convert_cell( + hc.categorize_lines(hc.categorize_raw_cell(CELL_1, CALC_RESULTS)) + ) + ).encode("utf-8") + ).hexdigest() + == "59bf549bc20720192d390989de803e79e1bd39c0062c14f0360ae1b4d4c91c58" + ) + assert ( + sha256( + str( + hc.convert_cell( + hc.categorize_lines(hc.categorize_raw_cell(CELL_2, CALC_RESULTS)) + ) + ).encode("utf-8") + ).hexdigest() + == "2c6e52d8a380eddbb0c82da8d2eb394635010fcf1e8a28b40dae9f67ac333ad5" + ) + assert ( + sha256( + str( + hc.convert_cell( + hc.categorize_lines(hc.categorize_raw_cell(CELL_3, CALC_RESULTS)) + ) + ).encode("utf-8") + ).hexdigest() + == "d78a388ac5289337a7786cc76e54f713de22e23e8dc9fcac055ebcac7eea7b32" + ) + def test_format_cell(): - assert sha256(str(hc.format_cell(hc.convert_cell(hc.categorize_lines(hc.categorize_raw_cell(CELL_1, CALC_RESULTS))))).encode('utf-8')).hexdigest() == '213e818835bd2414cd04a7ada9e5a581b0e47cbce744d6c7b62c9d3e2d709382' - assert sha256(str(hc.format_cell(hc.convert_cell(hc.categorize_lines(hc.categorize_raw_cell(CELL_2, CALC_RESULTS))))).encode('utf-8')).hexdigest() == '4aa8504a1d296652d35a36ead985d9b3f2e50044eb12a5bf579ea45c4dc3e914' - assert sha256(str(hc.format_cell(hc.convert_cell(hc.categorize_lines(hc.categorize_raw_cell(CELL_4, CALC_RESULTS))))).encode('utf-8')).hexdigest() == 'aa7a9f4858866bf27a19a7dad74892aa8e211a89dac6d45379b05ec79e656f6b' - assert sha256(str(hc.format_cell(hc.convert_cell(hc.categorize_lines(hc.categorize_raw_cell(CELL_3, CALC_RESULTS))))).encode('utf-8')).hexdigest() == '49be6186dfcd125f5b6b948e996a1d08200cdfce3671ac1552c8b987be709119' - assert sha256(str(hc.format_cell(hc.convert_cell(hc.categorize_lines(hc.categorize_raw_cell(CELL_5, CALC_RESULTS))))).encode('utf-8')).hexdigest() == 'e61f23b1d91f107a2ec7ad501563643eb8dfc2f7d6346b926e7ed9c880835fa8' - assert sha256(str(hc.format_cell(hc.convert_cell(hc.categorize_lines(hc.categorize_raw_cell(CELL_6, CALC_RESULTS))))).encode('utf-8')).hexdigest() == '3e75283e53f1328c8c461e270cdb8858d58044b2bcc65ad8ca19cdbce05d0331' + assert ( + sha256( + str( + hc.format_cell( + hc.convert_cell( + hc.categorize_lines( + hc.categorize_raw_cell(CELL_1, CALC_RESULTS) + ) + ) + ) + ).encode("utf-8") + ).hexdigest() + == "213e818835bd2414cd04a7ada9e5a581b0e47cbce744d6c7b62c9d3e2d709382" + ) + assert ( + sha256( + str( + hc.format_cell( + hc.convert_cell( + hc.categorize_lines( + hc.categorize_raw_cell(CELL_2, CALC_RESULTS) + ) + ) + ) + ).encode("utf-8") + ).hexdigest() + == "4aa8504a1d296652d35a36ead985d9b3f2e50044eb12a5bf579ea45c4dc3e914" + ) + assert ( + sha256( + str( + hc.format_cell( + hc.convert_cell( + hc.categorize_lines( + hc.categorize_raw_cell(CELL_4, CALC_RESULTS) + ) + ) + ) + ).encode("utf-8") + ).hexdigest() + == "aa7a9f4858866bf27a19a7dad74892aa8e211a89dac6d45379b05ec79e656f6b" + ) + assert ( + sha256( + str( + hc.format_cell( + hc.convert_cell( + hc.categorize_lines( + hc.categorize_raw_cell(CELL_3, CALC_RESULTS) + ) + ) + ) + ).encode("utf-8") + ).hexdigest() + == "49be6186dfcd125f5b6b948e996a1d08200cdfce3671ac1552c8b987be709119" + ) + assert ( + sha256( + str( + hc.format_cell( + hc.convert_cell( + hc.categorize_lines( + hc.categorize_raw_cell(CELL_5, CALC_RESULTS) + ) + ) + ) + ).encode("utf-8") + ).hexdigest() + == "e61f23b1d91f107a2ec7ad501563643eb8dfc2f7d6346b926e7ed9c880835fa8" + ) + assert ( + sha256( + str( + hc.format_cell( + hc.convert_cell( + hc.categorize_lines( + hc.categorize_raw_cell(CELL_6, CALC_RESULTS) + ) + ) + ) + ).encode("utf-8") + ).hexdigest() + == "3e75283e53f1328c8c461e270cdb8858d58044b2bcc65ad8ca19cdbce05d0331" + ) + def test_swap_values(): - assert hc.swap_values(deque(["=", "A", "+", 23]), {"A": 43}) == deque(["=", 43, "+", 23]) - assert hc.swap_values(deque(["eta", "=", "beta", "+", "theta"]), {"eta": 3, "beta": 2, "theta": 1}) == deque([3, "=", 2, "+", 1]) + assert hc.swap_values(deque(["=", "A", "+", 23]), {"A": 43}) == deque( + ["=", 43, "+", 23] + ) + assert hc.swap_values( + deque(["eta", "=", "beta", "+", "theta"]), {"eta": 3, "beta": 2, "theta": 1} + ) == deque([3, "=", 2, "+", 1]) + def test_swap_for_greek(): - assert hc.swap_for_greek(deque(["eta", "=", "beta", "+", "theta"])) == deque(["\\eta", "=", "\\beta", "+", "\\theta"]) - assert hc.swap_for_greek(deque(["M_r", "=", "phi", "\\cdot", deque(["psi", "\\cdot", "F_y"])])) == deque(["M_r", "=", "\\phi", "\\cdot", deque(["\\psi", "\\cdot", "F_y"])]) + assert hc.swap_for_greek(deque(["eta", "=", "beta", "+", "theta"])) == deque( + ["\\eta", "=", "\\beta", "+", "\\theta"] + ) + assert hc.swap_for_greek( + deque(["M_r", "=", "phi", "\\cdot", deque(["psi", "\\cdot", "F_y"])]) + ) == deque(["M_r", "=", "\\phi", "\\cdot", deque(["\\psi", "\\cdot", "F_y"])]) assert hc.swap_for_greek(deque(["lamb", "=", 3])) == deque(["\\lambda", "=", 3]) def test_swap_superscripts(): - assert hc.swap_superscripts(CALC_8_D) == deque(['Psi_45', '=', deque(['Omega_3', '*', '\\left(', 'pi', '\\right)', '^{', '2', '}', '*', 'A', '*', deque(['1', '/', 'C'])])]) - assert hc.swap_superscripts(CALC_7_D) == deque(['eta_xy', '=', '0.9', '*', deque(['B', '-', 'A']), '*', deque([deque(['2', '*', 'F_a']), '/', 'G_a_b', '-', '1']), '*', deque([deque(['1', '-', 'H_test_test_a']), '/', 'beta_farb']), '^{', '2', '}']) + assert hc.swap_superscripts(CALC_8_D) == deque( + [ + "Psi_45", + "=", + deque( + [ + "Omega_3", + "*", + "\\left(", + "pi", + "\\right)", + "^{", + "2", + "}", + "*", + "A", + "*", + deque(["1", "/", "C"]), + ] + ), + ] + ) + assert hc.swap_superscripts(CALC_7_D) == deque( + [ + "eta_xy", + "=", + "0.9", + "*", + deque(["B", "-", "A"]), + "*", + deque([deque(["2", "*", "F_a"]), "/", "G_a_b", "-", "1"]), + "*", + deque([deque(["1", "-", "H_test_test_a"]), "/", "beta_farb"]), + "^{", + "2", + "}", + ] + ) def test_swap_py_operators(): - assert hc.swap_py_operators(CALC_4_D) == deque(['H_test_test_a', '=', 'sqrt', deque([deque(['B', '+', 'A', '+', 'B', '+', 'C', '+', 'A', '+', 'B']), '/', deque(['B', '+', 'C', '+', 'B', '+', 'C', '+', 'B']), '+', 'B', '\\cdot', 'A', '\\cdot', 'B', '\\cdot', 'C', '\\cdot', 'B', '\\cdot', 'sqrt', deque(['B', '+', 'G_a_b', '+', 'B', '+', 'C', '+', 'B', '+', 'A', '+', 'B', '+', 'B', '+', 'A'])])]) - assert hc.swap_py_operators(CALC_7_D) == deque(['eta_xy', '=', '0.9', '\\cdot', deque(['B', '-', 'A']), '\\cdot', deque([deque(['2', '\\cdot', 'F_a']), '/', 'G_a_b', '-', '1']), '\\cdot', deque([deque(['1', '-', 'H_test_test_a']), '/', 'beta_farb']), '**', '2']) - assert hc.swap_py_operators(CALC_8_D) == deque(['Psi_45', '=', deque(['Omega_3', '\\cdot', 'pi', '**', '2', '\\cdot', 'A', '\\cdot', deque(['1', '/', 'C'])])]) + assert hc.swap_py_operators(CALC_4_D) == deque( + [ + "H_test_test_a", + "=", + "sqrt", + deque( + [ + deque(["B", "+", "A", "+", "B", "+", "C", "+", "A", "+", "B"]), + "/", + deque(["B", "+", "C", "+", "B", "+", "C", "+", "B"]), + "+", + "B", + "\\cdot", + "A", + "\\cdot", + "B", + "\\cdot", + "C", + "\\cdot", + "B", + "\\cdot", + "sqrt", + deque( + [ + "B", + "+", + "G_a_b", + "+", + "B", + "+", + "C", + "+", + "B", + "+", + "A", + "+", + "B", + "+", + "B", + "+", + "A", + ] + ), + ] + ), + ] + ) + assert hc.swap_py_operators(CALC_7_D) == deque( + [ + "eta_xy", + "=", + "0.9", + "\\cdot", + deque(["B", "-", "A"]), + "\\cdot", + deque([deque(["2", "\\cdot", "F_a"]), "/", "G_a_b", "-", "1"]), + "\\cdot", + deque([deque(["1", "-", "H_test_test_a"]), "/", "beta_farb"]), + "**", + "2", + ] + ) + assert hc.swap_py_operators(CALC_8_D) == deque( + [ + "Psi_45", + "=", + deque( + [ + "Omega_3", + "\\cdot", + "pi", + "**", + "2", + "\\cdot", + "A", + "\\cdot", + deque(["1", "/", "C"]), + ] + ), + ] + ) # TODO: Add test for swapping % and \bmod + def swap_math_funcs(): - assert hc.swap_math_funcs(CALC_5_D) == deque(['beta_farb', '=', '\\operatorname{min}', deque(['G_a_b', ',', 'H_test_test_a'])]) - assert hc.swap_math_funcs(CALC_3_D) == deque(['G_a_b', '=', deque(['B', '+', 'B', '+', 'A']), '/', deque([deque(['C', '+', 'A', '+', 'A']), '/', deque(['C', '+', 'B', '+', '\\sin', deque(['A'])])])]) + assert hc.swap_math_funcs(CALC_5_D) == deque( + [ + "beta_farb", + "=", + "\\operatorname{min}", + deque(["G_a_b", ",", "H_test_test_a"]), + ] + ) + assert hc.swap_math_funcs(CALC_3_D) == deque( + [ + "G_a_b", + "=", + deque(["B", "+", "B", "+", "A"]), + "/", + deque( + [ + deque(["C", "+", "A", "+", "A"]), + "/", + deque(["C", "+", "B", "+", "\\sin", deque(["A"])]), + ] + ), + ] + ) diff --git a/test_handcalcs/cell_1.py b/test_handcalcs/cell_1.py index e24beb0..bad8232 100644 --- a/test_handcalcs/cell_1.py +++ b/test_handcalcs/cell_1.py @@ -1,10 +1,6 @@ -a = 2 # Comment -y = (a+4) # Comment -alpha_eta_psi = 4 / (y**(a + 1)) # Comment +a = 2 # Comment +y = (a + 4) # Comment +alpha_eta_psi = 4 / (y ** (a + 1)) # Comment alpha_eta_psi calc_results = globals() - - - - diff --git a/test_handcalcs/cell_10.py b/test_handcalcs/cell_10.py index 5e79373..cc81f66 100644 --- a/test_handcalcs/cell_10.py +++ b/test_handcalcs/cell_10.py @@ -1,8 +1,8 @@ from math import floor, sin, log, log2, log10, sqrt -mu = 45 + sin(34 + 2)/2 # Comment +mu = 45 + sin(34 + 2) / 2 # Comment tau = sin(log2(log(3, 9))) eta = sqrt(1 / log10(6) / log(32)) -kappa = floor(23/4.5) # Last comment +kappa = floor(23 / 4.5) # Last comment -calc_results = globals() \ No newline at end of file +calc_results = globals() diff --git a/test_handcalcs/cell_11.py b/test_handcalcs/cell_11.py index 918d1f7..543299f 100644 --- a/test_handcalcs/cell_11.py +++ b/test_handcalcs/cell_11.py @@ -9,4 +9,4 @@ lamb = sqrt(f_y / F_e) P_r = phi * area * f_y * ((1 + lamb ** (2 * n)) ** (-1 / n)) -calc_results = globals() \ No newline at end of file +calc_results = globals() diff --git a/test_handcalcs/cell_2.py b/test_handcalcs/cell_2.py index ed558b3..4118f39 100644 --- a/test_handcalcs/cell_2.py +++ b/test_handcalcs/cell_2.py @@ -1,9 +1,5 @@ x = 2 -if x < 1: b = x # Comment -elif x >= 1: b = x*1; c = b # Comment +if x < 1: b = x # Comment +elif x >= 1: b = x * 1; c = b # Comment calc_results = globals() - - - - diff --git a/test_handcalcs/cell_2b.py b/test_handcalcs/cell_2b.py index f5aecda..2304dae 100644 --- a/test_handcalcs/cell_2b.py +++ b/test_handcalcs/cell_2b.py @@ -1,10 +1,6 @@ x = 10 -if x < 1: b = x # Comment -elif 1 < x < 5: b = x*1; c = b # Comment -else: b = x*1; c = b #Comment +if x < 1: b = x # Comment +elif 1 < x < 5: b = x * 1; c = b # Comment +else: b = x * 1; c = b # Comment calc_results = globals() - - - - diff --git a/test_handcalcs/cell_3.py b/test_handcalcs/cell_3.py index be8bb12..265dc7b 100644 --- a/test_handcalcs/cell_3.py +++ b/test_handcalcs/cell_3.py @@ -1,4 +1,6 @@ from math import ceil, sqrt + + def quad(*args, **kwargs): """ This is a mocked integration function to mimic @@ -7,19 +9,17 @@ def quad(*args, **kwargs): """ return (42, 0.001) + def F(x): - return x**2 + 3*x + return x**2 + 3 * x + y = -2 b = 3 -c = 4 +c = 4 alpha_eta_psi = 23 d = sqrt(1 / b / c) f = ceil((alpha_eta_psi + 1) % 2) -g = quad(F,y,b) +g = quad(F, y, b) calc_results = globals() - - - - diff --git a/test_handcalcs/cell_4.py b/test_handcalcs/cell_4.py index 3c4b8c2..1037da0 100644 --- a/test_handcalcs/cell_4.py +++ b/test_handcalcs/cell_4.py @@ -1,11 +1,7 @@ # Parameters -a = 2 # Comment +a = 2 # Comment b = 3 c = 5 -y = a+4 # Comment +y = a + 4 # Comment calc_results = globals() - - - - diff --git a/test_handcalcs/cell_5.py b/test_handcalcs/cell_5.py index ae80d92..1998120 100644 --- a/test_handcalcs/cell_5.py +++ b/test_handcalcs/cell_5.py @@ -1,13 +1,9 @@ from math import sqrt, sin, asin -a = 10000001 # Comment +a = 10000001 # Comment b = 20000002 c = 30000003 x = 5 -y = sqrt(a / b) + asin(sin(b / c)) + (a/b)**(0.5) + sqrt((a*b + b*c)/(b**2)) + sin(a/b) # Comment +y = sqrt(a / b)+ asin(sin(b / c)) + (a / b) ** (0.5) + sqrt((a * b + b * c) / (b**2)) + sin(a / b) # Comment calc_results = globals() - - - - diff --git a/test_handcalcs/cell_6.py b/test_handcalcs/cell_6.py index c4ac657..bb1a1ab 100644 --- a/test_handcalcs/cell_6.py +++ b/test_handcalcs/cell_6.py @@ -1,10 +1,6 @@ # Long a = 2 -b = 3*a -y = 2*a+4+3 +b = 3 * a +y = 2 * a + 4 + 3 calc_results = globals() - - - - diff --git a/test_handcalcs/cell_7.py b/test_handcalcs/cell_7.py index 1ab4f99..2ec8c64 100644 --- a/test_handcalcs/cell_7.py +++ b/test_handcalcs/cell_7.py @@ -1,4 +1,3 @@ - from math import asin, sin, sqrt # Short @@ -6,12 +5,8 @@ b = 43 c = 52 -f = c / a +b # Comment -g = c*f / a # Comment -d = sqrt(a / b) + asin(sin(b / c)) + (a/b)**(0.5) + sqrt((a*b + b*c)/(b**2)) + sin(a/b) # Comment +f = c / a + b # Comment +g = c * f / a # Comment +d = sqrt(a / b) + asin(sin(b / c)) + (a / b) ** (0.5) + sqrt((a * b + b * c) / (b**2)) + sin(a / b) # Comment calc_results = globals() - - - - diff --git a/test_handcalcs/cell_7b.py b/test_handcalcs/cell_7b.py index dad4852..53a7b32 100644 --- a/test_handcalcs/cell_7b.py +++ b/test_handcalcs/cell_7b.py @@ -1,17 +1,12 @@ - from math import asin, sin, sqrt # Short alpha_zeta = 9.84e-1 -b_prime_c= 43 -causal = 4.2+3.2j +b_prime_c = 43 +causal = 4.2 + 3.2j -f = causal/ alpha_zeta+b_prime_c# Comment -g = causal*f / alpha_zeta# Comment -d = sqrt(alpha_zeta/ b_prime_c) + sum((1,2,3)) + (alpha_zeta/b_prime_c)**(0.5) + sqrt((alpha_zeta*b_prime_c+ b_prime_c)/(1.23e3**2)) + sin(alpha_zeta/b_prime_c) # Comment +f = causal / alpha_zeta + b_prime_c # Comment +g = causal * f / alpha_zeta # Comment +d = sqrt(alpha_zeta / b_prime_c) + sum((1, 2, 3)) + (alpha_zeta / b_prime_c) ** (0.5) + sqrt((alpha_zeta * b_prime_c + b_prime_c) / (1.23e3**2)) + sin(alpha_zeta / b_prime_c) # Comment calc_results = globals() - - - - diff --git a/test_handcalcs/cell_8.py b/test_handcalcs/cell_8.py index 8e2b62e..15cb0ae 100644 --- a/test_handcalcs/cell_8.py +++ b/test_handcalcs/cell_8.py @@ -1,17 +1,11 @@ - from math import asin, sin, sqrt -# Symbolic a = 23 V_dot = 43 c = 52 -f = c / a +V_dot # Comment -g = c*f / a # Comment -d = sqrt(a / V_dot + asin(sin(V_dot / c)) + (a/V_dot)**(0.5) + sqrt((a*V_dot + V_dot*c)/(V_dot**2)) + sin(a/V_dot)) # Comment +f = c / a + V_dot # Comment +g = c * f / a # Comment +d = sqrt(a / V_dot+ asin(sin(V_dot / c))+ (a / V_dot) ** (0.5)+ sqrt((a * V_dot + V_dot * c) / (V_dot**2))+ sin(a / V_dot)) # Comment calc_results = globals() - - - - diff --git a/test_handcalcs/cell_9.py b/test_handcalcs/cell_9.py index bf9b88f..ca2f437 100644 --- a/test_handcalcs/cell_9.py +++ b/test_handcalcs/cell_9.py @@ -1,9 +1,9 @@ from math import sin, atan mu = 0.44 -CritSeg = 1.5 # sendo extramemente +CritSeg = 1.5 # sendo extramemente Delta_h = 9.641 -Raio = ((200/2)) # Config -Raio_Minimo = CritSeg * Delta_h/(sin(atan(mu+1)+1))**2 +Raio = (200 / 2) # Config +Raio_Minimo = CritSeg * Delta_h / (sin(atan(mu + 1) + 1)) ** 2 -calc_results = globals() \ No newline at end of file +calc_results = globals() diff --git a/test_handcalcs/error_cell.py b/test_handcalcs/error_cell.py index 2ca1855..49dd0c5 100644 --- a/test_handcalcs/error_cell.py +++ b/test_handcalcs/error_cell.py @@ -3,7 +3,3 @@ a > b calc_results = globals() - - - - diff --git a/test_handcalcs/function_mocks.py b/test_handcalcs/function_mocks.py index 0ed7148..529fec7 100644 --- a/test_handcalcs/function_mocks.py +++ b/test_handcalcs/function_mocks.py @@ -1,8 +1,9 @@ def euler__buckling__load(E, I, k, L): return 4500 - + + area = 1000 -E=200e3 +E = 200e3 I_x = 300e6 I_y = 150e6 k_x = 1.0 @@ -11,4 +12,3 @@ def euler__buckling__load(E, I, k, L): f_y = 350 phi = 0.9 n = 1.34 - diff --git a/test_handcalcs/test_handcalcs_file.py b/test_handcalcs/test_handcalcs_file.py index 28c2d73..518fbac 100644 --- a/test_handcalcs/test_handcalcs_file.py +++ b/test_handcalcs/test_handcalcs_file.py @@ -16,7 +16,11 @@ import inspect from collections import deque -from handcalcs.exporters import LatexHideInputExporter, HTMLHideInputExporter, PDFHideInputExporter +from handcalcs.exporters import ( + LatexHideInputExporter, + HTMLHideInputExporter, + PDFHideInputExporter, +) import handcalcs import pathlib @@ -64,16 +68,15 @@ "greek_exclusions": [], "param_columns": 3, "preferred_string_formatter": "L", - "custom_symbols": {"V_dot": "\\dot{V}"} + "custom_symbols": {"V_dot": "\\dot{V}"}, } config_underscore_spaces = config_options.copy() -config_underscore_spaces['underscore_subscripts'] = False +config_underscore_spaces["underscore_subscripts"] = False # TODO: Integration tests with nested log, nested exponents - class MockLatexObj1: def __init__(self, s: str): self.s = s @@ -96,7 +99,7 @@ def _repr_latex_(self): def int_func(x): - return x ** 2 + 3 * x + return x**2 + 3 * x def quad(f, a, b): @@ -118,14 +121,18 @@ def remove_imports_defs_and_globals(source: str): acc = [] doc_string = False for line in source_lines: - if (not doc_string - and line.lstrip(' \t').startswith('"""') - and line.lstrip(' \t').rstrip().endswith('"""',3)): + if ( + not doc_string + and line.lstrip(" \t").startswith('"""') + and line.lstrip(" \t").rstrip().endswith('"""', 3) + ): doc_string = False continue - elif (not doc_string - and line.lstrip(' \t').startswith('"""') - and not line.lstrip(' \t').rstrip().endswith('"""',3)): + elif ( + not doc_string + and line.lstrip(" \t").startswith('"""') + and not line.lstrip(" \t").rstrip().endswith('"""', 3) + ): doc_string = True continue elif doc_string and '"""' in line: @@ -143,6 +150,7 @@ def remove_imports_defs_and_globals(source: str): acc.append(line) return "\n".join(acc) + @handcalc() def func_1(x, y): a = 2 * x @@ -160,6 +168,7 @@ def func_2(x, y): b = 3 * a + y return locals() # not necessary, but allowed + @handcalc(jupyter_display=True) def func_3(x, y): """My single line docstring""" @@ -167,6 +176,7 @@ def func_3(x, y): b = 3 * a + y return locals() # not necessary, but allowed + line_args = {"override": "", "precision": None, "sci_not": False} line_args_params = {"override": "params", "precision": None, "sci_not": False} line_args_symbolic = {"override": "symbolic", "precision": None, "sci_not": False} @@ -194,48 +204,76 @@ def func_3(x, y): error_cell_source = remove_imports_defs_and_globals(inspect.getsource(error_cell)) cell_1_renderer = handcalcs.handcalcs.LatexRenderer( - cell_1_source, cell_1.calc_results, line_args_1, + cell_1_source, + cell_1.calc_results, + line_args_1, ) cell_2_renderer = handcalcs.handcalcs.LatexRenderer( - cell_2_source, cell_2.calc_results, line_args_2, + cell_2_source, + cell_2.calc_results, + line_args_2, ) cell_2b_renderer = handcalcs.handcalcs.LatexRenderer( - cell_2b_source, cell_2b.calc_results, line_args_2, + cell_2b_source, + cell_2b.calc_results, + line_args_2, ) cell_3_renderer = handcalcs.handcalcs.LatexRenderer( - cell_3_source, cell_3.calc_results, line_args_3, + cell_3_source, + cell_3.calc_results, + line_args_3, ) cell_4_renderer = handcalcs.handcalcs.LatexRenderer( - cell_4_source, cell_4.calc_results, line_args_params, + cell_4_source, + cell_4.calc_results, + line_args_params, ) cell_5_renderer = handcalcs.handcalcs.LatexRenderer( - cell_5_source, cell_5.calc_results, line_args, + cell_5_source, + cell_5.calc_results, + line_args, ) cell_6_renderer = handcalcs.handcalcs.LatexRenderer( - cell_6_source, cell_6.calc_results, line_args, + cell_6_source, + cell_6.calc_results, + line_args, ) cell_7_renderer = handcalcs.handcalcs.LatexRenderer( - cell_7_source, cell_7.calc_results, line_args_long, + cell_7_source, + cell_7.calc_results, + line_args_long, ) cell_7b_renderer = handcalcs.handcalcs.LatexRenderer( - cell_7b_source, cell_7b.calc_results, line_args_short, + cell_7b_source, + cell_7b.calc_results, + line_args_short, ) cell_8_renderer = handcalcs.handcalcs.LatexRenderer( - cell_8_source, cell_8.calc_results, line_args_symbolic, + cell_8_source, + cell_8.calc_results, + line_args, ) cell_9_renderer = handcalcs.handcalcs.LatexRenderer( - cell_9_source, cell_9.calc_results, line_args_symbolic, + cell_9_source, + cell_9.calc_results, + line_args_symbolic, ) cell_10_renderer = handcalcs.handcalcs.LatexRenderer( - cell_10_source, cell_10.calc_results, line_args_10, + cell_10_source, + cell_10.calc_results, + line_args_10, ) cell_11_renderer = handcalcs.handcalcs.LatexRenderer( - cell_11_source, cell_11.calc_results, line_args, + cell_11_source, + cell_11.calc_results, + line_args, ) # error_cell_renderer = handcalcs.handcalcs.LatexRenderer( error_cell_renderer = handcalcs.handcalcs.LatexRenderer( - error_cell_source, error_cell.calc_results, line_args, + error_cell_source, + error_cell.calc_results, + line_args, ) # Integration tests @@ -245,7 +283,7 @@ def func_3(x, y): def test_integration(): assert ( cell_1_renderer.render(config_options=config_options) - == '\\[\n\\begin{aligned}\na &= 2 \\; \\;\\textrm{(Comment)}\n\\\\[10pt]\ny &= 6 \\; \\;\\textrm{(Comment)}\n\\\\[10pt]\n\\alpha_{\\eta_{\\psi}} &= \\frac{ 4 }{ \\left( y \\right) ^{ \\left( a + 1 \\right) } } = \\frac{ 4 }{ \\left( 6 \\right) ^{ \\left( 2 + 1 \\right) } } &= 1.85185 \\times 10 ^ {-2} \\; \\;\\textrm{(Comment)}\n\\\\[10pt]\n\\alpha_{\\eta_{\\psi}} &= 1.85185 \\times 10 ^ {-2} \\; \n\\end{aligned}\n\\]' + == "\\[\n\\begin{aligned}\na &= 2 \\; \\;\\textrm{(Comment)}\n\\\\[10pt]\ny &= 6 \\; \\;\\textrm{(Comment)}\n\\\\[10pt]\n\\alpha_{\\eta_{\\psi}} &= \\frac{ 4 }{ \\left( y \\right) ^{ \\left( a + 1 \\right) } } = \\frac{ 4 }{ \\left( 6 \\right) ^{ \\left( 2 + 1 \\right) } } &= 1.85185 \\times 10 ^ {-2} \\; \\;\\textrm{(Comment)}\n\\\\[10pt]\n\\alpha_{\\eta_{\\psi}} &= 1.85185 \\times 10 ^ {-2} \\; \n\\end{aligned}\n\\]" ) assert ( cell_2_renderer.render(config_options=config_options) @@ -257,7 +295,7 @@ def test_integration(): ) assert ( cell_3_renderer.render(config_options=config_options) - == '\\[\n\\begin{aligned}\ny &= -2 \\; \n\\\\[10pt]\nb &= 3 \\; \n\\\\[10pt]\nc &= 4 \\; \n\\\\[10pt]\n\\alpha_{\\eta_{\\psi}} &= 23 \\; \n\\\\[10pt]\nd &= \\sqrt { \\frac{ 1 }{ b } \\cdot \\frac{1} { c } } = \\sqrt { \\frac{ 1 }{ 3 } \\cdot \\frac{1} { 4 } } &= 2.887 \\times 10 ^ {-1} \n\\\\[10pt]\nf &= \\left \\lceil \\left( \\alpha_{\\eta_{\\psi}} + 1 \\right) \\bmod 2 \\right \\rceil = \\left \\lceil \\left( 23 + 1 \\right) \\bmod 2 \\right \\rceil &= 0 \n\\\\[10pt]\ng &= \\int_{ y } ^ { b } \\left( x \\right) ^{ 2 } + 3 \\cdot x \\; dx = \\int_{ -2 } ^ { 3 } \\left( x \\right) ^{ 2 } + 3 \\cdot x \\; dx &= [42,\ 1.000 \\times 10 ^ {-3}] \n\\end{aligned}\n\\]' + == "\\[\n\\begin{aligned}\ny &= -2 \\; \n\\\\[10pt]\nb &= 3 \\; \n\\\\[10pt]\nc &= 4 \\; \n\\\\[10pt]\n\\alpha_{\\eta_{\\psi}} &= 23 \\; \n\\\\[10pt]\nd &= \\sqrt { \\frac{ 1 }{ b } \\cdot \\frac{1} { c } } = \\sqrt { \\frac{ 1 }{ 3 } \\cdot \\frac{1} { 4 } } &= 2.887 \\times 10 ^ {-1} \n\\\\[10pt]\nf &= \\left \\lceil \\left( \\alpha_{\\eta_{\\psi}} + 1 \\right) \\bmod 2 \\right \\rceil = \\left \\lceil \\left( 23 + 1 \\right) \\bmod 2 \\right \\rceil &= 0 \n\\\\[10pt]\ng &= \\int_{ y } ^ { b } \\left( x \\right) ^{ 2 } + 3 \\cdot x \\; dx = \\int_{ -2 } ^ { 3 } \\left( x \\right) ^{ 2 } + 3 \\cdot x \\; dx &= [42,\ 1.000 \\times 10 ^ {-3}] \n\\end{aligned}\n\\]" ) assert ( cell_4_renderer.render(config_options=config_options) @@ -281,20 +319,22 @@ def test_integration(): ) assert ( cell_8_renderer.render(config_options=config_options) - == '\\[\n\\begin{aligned}\na &= 23 \\; \n\\\\[10pt]\n\\dot{V} &= 43 \\; \n\\\\[10pt]\nc &= 52 \\; \n\\\\[10pt]\nf &= \\frac{ c }{ a } + \\dot{V} \\; \\;\\textrm{(Comment)}\n\\\\[10pt]\ng &= c \\cdot \\frac{ f }{ a } \\; \\;\\textrm{(Comment)}\n\\\\[10pt]\nd &= \\sqrt { \\frac{ a }{ \\dot{V} } + \\arcsin \\left( \\sin \\left( \\frac{ \\dot{V} }{ c } \\right) \\right) + \\left( \\frac{ a }{ \\dot{V} } \\right) ^{ 0.5 } + \\sqrt { \\frac{ a \\cdot \\dot{V} + \\dot{V} \\cdot c }{ \\left( \\dot{V} \\right) ^{ 2 } } } + \\sin \\left( \\frac{ a }{ \\dot{V} } \\right) } \\; \\;\\textrm{(Comment)}\n\\end{aligned}\n\\]' + == "\\[\n\\begin{aligned}\na &= 23 \\; \n\\\\[10pt]\n\\dot{V} &= 43 \\; \n\\\\[10pt]\nc &= 52 \\; \n\\\\[10pt]\nf &= \\frac{ c }{ a } + \\dot{V} = \\frac{ 52 }{ 23 } + 43 &= 45.261 \\; \\;\\textrm{(Comment)}\n\\\\[10pt]\ng &= c \\cdot \\frac{ f }{ a } = 52 \\cdot \\frac{ 45.261 }{ 23 } &= 102.329 \\; \\;\\textrm{(Comment)}\n\\\\[10pt]\nd &= \\sqrt { \\frac{ a }{ \\dot{V} } + \\arcsin \\left( \\sin \\left( \\frac{ \\dot{V} }{ c } \\right) \\right) + \\left( \\frac{ a }{ \\dot{V} } \\right) ^{ 0.5 } + \\sqrt { \\frac{ a \\cdot \\dot{V} + \\dot{V} \\cdot c }{ \\left( \\dot{V} \\right) ^{ 2 } } } + \\sin \\left( \\frac{ a }{ \\dot{V} } \\right) } \\\\&= \\sqrt { \\frac{ 23 }{ 43 } + \\arcsin \\left( \\sin \\left( \\frac{ 43 }{ 52 } \\right) \\right) + \\left( \\frac{ 23 }{ 43 } \\right) ^{ 0.5 } + \\sqrt { \\frac{ 23 \\cdot 43 + 43 \\cdot 52 }{ \\left( 43 \\right) ^{ 2 } } } + \\sin \\left( \\frac{ 23 }{ 43 } \\right) } \\\\&= 1.981 \\; \\;\\textrm{(Comment)}\\\\[10pt]\n\\end{aligned}\n\\]" ) assert ( cell_9_renderer.render(config_options=config_options) - == '\\[\n\\begin{aligned}\n\\mu &= 0.44 \\; \n\\\\[10pt]\n\\mathrm{CritSeg} &= 1.5 \\; \\;\\textrm{(sendo extramemente)}\n\\\\[10pt]\n\\Delta_{h} &= 9.641 \\; \n\\\\[10pt]\n\\mathrm{Raio} &= \\left( \\frac{ 200 }{ 2 } \\right) \\; \\;\\textrm{(Config)}\n\\\\[10pt]\n\\mathrm{Raio}_{Minimo} &= \\mathrm{CritSeg} \\cdot \\frac{ \\Delta_{h} }{ \\left( \\sin \\left( \\arctan \\left( \\mu + 1 \\right) + 1 \\right) \\right) ^{ 2 } } \\; \n\\end{aligned}\n\\]' + == "\\[\n\\begin{aligned}\n\\mu &= 0.44 \\; \n\\\\[10pt]\n\\mathrm{CritSeg} &= 1.5 \\; \\;\\textrm{(sendo extramemente)}\n\\\\[10pt]\n\\Delta_{h} &= 9.641 \\; \n\\\\[10pt]\n\\mathrm{Raio} &= \\left( \\frac{ 200 }{ 2 } \\right) \\; \\;\\textrm{(Config)}\n\\\\[10pt]\n\\mathrm{Raio}_{Minimo} &= \\mathrm{CritSeg} \\cdot \\frac{ \\Delta_{h} }{ \\left( \\sin \\left( \\arctan \\left( \\mu + 1 \\right) + 1 \\right) \\right) ^{ 2 } } \\; \n\\end{aligned}\n\\]" ) assert ( cell_10_renderer.render(config_options=config_options) - == '\\[\n\\begin{aligned}\n\\mu &= 45 + \\frac{ \\sin \\left( 34 + 2 \\right) }{ 2 } &= 4.450 \\times 10 ^ {1} \\; \\;\\textrm{(Comment)}\n\\\\[10pt]\n\\tau &= \\sin \\left( \\log_{2} \\left( \\log_{9} \\left( 3 \\right) \\right) \\right) &= -8.415 \\times 10 ^ {-1} \n\\\\[10pt]\n\\eta &= \\sqrt { \\frac{ 1 }{ \\log_{10} \\left( 6 \\right) } \\cdot \\frac{1} { \\ln \\left( 32 \\right) } } &= 6.089 \\times 10 ^ {-1} \n\\\\[10pt]\n\\kappa &= \\left \\lfloor \\frac{ 23 }{ 4.5 } \\right \\rfloor &= 5 \\; \\;\\textrm{(Last comment)}\n\\end{aligned}\n\\]' + == "\\[\n\\begin{aligned}\n\\mu &= 45 + \\frac{ \\sin \\left( 34 + 2 \\right) }{ 2 } &= 4.450 \\times 10 ^ {1} \\; \\;\\textrm{(Comment)}\n\\\\[10pt]\n\\tau &= \\sin \\left( \\log_{2} \\left( \\log_{9} \\left( 3 \\right) \\right) \\right) &= -8.415 \\times 10 ^ {-1} \n\\\\[10pt]\n\\eta &= \\sqrt { \\frac{ 1 }{ \\log_{10} \\left( 6 \\right) } \\cdot \\frac{1} { \\ln \\left( 32 \\right) } } &= 6.089 \\times 10 ^ {-1} \n\\\\[10pt]\n\\kappa &= \\left \\lfloor \\frac{ 23 }{ 4.5 } \\right \\rfloor &= 5 \\; \\;\\textrm{(Last comment)}\n\\end{aligned}\n\\]" ) - + assert ( cell_11_renderer.render(config_options=config_options) - == '\\[\n\\begin{aligned}\nF_{e_{x}} &= \\frac{ \\operatorname{euler\\ buckling\\ load} \\left( E ,\\ I_{x} ,\\ k_{x} ,\\ L \\right) }{ \\mathrm{area} } = \\frac{ \\operatorname{euler\\ buckling\\ load} \\left( 200000.000 ,\\ 300000000.000 ,\\ 1.000 ,\\ 3500 \\right) }{ 1000 } &= 4.500 \n\\\\[10pt]\nF_{e_{y}} &= \\frac{ \\operatorname{euler\\ buckling\\ load} \\left( E ,\\ I_{y} ,\\ k_{y} ,\\ L \\right) }{ \\mathrm{area} } = \\frac{ \\operatorname{euler\\ buckling\\ load} \\left( 200000.000 ,\\ 150000000.000 ,\\ 1.000 ,\\ 3500 \\right) }{ 1000 } &= 4.500 \n\\\\[10pt]\nF_{e} &= \\operatorname{min} \\left( F_{e_{x}} ,\\ F_{e_{y}} \\right) = \\operatorname{min} \\left( 4.500 ,\\ 4.500 \\right) &= 4.500 \n\\\\[10pt]\n\\lambda &= \\sqrt { \\frac{ f_{y} }{ F_{e} } } = \\sqrt { \\frac{ 350 }{ 4.500 } } &= 8.819 \n\\\\[10pt]\nP_{r} &= \\phi \\cdot \\mathrm{area} \\cdot f_{y} \\cdot \\left( 1 + \\left( \\lambda \\right) ^{ \\left( 2 \\cdot n \\right) } \\right) ^{ \\left( \\frac{ \\left( - 1 \\right) }{ n } \\right) } \\\\&= 0.900 \\cdot 1000 \\cdot 350 \\cdot \\left( 1 + \\left( 8.819 \\right) ^{ \\left( 2 \\cdot 1.340 \\right) } \\right) ^{ \\left( \\frac{ \\left( - 1 \\right) }{ 1.340 } \\right) } \\\\&= 4041.179 \\\\[10pt]\n\\end{aligned}\n\\]') + == "\\[\n\\begin{aligned}\nF_{e_{x}} &= \\frac{ \\operatorname{euler\\ buckling\\ load} \\left( E ,\\ I_{x} ,\\ k_{x} ,\\ L \\right) }{ \\mathrm{area} } = \\frac{ \\operatorname{euler\\ buckling\\ load} \\left( 200000.000 ,\\ 300000000.000 ,\\ 1.000 ,\\ 3500 \\right) }{ 1000 } &= 4.500 \n\\\\[10pt]\nF_{e_{y}} &= \\frac{ \\operatorname{euler\\ buckling\\ load} \\left( E ,\\ I_{y} ,\\ k_{y} ,\\ L \\right) }{ \\mathrm{area} } = \\frac{ \\operatorname{euler\\ buckling\\ load} \\left( 200000.000 ,\\ 150000000.000 ,\\ 1.000 ,\\ 3500 \\right) }{ 1000 } &= 4.500 \n\\\\[10pt]\nF_{e} &= \\operatorname{min} \\left( F_{e_{x}} ,\\ F_{e_{y}} \\right) = \\operatorname{min} \\left( 4.500 ,\\ 4.500 \\right) &= 4.500 \n\\\\[10pt]\n\\lambda &= \\sqrt { \\frac{ f_{y} }{ F_{e} } } = \\sqrt { \\frac{ 350 }{ 4.500 } } &= 8.819 \n\\\\[10pt]\nP_{r} &= \\phi \\cdot \\mathrm{area} \\cdot f_{y} \\cdot \\left( 1 + \\left( \\lambda \\right) ^{ \\left( 2 \\cdot n \\right) } \\right) ^{ \\left( \\frac{ \\left( - 1 \\right) }{ n } \\right) } \\\\&= 0.900 \\cdot 1000 \\cdot 350 \\cdot \\left( 1 + \\left( 8.819 \\right) ^{ \\left( 2 \\cdot 1.340 \\right) } \\right) ^{ \\left( \\frac{ \\left( - 1 \\right) }{ 1.340 } \\right) } \\\\&= 4041.179 \\\\[10pt]\n\\end{aligned}\n\\]" + ) + # Test decorator.py @@ -309,9 +349,11 @@ def test_handcalc(): def test_handcalc2(): assert func_2(4, 5) == {"x": 4, "y": 5, "a": 8, "b": 29} + def test_handcalcs3(): assert func_3(4, 5) == {"x": 4, "y": 5, "a": 8, "b": 29} + # Test template.py @@ -335,6 +377,7 @@ def test_html_exporter(): parent = exporter.__class__.__bases__[0]() assert parent.exclude_input == False + # Test expected exceptions @@ -597,10 +640,12 @@ def test_round_and_render_line_objects_to_latex(): ) assert handcalcs.handcalcs.round_and_render_line_objects_to_latex( - ParameterLine(line=deque(["y", "=", -2]), comment="", latex=""), cell_precision=3, cell_notation=True, **config_options, + ParameterLine(line=deque(["y", "=", -2]), comment="", latex=""), + cell_precision=3, + cell_notation=True, + **config_options, ) == ParameterLine(line=deque(["y", "=", "-2"]), comment="", latex="y = -2") - assert handcalcs.handcalcs.round_and_render_line_objects_to_latex( CalcLine( line=deque( @@ -1271,11 +1316,12 @@ def test_swap_for_greek(): deque(["eta", "=", "beta", "+", "theta"]), **config_options ) == deque(["\\eta", "=", "\\beta", "+", "\\theta"]) assert handcalcs.handcalcs.swap_for_greek( - deque(["M_r", "=", "phi", "\\cdot", deque(["psi", "\\cdot", "F_y"])]), **config_options + deque(["M_r", "=", "phi", "\\cdot", deque(["psi", "\\cdot", "F_y"])]), + **config_options, ) == deque(["M_r", "=", "\\phi", "\\cdot", deque(["\\psi", "\\cdot", "F_y"])]) - assert handcalcs.handcalcs.swap_for_greek(deque(["lamb", "=", 3]), **config_options) == deque( - ["\\lambda", "=", 3] - ) + assert handcalcs.handcalcs.swap_for_greek( + deque(["lamb", "=", 3]), **config_options + ) == deque(["\\lambda", "=", 3]) def test_test_for_scientific_float(): @@ -1325,25 +1371,42 @@ def test_swap_comparison_ops(): def test_test_for_long_var_strs(): - assert handcalcs.handcalcs.test_for_long_var_strs("x_y_a", **config_options) == False - assert handcalcs.handcalcs.test_for_long_var_strs("Rate_annual", **config_options) == True - assert handcalcs.handcalcs.test_for_long_var_strs("x_rake_red", **config_options) == False - assert handcalcs.handcalcs.test_for_long_var_strs("AB_x_y", **config_options) == True - assert handcalcs.handcalcs.test_for_long_var_strs("category_x", **config_options) == True + assert ( + handcalcs.handcalcs.test_for_long_var_strs("x_y_a", **config_options) == False + ) + assert ( + handcalcs.handcalcs.test_for_long_var_strs("Rate_annual", **config_options) + == True + ) + assert ( + handcalcs.handcalcs.test_for_long_var_strs("x_rake_red", **config_options) + == False + ) + assert ( + handcalcs.handcalcs.test_for_long_var_strs("AB_x_y", **config_options) == True + ) + assert ( + handcalcs.handcalcs.test_for_long_var_strs("category_x", **config_options) + == True + ) assert handcalcs.handcalcs.test_for_long_var_strs("x", **config_options) == False assert handcalcs.handcalcs.test_for_long_var_strs("xy", **config_options) == True assert handcalcs.handcalcs.test_for_long_var_strs(234.21, **config_options) == False - assert handcalcs.handcalcs.test_for_long_var_strs("\\frac{", **config_options) == False - assert handcalcs.handcalcs.test_for_long_var_strs("\\sin", **config_options) == False + assert ( + handcalcs.handcalcs.test_for_long_var_strs("\\frac{", **config_options) == False + ) + assert ( + handcalcs.handcalcs.test_for_long_var_strs("\\sin", **config_options) == False + ) def test_swap_long_var_strs(): - assert handcalcs.handcalcs.swap_long_var_strs(deque(["cat_xy_u", "+", 4]), **config_options) == deque( - ["\\mathrm{cat}_xy_u", "+", 4] - ) - assert handcalcs.handcalcs.swap_long_var_strs(deque(["RATE", "*", "4"]), **config_options) == deque( - ["\\mathrm{RATE}", "*", "4"] - ) + assert handcalcs.handcalcs.swap_long_var_strs( + deque(["cat_xy_u", "+", 4]), **config_options + ) == deque(["\\mathrm{cat}_xy_u", "+", 4]) + assert handcalcs.handcalcs.swap_long_var_strs( + deque(["RATE", "*", "4"]), **config_options + ) == deque(["\\mathrm{RATE}", "*", "4"]) assert handcalcs.handcalcs.swap_long_var_strs( deque(["\\sin", "\\left(", "apple_cart", "\\right)"]), **config_options ) == deque(["\\sin", "\\left(", "\\mathrm{apple}_cart", "\\right)"]) @@ -1519,11 +1582,18 @@ def test_swap_math_funcs(): ["z", "=", deque(["double", deque(["\\left(", "a", "/", "b", "\\right)"])])] ), dict(), - ) == deque(['z', - '=', - deque(['\\operatorname{double}', - deque(['\\left(', 'a', '/', 'b', '\\right)']), - ])]) + ) == deque( + [ + "z", + "=", + deque( + [ + "\\operatorname{double}", + deque(["\\left(", "a", "/", "b", "\\right)"]), + ] + ), + ] + ) assert handcalcs.handcalcs.swap_math_funcs( deque(["rate", "=", deque(["sin", "\\left(", "a", "\\right)"])]), dict() ) == deque(["rate", "=", deque(["\\sin", "\\left(", "a", "\\right)"])]) @@ -1667,10 +1737,39 @@ def test_format_strings(): def test_latex_repr(): mock_obj_1 = MockLatexObj1("test string") mock_obj_2 = MockLatexObj2("23 23") - assert handcalcs.handcalcs.latex_repr(123, precision=3, use_scientific_notation=True, preferred_formatter = "L") == "123" - assert handcalcs.handcalcs.latex_repr(20 * si.Pa, precision=3, use_scientific_notation=True, preferred_formatter = "L") == "2.000 \\times 10^ {1}\\ \\mathrm{Pa}" - assert handcalcs.handcalcs.latex_repr(mock_obj_1, precision=3, use_scientific_notation=True, preferred_formatter = "L") == "\\mathrm{test string}" - assert handcalcs.handcalcs.latex_repr(mock_obj_2, precision=3, use_scientific_notation=True, preferred_formatter = "L") == "\\text{23 23}" + assert ( + handcalcs.handcalcs.latex_repr( + 123, precision=3, use_scientific_notation=True, preferred_formatter="L" + ) + == "123" + ) + assert ( + handcalcs.handcalcs.latex_repr( + 20 * si.Pa, + precision=3, + use_scientific_notation=True, + preferred_formatter="L", + ) + == "2.000 \\times 10^ {1}\\ \\mathrm{Pa}" + ) + assert ( + handcalcs.handcalcs.latex_repr( + mock_obj_1, + precision=3, + use_scientific_notation=True, + preferred_formatter="L", + ) + == "\\mathrm{test string}" + ) + assert ( + handcalcs.handcalcs.latex_repr( + mock_obj_2, + precision=3, + use_scientific_notation=True, + preferred_formatter="L", + ) + == "\\text{23 23}" + ) def test_swap_integrals(): @@ -1706,16 +1805,48 @@ def test_replace_underscores(): deque(["cat_a", "+", deque(["\\Delta_T", "\\cdot", 234.4])]) ) == deque(["cat\\ a", "+", deque(["\\Delta\\ T", "\\cdot", 234.4])]) + def test_swap_chained_fracs(): - assert handcalcs.handcalcs.swap_chained_fracs( # Test for basic functionality + assert handcalcs.handcalcs.swap_chained_fracs( # Test for basic functionality deque(["d", "=", "a", "/", "b", "/", "3"]) ) == deque(["d", "=", "a", "/", "b", "\\cdot", "\\frac{1}", "{", "3", "}"]) - assert handcalcs.handcalcs.swap_chained_fracs( # Test for chained div followed by non-chained div + assert handcalcs.handcalcs.swap_chained_fracs( # Test for chained div followed by non-chained div deque(["d", "=", "a", "/", "b", "/", "3", "+", "c", "/", "e"]) - ) == deque(["d", "=", "a", "/", "b", "\\cdot", "\\frac{1}", "{", "3", "}", "+", "c", "/", "e"]) - assert handcalcs.handcalcs.swap_chained_fracs( # Test for nested chained divs + ) == deque( + [ + "d", + "=", + "a", + "/", + "b", + "\\cdot", + "\\frac{1}", + "{", + "3", + "}", + "+", + "c", + "/", + "e", + ] + ) + assert handcalcs.handcalcs.swap_chained_fracs( # Test for nested chained divs deque(["d", "=", "a", "/", "b", "/", deque(["a", "/", "b", "/", "3"])]) - ) == deque(["d", "=", "a", "/", "b", "\\cdot", "\\frac{1}", "{", deque(["a", "/", "b", "\\cdot", "\\frac{1}", "{", "3", "}"]), "}"]) + ) == deque( + [ + "d", + "=", + "a", + "/", + "b", + "\\cdot", + "\\frac{1}", + "{", + deque(["a", "/", "b", "\\cdot", "\\frac{1}", "{", "3", "}"]), + "}", + ] + ) + def test_test_for_py_operator(): assert handcalcs.handcalcs.test_for_py_operator("*") == True @@ -1738,19 +1869,27 @@ def test_test_for_py_operator(): def test_for_numeric_line(): - assert handcalcs.handcalcs.test_for_numeric_line( - deque(["=", "2", "*", "4"]) - ) == True - assert handcalcs.handcalcs.test_for_numeric_line( - deque(["=", "2", "*", "b"]) - ) == False - assert handcalcs.handcalcs.test_for_numeric_line( - deque(["=", "2", "*", deque(["2", "*", "4"])]) - ) == True - assert handcalcs.handcalcs.test_for_numeric_line( - deque(["=", "2", "*", deque(["b", "*", "4"])]) - ) == False - assert handcalcs.handcalcs.test_for_numeric_line( - deque(["=", "2", "*", deque(["sin", "4"])]) - ) == True - + assert ( + handcalcs.handcalcs.test_for_numeric_line(deque(["=", "2", "*", "4"])) == True + ) + assert ( + handcalcs.handcalcs.test_for_numeric_line(deque(["=", "2", "*", "b"])) == False + ) + assert ( + handcalcs.handcalcs.test_for_numeric_line( + deque(["=", "2", "*", deque(["2", "*", "4"])]) + ) + == True + ) + assert ( + handcalcs.handcalcs.test_for_numeric_line( + deque(["=", "2", "*", deque(["b", "*", "4"])]) + ) + == False + ) + assert ( + handcalcs.handcalcs.test_for_numeric_line( + deque(["=", "2", "*", deque(["sin", "4"])]) + ) + == True + ) diff --git a/test_handcalcs/test_pint.py b/test_handcalcs/test_pint.py index 5ba5599..edec5ad 100644 --- a/test_handcalcs/test_pint.py +++ b/test_handcalcs/test_pint.py @@ -1,40 +1,57 @@ import pint import handcalcs.global_config -from handcalcs.handcalcs import ( - CalcLine, round_and_render_line_objects_to_latex -) +from handcalcs.handcalcs import CalcLine, round_and_render_line_objects_to_latex ureg = pint.UnitRegistry(auto_reduce_dimensions=True) -ureg.default_format = '~' +ureg.default_format = "~" ft = ureg.ft kip = ureg.kip config_options = handcalcs.global_config._config + def test_pint_rounding(): - L = (1.23456789 * kip) - d = (2 * ft) + L = 1.23456789 * kip + d = 2 * ft M = L * d - assert round_and_render_line_objects_to_latex( - CalcLine([L], '', ''), cell_precision=2, cell_notation=False, **config_options - ).latex == '1.23\\ \\mathrm{kip}' + assert ( + round_and_render_line_objects_to_latex( + CalcLine([L], "", ""), + cell_precision=2, + cell_notation=False, + **config_options + ).latex + == "1.23\\ \\mathrm{kip}" + ) - assert round_and_render_line_objects_to_latex( - CalcLine([d], '', ''), cell_precision=2, cell_notation=True, **config_options - ).latex == '2.00\\times 10^{0}\\ \\mathrm{foot}' + assert ( + round_and_render_line_objects_to_latex( + CalcLine([d], "", ""), + cell_precision=2, + cell_notation=True, + **config_options + ).latex + == "2.00\\times 10^{0}\\ \\mathrm{foot}" + ) - assert round_and_render_line_objects_to_latex( - CalcLine([M], '', ''), cell_precision=2, cell_notation=False, **config_options - ).latex == '2.47\\ \\mathrm{foot} \\cdot \\mathrm{kip}' + assert ( + round_and_render_line_objects_to_latex( + CalcLine([M], "", ""), + cell_precision=2, + cell_notation=False, + **config_options + ).latex + == "2.47\\ \\mathrm{foot} \\cdot \\mathrm{kip}" + ) def test_pint_with_sympy(): # NOTE: This test fails because pint introduced breaking changes at some point and # the module path of pint.quantity._Quantity._sympy_ no longer exists. A cursory - # review of the pint code could not locate it. + # review of the pint code could not locate it. # Because pint is still in 0.x.y, the API is likely to be volatile which makes # testing difficult. diff --git a/test_handcalcs/test_render_file.py b/test_handcalcs/test_render_file.py index 791e555..e8d9407 100644 --- a/test_handcalcs/test_render_file.py +++ b/test_handcalcs/test_render_file.py @@ -59,6 +59,5 @@ def test_parse_line_args(): "override": "", "precision": 1, "sympy": False, - "sci_not": True + "sci_not": True, } - diff --git a/test_handcalcs/test_sympy_kit.py b/test_handcalcs/test_sympy_kit.py index ee69dd0..a2fe0ac 100644 --- a/test_handcalcs/test_sympy_kit.py +++ b/test_handcalcs/test_sympy_kit.py @@ -1,8 +1,6 @@ import inspect from collections import deque -from handcalcs.handcalcs import ( - CalcLine, round_and_render_line_objects_to_latex -) +from handcalcs.handcalcs import CalcLine, round_and_render_line_objects_to_latex import handcalcs.sympy_kit as sk import handcalcs.global_config import pathlib @@ -16,6 +14,7 @@ d = sp.Eq(2 * a + b, 14) config_options = handcalcs.global_config._config + def test_sympy_cell_line_lists(): assert sk.sympy_cell_line_lists("x = 9\ny = 10") == [["x", "9"], ["y", "10"]] @@ -58,10 +57,22 @@ def test_convert_sympy_cell_to_py_cell(): def test_sympy_rounding(): expr = 12.3456789 * a + 1.23456789e-55 * b - assert round_and_render_line_objects_to_latex( - CalcLine([expr], '', ''), cell_precision=3, cell_notation=True, **config_options - ).latex == '12.35 a + 1.235 \\cdot 10^{-55} b' + assert ( + round_and_render_line_objects_to_latex( + CalcLine([expr], "", ""), + cell_precision=3, + cell_notation=True, + **config_options + ).latex + == "12.35 a + 1.235 \\cdot 10^{-55} b" + ) - assert round_and_render_line_objects_to_latex( - CalcLine([expr], '', ''), cell_precision=4, cell_notation=False, **config_options - ).latex == '12.3457 a' + assert ( + round_and_render_line_objects_to_latex( + CalcLine([expr], "", ""), + cell_precision=4, + cell_notation=False, + **config_options + ).latex + == "12.3457 a" + )