Skip to content

Conversation

@rocky
Copy link
Member

@rocky rocky commented Jul 7, 2025

Make use of the ".value" field in BaseElement objects, especially Expression.to_python().

Recall, the parser creates Python equivalents for certain literal values. In particular, a list of strings. For this, a Python tuple of str appears. And this kind of thing happens a lot. And note that in contrast to Mathics3 strings, a Python string can be duplicated. So by using .value of strings found in parsing, we are not only saving time in creation by not copying, but also memory. And we save time in to_python() access as well. In some cases, like datetime objects, we can just skip to_python() and use .value instead.

This motivation for this came from a desire to do a similar thing for SymPy and NumPy expressions, but that will have to wait.

In the parser, list element literals are tuple types, whereas to_python () returns lists. In the places where a Python list for a literal was expected, we have a Python tuple for the literal. In these cases, the code has been adjusted to allow both tuple and list types. Where certain list-only operations like mutation were needed, tuples are converted to lists in a new variable.

Methods in module mathics.core.atoms have been put in alphabetical order. A bug in convert_expression_elements in setting .value was fixed. I suspect it wasn't noticed before because we were not making use of .value.

Some of the FilePrint error messages have been aligned better for WMA's (and this makes the errors clearer, too.)

This means that where previously we had list literals, we now have tuple literals
@rocky rocky requested a review from mmatera July 7, 2025 21:26
Copy link
Contributor

@mmatera mmatera left a comment

Choose a reason for hiding this comment

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

LGTM

@rocky rocky merged commit 39acb51 into master Jul 8, 2025
15 checks passed
@rocky rocky deleted the numeric_lists branch July 8, 2025 01:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants