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

Mathicssession #1001

Merged
merged 2 commits into from
Nov 14, 2020
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
25 changes: 25 additions & 0 deletions mathics/session.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from mathics.core.parser import parse, SingleLineFeeder
from mathics.core.definitions import Definitions
from mathics.core.evaluation import Evaluation
from mathics import settings


class MathicsSession:
def __init__(self, add_builtin=True, catch_interrupt=False, form="InputForm"):
self.definitions = Definitions(add_builtin)
self.evaluation = Evaluation(definitions=self.definitions, catch_interrupt=catch_interrupt)
self.form = form
self.last_result = None

def evaluate(self, str_expression, timeout = None, form=None):
expr = parse(self.definitions, SingleLineFeeder(str_expression))
if form is None:
form = self.form
self.last_result = expr.evaluate(self.evaluation)
return self.last_result

def format_result(self, str_expression=None, timeout = None, form=None):
self.last_result
if form is None:
form = self.form
return res.do_format(self.evaluation, form)
20 changes: 11 additions & 9 deletions test/test_evaluation.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@

from mathics.session import MathicsSession

from mathics.core.parser import parse, SingleLineFeeder
from mathics.core.definitions import Definitions
from mathics.core.evaluation import Evaluation
import pytest


definitions = Definitions(add_builtin=True)
evaluation = Evaluation(definitions=definitions, catch_interrupt=False)
session = MathicsSession(add_builtin=True, catch_interrupt=False)


def _evaluate(str_expression):
expr = parse(definitions, SingleLineFeeder(str_expression))
return expr.evaluate(evaluation)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -49,9 +48,11 @@ def _evaluate(str_expression):
(r'StringInsert[{"abcdefghijklm", "Mathics"}, "X", {}]', r'{"abcdefghijklm", "Mathics"}'),
],
)

def test_evaluation(str_expr: str, str_expected: str, message=""):
result = _evaluate(str_expr)
expected = _evaluate(str_expected)
global session
result = session.evaluate(str_expr)
expected = session.evaluate(str_expected)

if message:
assert result == expected, message
Expand All @@ -60,14 +61,15 @@ def test_evaluation(str_expr: str, str_expected: str, message=""):


def test_exit():
global session
try:
_evaluate("Exit[-37]")
session.evaluate("Exit[-37]")
except SystemExit as e:
assert e.code == -37


def test_quit():
try:
_evaluate("Quit[-37]")
session.evaluate("Quit[-37]")
except SystemExit as e:
assert e.code == -37