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

Prettyform #1

Closed
wants to merge 2 commits into from
Closed

Prettyform #1

wants to merge 2 commits into from

Conversation

mmatera
Copy link
Owner

@mmatera mmatera commented Nov 11, 2024

Improving sympy.printing.pretty.stringpict API

References to other Issues or PRs

I am working on giving Mathics (Mathics3/mathics-core#1155) the ability to produce prettyprint
-like output from Mathics Expression objects. To avoid duplicating efforts, I tried to hook on the sympy.printing.pretty package, but I found some problems.
After trying to use the PrettyPrinter, I realized that since mathics.core.Expression objects behave differently than Sympy objects, the best would be to generate text blocks directly from the sympy.printing.pretty.stringpict.prettyForm class. A draft of it can be seen at Mathics3/mathics-core#1162

In this journey, I found some issues in the design of sympy.printing.pretty.stringpict that makes me difficult to use it directly on my project.
This PR includes the changes I need to make it work. Also, it probably helps with sympy#20894.

Brief description of what is fixed or changed

In this PR I propose some changes in the stringPict API, which I hope will make it easier to use it both inside Sympy or in in other projects, like Mathics.

  • the methods subscript and superscript were added to the stringPict interface
  • the methods above, below, left, right and parens were modified to return prettyForm or stringPict
    objects depending on the class of the object over which are called.
  • above, below, left, and right now support a keyword argument align that allows to control the relative alignment of the elements.
  • the root method was fixed, and used to simplify the code in PrettyPrinter._print_nth_root().
  • doctests for the modified methods were added.
  • tests and the use of the modified methods were adjusted along the code.

The idea is that stringPict.[above|below,left|right|subscript|superscript](...) work as high-level methods to build stringPict and prettyForm complex objects, leaving stringPict.[stack|next]() methods as low-level methods. High-level methods then return objects of the same type as the caller object (stringPict|prettyForm), while low-level methods return tuples of (str, int).
The use of the high-level methods allows us to avoid the idiom prettyForm(*stringPict.method(...)), which is spread all over Sympy. This behavior seems to be suggested by the implementation of stringPict.root, a method that didn't work in the master branch but does it here.

Another change included is to support horizontal and vertical alignment in text blocks, which was marked as a TODO task, and which also helps to build complex stringPicts without explicitly computing the baselines on the building routines.

The implementation of stringPict.root, which is working now, suggests that this organization was considered before.

Other comments

I tried to modify the rest of the code as little as possible to play with my project without breaking tests. I have more suggestions for improving the pretty module, but could be go in another PR.

@mmatera mmatera force-pushed the prettyform branch 3 times, most recently from eff4971 to 0f43d43 Compare November 12, 2024 01:02
…ht` methods.

* `paren`, `top`, `bottom`, `left` and `right` produce `stringPict` objects
* adding `superscript` and `subscript` methods
* fixing `root`
@mmatera mmatera force-pushed the prettyform branch 2 times, most recently from 75daec5 to 652c1c2 Compare November 13, 2024 01:08
adjust comments on root
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.

1 participant