Skip to content

Commit

Permalink
black
Browse files Browse the repository at this point in the history
  • Loading branch information
connorferster committed Oct 14, 2020
1 parent 0ae4856 commit e1b935a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 40 deletions.
34 changes: 14 additions & 20 deletions handcalcs/handcalcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1781,8 +1781,6 @@ def swap_floor_ceil(d: deque, func_name: str, calc_results: dict) -> deque:
swapped_deque.append(item)
return swapped_deque



lpar = "\\left("
rpar = "\\right)"
swapped_deque = deque([])
Expand All @@ -1801,9 +1799,6 @@ def swap_floor_ceil(d: deque, func_name: str, calc_results: dict) -> deque:
return swapped_deque





def flatten_deque(d: deque) -> deque:
new_deque = deque([])
for item in flatten(d):
Expand Down Expand Up @@ -2395,9 +2390,6 @@ def swap_values(pycode_as_deque: deque, tex_results: dict) -> deque:
return outgoing





def test_for_unary(d: deque) -> bool:
"""
Returns True if 'd' represents a unary expression, e.g. -1.
Expand Down Expand Up @@ -2430,7 +2422,7 @@ def get_function_name(d: deque) -> str:
return d[0]
elif test_for_function_name(dummy_deque):
return dummy_deque[0]
# elif (isinstance(d[0], str) and re.match(r"^[A-Za-z0-9_]+$", d[0])
# elif (isinstance(d[0], str) and re.match(r"^[A-Za-z0-9_]+$", d[0])
# and isinstance(d[1], deque)# and d[1][0] == "\\left("
# ):
# return d[0]
Expand All @@ -2453,17 +2445,17 @@ def test_for_function_name(d: deque) -> bool:
(len(d) == 2 or len(d) == 4)
and (isinstance(d[0], str) and re.match(r"^[A-Za-z0-9_]+$", d[0]))
and (
isinstance(d[1], str) and re.match(r"^[A-Za-z0-9_]+$", d[1])
or d[1] == "\\left(")
isinstance(d[1], str)
and re.match(r"^[A-Za-z0-9_]+$", d[1])
or d[1] == "\\left("
)
):
return True
elif (
len(d) > 1
and isinstance(d[0], str)
and re.match(r"^[A-Za-z0-9_]+$", d[0])
and isinstance(
d[1], deque
)
and isinstance(d[1], deque)
):
return True
else:
Expand Down Expand Up @@ -2573,11 +2565,12 @@ def insert_parentheses(pycode_as_deque: deque) -> deque:
new_item = insert_parentheses(item)
swapped_deque.append(new_item)

elif (typ_arithmetic
# and not prev_item == lpar
elif (
typ_arithmetic
# and not prev_item == lpar
and not skip_fraction_token
):

if test_for_fraction_exception(item, next_item):

skip_fraction_token = True
Expand All @@ -2586,7 +2579,7 @@ def insert_parentheses(pycode_as_deque: deque) -> deque:
else:
if (
prev_item not in func_exclude
#and not test_for_nested_deque(item)
# and not test_for_nested_deque(item)
and next_item != "**"
): # Allow swap_superscript to handle its parenths
item = insert_arithmetic_parentheses(item)
Expand Down Expand Up @@ -2636,7 +2629,8 @@ def swap_dec_sep(d: deque, dec_sep: str) -> deque:
swapped_deque = deque([])
a = "{"
b = "}"
if dec_sep == ".": return d
if dec_sep == ".":
return d
for item in d:
if is_number(item):
item = item.replace(".", f"{a}{dec_sep}{b}")
Expand All @@ -2650,4 +2644,4 @@ def swap_dec_sep(d: deque, dec_sep: str) -> deque:
swapped_deque.append(" ".join(swapped_components))
else:
swapped_deque.append(item)
return swapped_deque
return swapped_deque
33 changes: 13 additions & 20 deletions test_handcalcs/test_handcalcs_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def func_2(x, y):
def test_integration():
assert (
cell_1_renderer.render()
== '\\[\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()
Expand All @@ -212,27 +212,27 @@ def test_integration():
)
assert (
cell_5_renderer.render()
== '\\[\n\\begin{aligned}\na &= 10000001 \\; \\;\\textrm{(Comment)}\n\\\\[10pt]\nb &= 20000002 \\; \n\\\\[10pt]\nc &= 30000003 \\; \n\\\\[10pt]\nx &= 5 \\; \n\\\\[10pt]\ny &= \\sqrt { \\frac{ a }{ b } } + \\arcsin \\left( \\sin \\left( \\frac{ b }{ c } \\right) \\right) + \\left( \\frac{ a }{ b } \\right) ^{ 0.5 } + \\sqrt { \\frac{ a \\cdot b + b \\cdot c }{ \\left( b \\right) ^{ 2 } } } + \\sin \\left( \\frac{ a }{ b } \\right) \\\\&= \\sqrt { \\frac{ 10000001 }{ 20000002 } } + \\arcsin \\left( \\sin \\left( \\frac{ 20000002 }{ 30000003 } \\right) \\right) + \\left( \\frac{ 10000001 }{ 20000002 } \\right) ^{ 0.5 } + \\sqrt { \\frac{ 10000001 \\cdot 20000002 + 20000002 \\cdot 30000003 }{ \\left( 20000002 \\right) ^{ 2 } } } + \\sin \\left( \\frac{ 10000001 }{ 20000002 } \\right) \\\\&= 3.975 \\; \\;\\textrm{(Comment)}\\\\\n\\end{aligned}\n\\]'
== "\\[\n\\begin{aligned}\na &= 10000001 \\; \\;\\textrm{(Comment)}\n\\\\[10pt]\nb &= 20000002 \\; \n\\\\[10pt]\nc &= 30000003 \\; \n\\\\[10pt]\nx &= 5 \\; \n\\\\[10pt]\ny &= \\sqrt { \\frac{ a }{ b } } + \\arcsin \\left( \\sin \\left( \\frac{ b }{ c } \\right) \\right) + \\left( \\frac{ a }{ b } \\right) ^{ 0.5 } + \\sqrt { \\frac{ a \\cdot b + b \\cdot c }{ \\left( b \\right) ^{ 2 } } } + \\sin \\left( \\frac{ a }{ b } \\right) \\\\&= \\sqrt { \\frac{ 10000001 }{ 20000002 } } + \\arcsin \\left( \\sin \\left( \\frac{ 20000002 }{ 30000003 } \\right) \\right) + \\left( \\frac{ 10000001 }{ 20000002 } \\right) ^{ 0.5 } + \\sqrt { \\frac{ 10000001 \\cdot 20000002 + 20000002 \\cdot 30000003 }{ \\left( 20000002 \\right) ^{ 2 } } } + \\sin \\left( \\frac{ 10000001 }{ 20000002 } \\right) \\\\&= 3.975 \\; \\;\\textrm{(Comment)}\\\\\n\\end{aligned}\n\\]"
)
assert (
cell_6_renderer.render()
== "\\[\n\\begin{aligned}\na &= 2 \\; \n\\\\[10pt]\nb &= 3 \\cdot a \\\\&= 3 \\cdot 2 \\\\&= 6 \\\\\n\\\\[10pt]\ny &= 2 \\cdot a + 4 + 3 \\\\&= 2 \\cdot 2 + 4 + 3 \\\\&= 11 \\\\\n\\end{aligned}\n\\]"
)
assert (
cell_7_renderer.render()
== '\\[\n\\begin{aligned}\na &= 23 \\; \n\\\\[10pt]\nb &= 43 \\; \n\\\\[10pt]\nc &= 52 \\; \n\\\\[10pt]\nf &= \\frac{ c }{ a } + b \\\\&= \\frac{ 52 }{ 23 } + 43 \\\\&= 45.26 \\; \\;\\textrm{(Comment)}\\\\\n\\\\[10pt]\ng &= c \\cdot \\frac{ f }{ a } \\\\&= 52 \\cdot \\frac{ 45.26 }{ 23 } \\\\&= 102.33 \\; \\;\\textrm{(Comment)}\\\\\n\\\\[10pt]\nd &= \\sqrt { \\frac{ a }{ b } } + \\arcsin \\left( \\sin \\left( \\frac{ b }{ c } \\right) \\right) + \\left( \\frac{ a }{ b } \\right) ^{ 0.5 } + \\sqrt { \\frac{ a \\cdot b + b \\cdot c }{ \\left( b \\right) ^{ 2 } } } + \\sin \\left( \\frac{ a }{ b } \\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) \\\\&= 4.12 \\; \\;\\textrm{(Comment)}\\\\\n\\end{aligned}\n\\]'
== "\\[\n\\begin{aligned}\na &= 23 \\; \n\\\\[10pt]\nb &= 43 \\; \n\\\\[10pt]\nc &= 52 \\; \n\\\\[10pt]\nf &= \\frac{ c }{ a } + b \\\\&= \\frac{ 52 }{ 23 } + 43 \\\\&= 45.26 \\; \\;\\textrm{(Comment)}\\\\\n\\\\[10pt]\ng &= c \\cdot \\frac{ f }{ a } \\\\&= 52 \\cdot \\frac{ 45.26 }{ 23 } \\\\&= 102.33 \\; \\;\\textrm{(Comment)}\\\\\n\\\\[10pt]\nd &= \\sqrt { \\frac{ a }{ b } } + \\arcsin \\left( \\sin \\left( \\frac{ b }{ c } \\right) \\right) + \\left( \\frac{ a }{ b } \\right) ^{ 0.5 } + \\sqrt { \\frac{ a \\cdot b + b \\cdot c }{ \\left( b \\right) ^{ 2 } } } + \\sin \\left( \\frac{ a }{ b } \\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) \\\\&= 4.12 \\; \\;\\textrm{(Comment)}\\\\\n\\end{aligned}\n\\]"
)
assert (
cell_7b_renderer.render()
== "\\[\n\\begin{aligned}\n\\alpha_{\\zeta} &= 0.984 \\; \n\\\\[10pt]\nb'_{c} &= 43 \\; \n\\\\[10pt]\n\\mathrm{causal} &= (4.2+3.2j) \\; \n\\\\[10pt]\nf &= \\frac{ \\mathrm{causal} }{ \\alpha_{\\zeta} } + b'_{c} = \\frac{ (4.2+3.2j) }{ 0.984 } + 43 &= (47.268+3.252j) \\; \\;\\textrm{(Comment)}\n\\\\[10pt]\ng &= \\mathrm{causal} \\cdot \\frac{ f }{ \\alpha_{\\zeta} } = (4.2+3.2j) \\cdot \\frac{ (47.268+3.252j) }{ 0.984 } &= (191.179+167.599j) \\; \\;\\textrm{(Comment)}\n\\\\[10pt]\nd &= \\sqrt { \\frac{ \\alpha_{\\zeta} }{ b'_{c} } } + \\Sigma \\left( 1 ,\\ 2 ,\\ 3 \\right) + \\left( \\frac{ \\alpha_{\\zeta} }{ b'_{c} } \\right) ^{ 0.5 } + \\sqrt { \\frac{ \\alpha_{\\zeta} \\cdot b'_{c} + b'_{c} }{ \\left( 1.23 \\times 10 ^ {3 } \\right) ^{ 2 } } } + \\sin \\left( \\frac{ \\alpha_{\\zeta} }{ b'_{c} } \\right) = \\sqrt { \\frac{ 0.984 }{ 43 } } + \\Sigma \\left( 1 ,\\ 2 ,\\ 3 \\right) + \\left( \\frac{ 0.984 }{ 43 } \\right) ^{ 0.5 } + \\sqrt { \\frac{ 0.984 \\cdot 43 + 43 }{ \\left( 1.23 \\times 10 ^ {3 } \\right) ^{ 2 } } } + \\sin \\left( \\frac{ 0.984 }{ 43 } \\right) &= 6.333 \\; \\;\\textrm{(Comment)}\n\\end{aligned}\n\\]"
)
assert (
cell_8_renderer.render()
== '\\[\n\\begin{aligned}\na &= 23 \\; \n\\\\[10pt]\nb &= 43 \\; \n\\\\[10pt]\nc &= 52 \\; \n\\\\[10pt]\nf &= \\frac{ c }{ a } + b \\; \\;\\textrm{(Comment)}\n\\\\[10pt]\ng &= c \\cdot \\frac{ f }{ a } \\; \\;\\textrm{(Comment)}\n\\\\[10pt]\nd &= \\sqrt { \\frac{ a }{ b } } + \\arcsin \\left( \\sin \\left( \\frac{ b }{ c } \\right) \\right) + \\left( \\frac{ a }{ b } \\right) ^{ 0.5 } + \\sqrt { \\frac{ a \\cdot b + b \\cdot c }{ \\left( b \\right) ^{ 2 } } } + \\sin \\left( \\frac{ a }{ b } \\right) \\; \\;\\textrm{(Comment)}\n\\end{aligned}\n\\]'
== "\\[\n\\begin{aligned}\na &= 23 \\; \n\\\\[10pt]\nb &= 43 \\; \n\\\\[10pt]\nc &= 52 \\; \n\\\\[10pt]\nf &= \\frac{ c }{ a } + b \\; \\;\\textrm{(Comment)}\n\\\\[10pt]\ng &= c \\cdot \\frac{ f }{ a } \\; \\;\\textrm{(Comment)}\n\\\\[10pt]\nd &= \\sqrt { \\frac{ a }{ b } } + \\arcsin \\left( \\sin \\left( \\frac{ b }{ c } \\right) \\right) + \\left( \\frac{ a }{ b } \\right) ^{ 0.5 } + \\sqrt { \\frac{ a \\cdot b + b \\cdot c }{ \\left( b \\right) ^{ 2 } } } + \\sin \\left( \\frac{ a }{ b } \\right) \\; \\;\\textrm{(Comment)}\n\\end{aligned}\n\\]"
)
assert (
cell_9_renderer.render()
== '\\[\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\\]"
)


Expand Down Expand Up @@ -1324,22 +1324,15 @@ def test_swap_long_var_strs():


def test_test_for_function_name():
assert (
handcalcs.handcalcs.test_for_function_name(deque(["sin", "45"])) == True
)
assert handcalcs.handcalcs.test_for_function_name(deque(["sin", "45"])) == True
assert (
handcalcs.handcalcs.test_for_function_name(
deque(["sin", deque(["a", "/", "b"])])
)
== True
)
assert (
handcalcs.handcalcs.test_for_function_name(deque(["1", "+", "b"]))
== False
)
assert (
handcalcs.handcalcs.test_for_function_name(deque(["-", "a"])) == False
)
assert handcalcs.handcalcs.test_for_function_name(deque(["1", "+", "b"])) == False
assert handcalcs.handcalcs.test_for_function_name(deque(["-", "a"])) == False
assert (
handcalcs.handcalcs.test_for_function_name(
deque(["sin", deque(["tan", deque(["a", "/", "b"])])])
Expand Down Expand Up @@ -1397,9 +1390,9 @@ def test_insert_function_parentheses():
deque(["sin", deque(["a", "/", "b"])])
) == deque(["sin", deque(["\\left(", "a", "/", "b", "\\right)"])])
assert handcalcs.handcalcs.insert_function_parentheses(
deque(['double', deque(['tan', deque(['4', '/', 'a'])])])
deque(["double", deque(["tan", deque(["4", "/", "a"])])])
) == deque(
["double", deque([ "\\left(", "tan", deque(["4", "/", "a"]), "\\right)"])]
["double", deque(["\\left(", "tan", deque(["4", "/", "a"]), "\\right)"])]
)


Expand Down Expand Up @@ -1494,7 +1487,7 @@ def test_swap_math_funcs():

assert handcalcs.handcalcs.swap_math_funcs(
deque(
["z", "=", deque(["double",deque([ "\\left(", "a", "/", "b", "\\right)"])])]
["z", "=", deque(["double", deque(["\\left(", "a", "/", "b", "\\right)"])])]
),
dict(),
) == deque(
Expand All @@ -1504,14 +1497,14 @@ def test_swap_math_funcs():
deque(
[
"\\operatorname{double}",
deque(["\\left(","a", "/", "b", "\\right)"]),
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)"])])
) == deque(["rate", "=", deque(["\\sin", "\\left(", "a", "\\right)"])])
assert handcalcs.handcalcs.swap_math_funcs(
deque(["test", "=", deque(["sqrt", deque(["b", "+", "b"])])]), dict()
) == deque(["test", "=", deque(["\\sqrt", "{", deque(["b", "+", "b"]), "}"])])
Expand Down

0 comments on commit e1b935a

Please sign in to comment.