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

printing: Remove curly braces from subscripted variables in python() and pycode() #20806

Merged

Conversation

AaronStiff
Copy link
Contributor

References to other Issues or PRs

Fixes #20762

Brief description of what is fixed or changed

Added handling for subscripted variables in python and pycode. Subscripted variables only become an issue when using a parsed latex expression, since latex wraps them in curly braces. python and pycode were creating variable names with curly braces in them, resulting in syntax errors. Added a simple check and modification in python, but redefined inherited _print_Symbol in pycode's PythonCodePrinter.

Other comments

Release Notes

  • printing
    • Remove curly braces from subscripted variables in python() and pycode()

@sympy-bot
Copy link

sympy-bot commented Jan 15, 2021

Hi, I am the SymPy bot (v161). I'm here to help you write a release notes entry. Please read the guide on how to write release notes.

Your release notes are in good order.

Here is what the release notes will look like:

This will be added to https://github.com/sympy/sympy/wiki/Release-Notes-for-1.10.

Click here to see the pull request description that was parsed.
<!-- Your title above should be a short description of what
was changed. Do not include the issue number in the title. -->

#### References to other Issues or PRs
<!-- If this pull request fixes an issue, write "Fixes #NNNN" in that exact
format, e.g. "Fixes #1234" (see
https://tinyurl.com/auto-closing for more information). Also, please
write a comment on that issue linking back to this pull request once it is
open. -->
Fixes #20762 

#### Brief description of what is fixed or changed
Added handling for subscripted variables in `python` and `pycode`. Subscripted variables only become an issue when using a parsed latex expression, since latex wraps them in curly braces. `python` and `pycode` were creating variable names with curly braces in them, resulting in syntax errors. Added a simple check and modification in `python`, but redefined inherited `_print_Symbol` in `pycode`'s `PythonCodePrinter`.

#### Other comments


#### Release Notes

<!-- Write the release notes for this release below. See
https://github.com/sympy/sympy/wiki/Writing-Release-Notes for more information
on how to write release notes. The bot will check your release notes
automatically to see if they are formatted correctly. -->

<!-- BEGIN RELEASE NOTES -->
* printing
    * Remove curly braces from subscripted variables in python() and pycode()
<!-- END RELEASE NOTES -->

Update

The release notes on the wiki have been updated.

@AaronStiff
Copy link
Contributor Author

AaronStiff commented Jan 15, 2021

Where should I put the tests for this? They require antlr, which doesn't get imported in test_pycode.py or test_python.py.

@oscargus
Copy link
Contributor

oscargus commented Sep 5, 2021

Where should I put the tests for this?

Either you check for it in the test, something like

antlr4 = import_module("antlr4")

# disable tests if antlr4-python*-runtime is not present
if not antlr4:
    disabled = True

so import antlr in the test and check if it is present, if not just return.

Or you may put the test in https://github.com/sympy/sympy/blob/master/sympy/parsing/tests/test_latex.py (where the code above is borrowed from).

newsymbolname = symbolname
# Remove curly braces from subscripted variables
if '{' in symbolname:
newsymbolname = ''.join([s if s != '{' and s != '}' else '' for s in symbolname])
Copy link
Contributor

Choose a reason for hiding this comment

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

What about newsymbolname = symbolname.replace('{', '').replace('}', '')?

@AaronStiff
Copy link
Contributor Author

Woah, I haven't touched sympy for half a year! :)

Would you mind taking over here?

@oscargus
Copy link
Contributor

oscargus commented Sep 5, 2021

Sure, I can probably give it a go. Seems like it is just to sort out the test aspect (and possibly rewrite using replace).

@oscargus
Copy link
Contributor

oscargus commented Sep 5, 2021

Couldn't figure out how to easily get your repo to my local computer so went for the "edit on GitHub"-approach. Not a brilliant idea in hindsight, but hopefully it will pass...

@github-actions
Copy link

github-actions bot commented Sep 5, 2021

Benchmark results from GitHub Actions

Lower numbers are good, higher numbers are bad. A ratio less than 1
means a speed up and greater than 1 means a slowdown. Green lines
beginning with + are slowdowns (the PR is slower then master or
master is slower than the previous release). Red lines beginning
with - are speedups.

Significantly changed benchmark results (PR vs master)

Significantly changed benchmark results (master vs previous release)

Full benchmark results can be found as artifacts in GitHub Actions
(click on checks at the top of the PR).

@oscarbenjamin
Copy link
Contributor

Couldn't figure out how to easily get your repo to my local computer so went for the "edit on GitHub"-approach. Not a brilliant idea in hindsight, but hopefully it will pass...

My approach to doing this is twofold. To check out a PR I have the following in .git/config:

[remote "upstream"]
  url = https://github.com/sympy/sympy.git
  fetch = +refs/heads/*:refs/remotes/upstream/*
  fetch = +refs/pull/*/head:refs/remotes/upstream/pr/*
  pushurl = no_push

With that I can do git fetch upstream and then any PR is available as git checkout upsteram/pr/12345.

Then if I want to make changes and push to the PR it's:

git checkout upstream/pr/20806
# make changes
git commit -am 'message'
git push git@github.com:AaronStiff/sympy.git HEAD:issues/20762-python-printing-subscripts

The username and branch name are taken from the header of the webpage in the PR on GitHub

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

pycode of latex expression with subscript leads to syntax error
5 participants