Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/preview_implementations/physical_quantity_preview.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import tokenize
from copy import deepcopy

from ..utility.expression_utilities import (
Expand Down Expand Up @@ -119,7 +120,7 @@ def preview_function(response: str, params: Params) -> Result:
latex_out = res_parsed.latex_string
sympy_out = response

except SyntaxError as e:
except (SyntaxError, tokenize.TokenError) as e:
raise Exception("Failed to parse Sympy expression") from e
except ValueError as e:
raise Exception("Failed to parse LaTeX expression") from e
Expand Down
1 change: 0 additions & 1 deletion app/preview_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,5 @@ def test_laplace_transforms(self, response, is_latex, latex, sympy):
assert result["preview"]["sympy"] == sympy



if __name__ == "__main__":
pytest.main(['-xk not slow', "--tb=line", os.path.abspath(__file__)])
17 changes: 17 additions & 0 deletions app/tests/physical_quantity_preview_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,23 @@ def test_handwritten_input(self):
assert result["latex"] == r'162~\frac{\mathrm{newton}}{\mathrm{metre}^{(2)}}' # TODO: Fix so that unnecessary parenthesis are simplified away
assert result["sympy"] == "162 newton/metre**(2)"

@pytest.mark.parametrize(
"response, latex, sympy", [
(r"\frac{F}{p \cdot \mu}", r"\frac{F}{\mu p}", "F/((mu*p))"),
(r"F \cdot p", "F p", "F*p"),
(r"\frac{10}{2} \mathrm{~kg}", r"\frac{10}{2}~\mathrm{kilogram}", "10/2 kilogram"),
]
)
def test_latex_physical_quantity_preview(self, response, latex, sympy):
params = {
"is_latex": True,
"physical_quantity": True,
"elementary_functions": True,
}
result = preview_function(response, params)["preview"]
assert result["latex"] == latex
assert result["sympy"] == sympy


if __name__ == "__main__":
pytest.main(['-xk not slow', "--tb=line", os.path.abspath(__file__)])
5 changes: 5 additions & 0 deletions app/utility/physical_quantity_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from ..feedback.physical_quantity import feedback_string_generators as physical_quantity_feedback_string_generators

from ..preview_implementations.symbolic_preview import preview_function as symbolic_preview
from .preview_utilities import parse_latex

QuantityTags = Enum("QuantityTags", {v: i for i, v in enumerate("UVNR", 1)})

Expand Down Expand Up @@ -209,6 +210,10 @@ def _expand_units(self, node):
def _all_forms(self):
parsing_params = self.parsing_params
converted_value = self.value.content_string() if self.value is not None else None

if converted_value is not None and self.parameters.get("is_latex", False):
symbols = self.parameters.get("symbols", {})
converted_value = parse_latex(converted_value, symbols, self.parameters.get("simplify", False))
converted_unit = None
expanded_unit = None
converted_dimension = parse_expression("1", parsing_params)
Expand Down
Loading