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.
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 thesympy.printing.pretty
package, but I found some problems.After trying to use the
PrettyPrinter
, I realized that sincemathics.core.Expression
objects behave differently than Sympy objects, the best would be to generate text blocks directly from thesympy.printing.pretty.stringpict.prettyForm
class. A draft of it can be seen at Mathics3/mathics-core#1162In 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.subscript
andsuperscript
were added to thestringPict
interfaceabove
,below
,left
,right
andparens
were modified to returnprettyForm
orstringPict
objects depending on the class of the object over which are called.
above
,below
,left
, andright
now support a keyword argumentalign
that allows to control the relative alignment of the elements.root
method was fixed, and used to simplify the code inPrettyPrinter._print_nth_root()
.The idea is that
stringPict.[above|below,left|right|subscript|superscript](...)
work as high-level methods to buildstringPict
andprettyForm
complex objects, leavingstringPict.[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 ofstringPict.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.