Skip to content

Commit 4302237

Browse files
authored
Bye-bye python_form parameter. (#554)
* removing python_form parameter from to_python
1 parent fa3f252 commit 4302237

File tree

4 files changed

+19
-25
lines changed

4 files changed

+19
-25
lines changed

mathics/builtin/inout.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,9 +439,14 @@ class PythonForm(Builtin):
439439
def apply_python(self, expr, evaluation) -> Expression:
440440
"MakeBoxes[expr_, PythonForm]"
441441

442+
def build_python_form(expr):
443+
if isinstance(expr, Symbol):
444+
return expr.to_sympy()
445+
return expr.to_python()
446+
442447
try:
443448
# from trepan.api import debug; debug()
444-
python_equivalent = expr.to_python(python_form=True)
449+
python_equivalent = build_python_form(expr)
445450
except Exception:
446451
return
447452
return StringFromPython(python_equivalent)

mathics/core/element.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -422,20 +422,14 @@ def user_hash(self, update) -> None:
422422
# __hash__ might only hash a sample of the data available.
423423
raise NotImplementedError
424424

425-
def to_python(self, *args, python_form: bool = False, **kwargs):
425+
def to_python(self, *args, **kwargs):
426426
# Returns a native builtin Python object
427427
# something in (int, float, complex, str, tuple, list or dict.).
428428
# (See discussions in
429429
# https://github.com/Mathics3/mathics-core/discussions/550
430430
# and
431431
# https://github.com/Mathics3/mathics-core/pull/551
432432
#
433-
#
434-
# if n_evaluation is an Evaluation object, then the expression
435-
# is passed by an eval_N().
436-
# If python_form is True, the standard behaviour is changed,
437-
# and it seems to behave like to_sympy....
438-
439433
raise NotImplementedError
440434

441435
def to_mpmath(self):

mathics/core/expression.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1382,7 +1382,9 @@ def to_python(self, *args, **kwargs):
13821382
"""
13831383
from mathics.builtin.base import mathics_to_python
13841384

1385-
n_evaluation = kwargs.get("n_evaluation")
1385+
n_evaluation = kwargs.get("n_evaluation", None)
1386+
assert n_evaluation is None
1387+
13861388
head = self._head
13871389
if head is SymbolFunction:
13881390

@@ -1416,6 +1418,9 @@ def to_python(self, *args, **kwargs):
14161418
# keywords=[],
14171419
# )
14181420
return py_obj
1421+
1422+
# Notice that in this case, `to_python` returns a Mathics Expression object,
1423+
# instead of a builtin native object.
14191424
return self
14201425

14211426
def to_sympy(self, **kwargs):

mathics/core/symbols.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -596,22 +596,12 @@ def to_python(self, *args, python_form: bool = False, **kwargs):
596596
if value is not self:
597597
return value.to_python()
598598

599-
if python_form:
600-
# TODO: consider to return self.value if available.
601-
# For general symbols, one possibility would be to
602-
# return a sympy symbol, also stored in value.
603-
604-
# Also, why we need this parameter? If the idea is
605-
# that to_python returns native (builtin) Python types,
606-
# then to get a sympy object, we should use to_sympy.
607-
return self.to_sympy(**kwargs)
608-
else:
609-
# For general symbols, the default behaviour is
610-
# to return a 'str'. The reason seems to be
611-
# that native (builtin) Python types
612-
# are better for being used as keys in
613-
# dictionaries.
614-
return self.name
599+
# For general symbols, the default behaviour is
600+
# to return a 'str'. The reason seems to be
601+
# that native (builtin) Python types
602+
# are better for being used as keys in
603+
# dictionaries.
604+
return self.name
615605

616606
def to_sympy(self, **kwargs):
617607
from mathics.builtin import mathics_to_sympy

0 commit comments

Comments
 (0)