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
7 changes: 6 additions & 1 deletion mathics/builtin/inout.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,9 +439,14 @@ class PythonForm(Builtin):
def apply_python(self, expr, evaluation) -> Expression:
"MakeBoxes[expr_, PythonForm]"

def build_python_form(expr):
if isinstance(expr, Symbol):
return expr.to_sympy()
return expr.to_python()

try:
# from trepan.api import debug; debug()
python_equivalent = expr.to_python(python_form=True)
python_equivalent = build_python_form(expr)
except Exception:
return
return StringFromPython(python_equivalent)
Expand Down
8 changes: 1 addition & 7 deletions mathics/core/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,20 +422,14 @@ def user_hash(self, update) -> None:
# __hash__ might only hash a sample of the data available.
raise NotImplementedError

def to_python(self, *args, python_form: bool = False, **kwargs):
def to_python(self, *args, **kwargs):
# Returns a native builtin Python object
# something in (int, float, complex, str, tuple, list or dict.).
# (See discussions in
# https://github.com/Mathics3/mathics-core/discussions/550
# and
# https://github.com/Mathics3/mathics-core/pull/551
#
#
# if n_evaluation is an Evaluation object, then the expression
# is passed by an eval_N().
# If python_form is True, the standard behaviour is changed,
# and it seems to behave like to_sympy....

raise NotImplementedError

def to_mpmath(self):
Expand Down
7 changes: 6 additions & 1 deletion mathics/core/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -1382,7 +1382,9 @@ def to_python(self, *args, **kwargs):
"""
from mathics.builtin.base import mathics_to_python

n_evaluation = kwargs.get("n_evaluation")
n_evaluation = kwargs.get("n_evaluation", None)
assert n_evaluation is None

head = self._head
if head is SymbolFunction:

Expand Down Expand Up @@ -1416,6 +1418,9 @@ def to_python(self, *args, **kwargs):
# keywords=[],
# )
return py_obj

# Notice that in this case, `to_python` returns a Mathics Expression object,
# instead of a builtin native object.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expect over time that more iterations over the code will be done and the thing that seem unusual will be addressed or there will be explanations of why they are the way they are.

return self

def to_sympy(self, **kwargs):
Expand Down
22 changes: 6 additions & 16 deletions mathics/core/symbols.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,22 +596,12 @@ def to_python(self, *args, python_form: bool = False, **kwargs):
if value is not self:
return value.to_python()

if python_form:
# TODO: consider to return self.value if available.
# For general symbols, one possibility would be to
# return a sympy symbol, also stored in value.

# Also, why we need this parameter? If the idea is
# that to_python returns native (builtin) Python types,
# then to get a sympy object, we should use to_sympy.
return self.to_sympy(**kwargs)
else:
# For general symbols, the default behaviour is
# to return a 'str'. The reason seems to be
# that native (builtin) Python types
# are better for being used as keys in
# dictionaries.
return self.name
# For general symbols, the default behaviour is
# to return a 'str'. The reason seems to be
# that native (builtin) Python types
# are better for being used as keys in
# dictionaries.
return self.name

def to_sympy(self, **kwargs):
from mathics.builtin import mathics_to_sympy
Expand Down