Use ".value" in expr.to_python() and more often #1417
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
strappears. 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.valueof strings found in parsing, we are not only saving time in creation by not copying, but also memory. And we save time into_python()access as well. In some cases, like datetime objects, we can just skipto_python()and use.valueinstead.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
tupletypes, whereasto_python ()returnslists. 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.atomshave been put in alphabetical order. A bug inconvert_expression_elementsin setting.valuewas fixed. I suspect it wasn't noticed before because we were not making use of.value.Some of the
FilePrinterror messages have been aligned better for WMA's (and this makes the errors clearer, too.)