-
Notifications
You must be signed in to change notification settings - Fork 69
Add 'make eval'. Fix crash in pprint(). Reformat some files. #237
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
Changes from all commits
d664fcc
74624d1
0777bf0
45741c5
b54d423
e16d909
35a44a1
2e37350
ea2a03c
4ede33f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -48,20 +48,25 @@ def pretty_print(obj: object, prefix: str = "", suffix: str = "") -> None: | |||||||||
| import pprint | ||||||||||
|
|
||||||||||
| line_width = min(200, shutil.get_terminal_size().columns) | ||||||||||
| print(pprint.pformat(obj, width=line_width)) | ||||||||||
| print(prefix + pprint.pformat(obj, width=line_width) + suffix) | ||||||||||
|
|
||||||||||
|
|
||||||||||
| def format_code(text: str, line_width=None) -> str: | ||||||||||
| """Format a Python literal expression using pprint. | ||||||||||
|
|
||||||||||
| NOTE: The text must be a valid Python literal expression (as produced by repr()). | ||||||||||
| Falls back to plain text formatting if the text is not a valid literal. | ||||||||||
| """ | ||||||||||
| import ast | ||||||||||
| import pprint | ||||||||||
|
|
||||||||||
| if line_width is None: | ||||||||||
| line_width = min(200, shutil.get_terminal_size().columns) | ||||||||||
| return pprint.pformat(ast.literal_eval(text), width=line_width) | ||||||||||
| try: | ||||||||||
| return pprint.pformat(ast.literal_eval(text), width=line_width) | ||||||||||
| except (ValueError, SyntaxError): | ||||||||||
| # Fall back to simple pprint of the string itself if it's not a valid literal | ||||||||||
| return pprint.pformat(text, width=line_width) | ||||||||||
|
|
||||||||||
|
|
||||||||||
| def reindent(text: str) -> str: | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That suggestion would make the code incorrect. |
||||||||||
|
|
||||||||||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pprint.pformaton a plain string wraps it in quotes — returningtextdirectly avoids that.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's put a pin in that until we see its effect. (In any case you would have to update the comment too.)