Improve prettyprinting of multidimensional numpy arrays.#50
Open
anntzer wants to merge 1 commit intotommikaikkonen:masterfrom
Open
Improve prettyprinting of multidimensional numpy arrays.#50anntzer wants to merge 1 commit intotommikaikkonen:masterfrom
anntzer wants to merge 1 commit intotommikaikkonen:masterfrom
Conversation
Items are now aligned by relying on numpy's own implementation.
Contributor
Author
|
After some more investigation, looks like the desired result (forcing a newline before the dtype if the array repr goes over multiple lines) can be achieved with diff --git i/prettyprinter/extras/numpy.py w/prettyprinter/extras/numpy.py
index e9aa1f6..7dffb34 100644
--- i/prettyprinter/extras/numpy.py
+++ w/prettyprinter/extras/numpy.py
@@ -47,8 +47,8 @@ def pretty_arraywrapper(value, ctx):
if len(lines) == 1:
return s
else:
- interspersed = [HARDLINE] * 2 * len(lines)
- interspersed[1::2] = lines
+ interspersed = [HARDLINE] * (2 * len(lines) - 1)
+ interspersed[::2] = lines
return Concat(interspersed)
diff --git i/prettyprinter/prettyprinter.py w/prettyprinter/prettyprinter.py
index 521ad01..e249284 100644
--- i/prettyprinter/prettyprinter.py
+++ w/prettyprinter/prettyprinter.py
@@ -893,6 +893,7 @@ def build_fncall(
fndoc = general_identifier(fndoc)
has_comment = bool(trailing_comment)
+ has_hardline = False
argdocs = list(argdocs)
kwargdocs = list(kwargdocs)
@@ -957,6 +958,10 @@ def build_fncall(
else:
comment_str = None
+ from .doctypes import Concat, HARDLINE
+ if isinstance(doc, Concat) and any(doc is HARDLINE for doc in doc.docs):
+ has_hardline = True
+
part = concat([doc, NIL if last else COMMA])
if comment_str:
@@ -982,7 +987,7 @@ def build_fncall(
outer = (
always_break
- if has_comment
+ if has_comment or has_hardline
else group
)(on top of this PR's patch). I guess always breaking when a subdoc has hardlines makes sense? Not sure about possible unintended consequences, though. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Items are now aligned by relying on numpy's own implementation.
Fixes most of #49.
It looks like I needed to insert hard linebreaks myself to get the right indentation. One minor remaining problematic point is that the array's dtype gets printed on the same line as the array (if it fits) rather than on a separate line:
I guess the proper fix would be to move the dtype-handling logic from pretty_numpy to pretty_arraywrapper as well, but this would also require (I think?) duplicating some of the pretty_call logic, so it won't be for this PR...