Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add symmath to openedx-calc #38

Merged
merged 1 commit into from
Jan 20, 2022
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
2 changes: 1 addition & 1 deletion .coveragerc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[run]
data_file = reports/.coverage
source = calc
source = calc, symmath
branch = true
relative_files = True

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ build
.pytest_cache
.coverage
venv/
.vscode/
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/make -f

.PHONY: help
.PHONY: help requirements
help: ## This.
@perl -ne 'print if /^[a-zA-Z_-]+:.*## .*$$/' $(MAKEFILE_LIST) \
| sort \
Expand All @@ -12,6 +12,11 @@ clean: ## Remove all build artifacts
test: ## Run the library test suite
tox

requirements: ## install development environment requirements
pip install -r requirements/pip.txt
pip install -r requirements/pip_tools.txt
pip install -r requirements/test.txt

upgrade: export CUSTOM_COMPILE_COMMAND=make upgrade
upgrade: ## update the requirements/*.txt files with the latest packages satisfying requirements/*.in
pip install -q -r requirements/pip_tools.txt
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
openedx-calc
============

A helper library for mathematical calculations, used by the `edx-platform`_.
A helper library for mathematical calculations and symbolic mathematics, used by the `edx-platform`_.
Carlos-Muniz marked this conversation as resolved.
Show resolved Hide resolved

This code originally lived in the `edx-platform`_ repo, but now exists here independently.

Expand Down
2 changes: 1 addition & 1 deletion calc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@

from .calc import *

__version__ = '2.0.1'
__version__ = '3.0.0'
Carlos-Muniz marked this conversation as resolved.
Show resolved Hide resolved
21 changes: 10 additions & 11 deletions calc/calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import numbers
import operator

from functools import reduce
import numpy
from pyparsing import (
CaselessLiteral,
Expand All @@ -28,7 +29,7 @@
)

from . import functions
from functools import reduce


# Functions available by default
# We use scimath variants which give complex results when needed. For example:
Expand Down Expand Up @@ -86,14 +87,12 @@ class UndefinedVariable(Exception):
"""
Indicate when a student inputs a variable which was not expected.
"""
pass


class UnmatchedParenthesis(Exception):
"""
Indicate when a student inputs a formula with mismatched parentheses.
"""
pass


def lower_dict(input_dict):
Expand Down Expand Up @@ -216,14 +215,14 @@ def eval_product(parse_result):
return prod


def add_defaults(variables, functions, case_sensitive):
def add_defaults(additional_variables, additional_functions, case_sensitive):
"""
Create dictionaries with both the default and user-defined variables.
"""
all_variables = dict(DEFAULT_VARIABLES)
all_functions = dict(DEFAULT_FUNCTIONS)
all_variables.update(variables)
all_functions.update(functions)
all_variables.update(additional_variables)
all_functions.update(additional_functions)

if not case_sensitive:
all_variables = lower_dict(all_variables)
Expand All @@ -232,7 +231,7 @@ def add_defaults(variables, functions, case_sensitive):
return (all_variables, all_functions)


def evaluator(variables, functions, math_expr, case_sensitive=False):
def evaluator(variables, unary_functions, math_expr, case_sensitive=False):
"""
Evaluate an expression; that is, take a string of math and return a float.

Expand All @@ -250,7 +249,7 @@ def evaluator(variables, functions, math_expr, case_sensitive=False):
math_interpreter.parse_algebra()

# Get our variables together.
all_variables, all_functions = add_defaults(variables, functions, case_sensitive)
all_variables, all_functions = add_defaults(variables, unary_functions, case_sensitive)

# ...and check them
math_interpreter.check_variables(all_variables, all_functions)
Expand Down Expand Up @@ -355,7 +354,7 @@ def parse_algebra(self):
inner_number = Combine(inner_number)

# SI suffixes and percent.
number_suffix = MatchFirst(Literal(k) for k in SUFFIXES.keys())
number_suffix = MatchFirst(Literal(k) for k in SUFFIXES)

# 0.33k or 17
plus_minus = Literal('+') | Literal('-')
Expand Down Expand Up @@ -459,7 +458,7 @@ def check_variables(self, valid_variables, valid_functions):
casify = lambda x: x.lower() # Lowercase for case insens.

bad_vars = {var for var in self.variables_used
if casify(var) not in valid_variables}
if casify(var) not in valid_variables}

if bad_vars:
varnames = ", ".join(sorted(bad_vars))
Expand All @@ -479,7 +478,7 @@ def check_variables(self, valid_variables, valid_functions):
raise UndefinedVariable(message)

bad_funcs = {func for func in self.functions_used
if casify(func) not in valid_functions}
if casify(func) not in valid_functions}
if bad_funcs:
funcnames = ', '.join(sorted(bad_funcs))
message = f"Invalid Input: {funcnames} not permitted in answer as a function"
Expand Down
2 changes: 1 addition & 1 deletion calc/preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
string of latex, store it in a custom class `LatexRendered`.
"""

from .calc import DEFAULT_FUNCTIONS, DEFAULT_VARIABLES, SUFFIXES, ParseAugmenter
from functools import reduce
from .calc import DEFAULT_FUNCTIONS, DEFAULT_VARIABLES, SUFFIXES, ParseAugmenter


class LatexRendered:
Expand Down
Loading