pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/ipython/ipython/pull/14081

ets/global-0bd78641c0a1f3e0.css" /> Autobalance by jb-leger · Pull Request #14081 · ipython/ipython · GitHub
Skip to content

Autobalance#14081

Draft
jb-leger wants to merge 8 commits intoipython:mainfrom
jb-leger:autobalance
Draft

Autobalance#14081
jb-leger wants to merge 8 commits intoipython:mainfrom
jb-leger:autobalance

Conversation

@jb-leger
Copy link
Copy Markdown

This PR adds a mode for automatic opening of brackets, braces and braces.

This follows a strange idea that appeared on Saturday the 13th, following the other strange idea in #14077.

The logic used is as follows:

  • if the mode is enabled
  • and if the cell is only one line long
  • and if there are unmatched [{()}]
  • and if it is enough to add opening symbols at the beginning and/or closing symbols at the end

then the line is modified.

Technical points:

  • I chose not to use prefilters, but input transformers for this, because prefilters work with the check/handle logic, which does not allow to have at the same time the addition of symbols and the use of a magic (like %time for example).
  • the TransformerManager didn't have a member allowing to come back to the shell, and it's essential for me to access the shell config and to be able to display the modified line (when there is a modification)
  • the transformations had no way to have additional arguments and only received the lines. I had to add kwargs to be able to add arguments that transformations are free to use or not.

Demo:

Python 3.11.2 (main, Mar 13 2023, 12:18:29) [GCC 12.2.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.14.0.dev -- An enhanced Interactive Python. Type '?' for help.

In [1]: 1+1)/2
  Cell In[1], line 1
    1+1)/2
       ^
SyntaxError: unmatched ')'


In [2]: %autobalance
Autobalance enabled

In [3]: 1+1)/2
------> (1+1)/2
Out[3]: 1.0

In [4]: 1+1)/(3+4
------> (1+1)/(3+4)
Out[4]: 0.2857142857142857

In [5]: i+1)/2 for i in range(5)]+[1,2
------> [(i+1)/2 for i in range(5)]+[1,2]
Out[5]: [0.5, 1.0, 1.5, 2.0, 2.5, 1, 2]

In [6]: my_values = i+1)/2 for i in range(5)]+[1,2
------> my_values = [(i+1)/2 for i in range(5)]+[1,2]

In [7]: %config TerminalInteractiveShell.show_rewritten_input = False

In [8]: 1+1)/(3+4
Out[8]: 0.2857142857142857

This is my first time diving into IPython code (and contributing to it), I may have missed a lot things. I place this PR in draft for two reasons:

  • the changes made to the TransformerManager logic (with shell passing and kwargs on transformations) seem a bit intrusive to me, and discussing them seems like the best option.
  • I have not yet written the specific tests for this new behavior.

Finally, I think it is important to mention the adapted xkcd.


# only for type checking, do not import this, it lead to a circular
# import
if False:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You can do

from typing import TYPE_CHECKING

...

if TYPE_CHECKING:

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

fixed in 10f3017.

symbols are added in the begin of the expression, and closings symbols
at the end of the line. E.g. `1+2)/2]*(2+3` becomes `[(1+2)/2]*(2+3)`.
""",
).tag(config=True)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

👍

Comment on lines +244 to +245
shell = kwargs.get("shell")
display = kwargs.get("display", False)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I see you are adding kwargs everywhere to get shell and display here, but that will break API of external transforms that don't take a kwargs. So we may wan to have a

class Autobalancer:

    def __init__(self, shell):
        ....

    def __call__(...):



Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Very good idea, thank you. This solution is a lot less intrusive. Done in 79a23bf.

]
self.line_transforms = [
cell_magic,
autobalance,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

And here you should be able to have Autobalancer(shell)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Continuation of last thread. Done in 79a23bf.

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.

2 participants

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy