SoFunction
Updated on 2024-11-16

IPython 8.0 Python Command Line Interactive Tools

Preface:

IPython is an enhanced version of Python's native interactive shell that performs many unusual tasks, such as helping to parallelize computation; use it primarily for the interactive help it provides, such as code coloring, improved command-line callbacks, tab completion, macro functionality, and improved interactive help.

IPython 8.0 has been a long time in the making, with major improvements to the existing codebase and several new features. New features include the use of Black to reformat code in the CLI, ghost suggestions, and better backtracking for highlighting error nodes, making complex expressions easier to debug.

1. Retrospective improvements

The previous error traceback shows a hash table (hash) for compiling the Python AST:

In [1]: def foo():
...:     return 3 / 0
...:

In [2]: foo()
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-2-c19b6d9633cf> in <module>
----> 1 foo()

<ipython-input-1-1595a74c32d5> in foo()
    1 def foo():
----> 2     return 3 / 0
    3

ZeroDivisionError: division by zero

Now that the error traceback is formatted correctly, the cell number where the error occurred is displayed:

In [1]: def foo():
...:     return 3 / 0
...:

Input In [2]: foo()
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
input In [2], in <module>
----> 1 foo()

Input In [1], in foo()
    1 def foo():
----> 2     return 3 / 0

ZeroDivisionError: division by zero

The second backtracking improvement isstack_data package integration; it provides smarter information in backtracking; it highlights the AST node where the error occurred, which helps to quickly narrow down the scope of the error.For example:

def foo(i):
    x = [[[0]]]
    return x[0][i][0]


def bar():
    return foo(0) + foo(
        1
    ) + foo(2)

call (programming)bar() raises a foo on the return line of IndexError, and IPython 8.0 can tell you where the indexing error occurred:

IndexError
Input In [2], in <module>
----> 1 bar()
        ^^^^^

Input In [1], in bar()
      6 def bar():
----> 7     return foo(0) + foo(
                            ^^^^
      8         1
         ^^^^^^^^
      9     ) + foo(2)
         ^^^^

Input In [1], in foo(i)
      1 def foo(i):
      2     x = [[[0]]]
----> 3     return x[0][i][0]
                   ^^^^^^^

Positions marked with ^ are highlighted in the terminal.

The third backtracking improvement is the most discreet, but has a big impact on productivity, appending a colon :: and line number to the filename in the backtracking:

ZeroDivisionError               Traceback (most recent call last)
File ~/:4, in <module>
      1 def f():
      2     1/0
----> 4 f()

File ~/:2, in f()
      1 def f():
----> 2     1/0

Many terminals and editors have integrated features that allow you to jump directly to the file/line associated with the error when using this syntax.

2. Auto-suggestion

Ptpython Allows the user toptpython/ The auto-suggestion feature, which includes a rich set of code-completion suggestions, is enabled in the

As pictured:

Currently, autosuggestions are only displayed in emacs or vi insertion editing mode:

ctrl ectrl f cap (a poem)alt f Shortcuts work in emacs mode by default.
To use these shortcuts in vi insertion mode, you must add a new key to the Create custom key bindings in the

3. Use of "?" and "????" Viewing Object Information

In IPDB, it is now possible to use "?" and "? ? " to display information about an object.This can also be done when using the IPython prompt:

ipdb> partial?
Init signature: partial(self, /, *args, **kwargs)
Docstring:
partial(func, *args, **keywords) - new function with partial application
of the given arguments and keywords.
File:           ~/.pyenv/versions/3.8.6/lib/python3.8/
Type:           type
Subclasses:

4. Historical scope global function

beforehand%history The user can specify a range of sessions and rows for the function.Example:

~8/1-~6/5   # see history from the first line of 8 sessions ago,
            # to the fifth line of 6 sessions ago.``

Alternatively, you can specify a global mode (global):

-g <pattern>  # glob ALL history for the specified pattern.

However, it is not possible to specify both at the same time; if the user does specify a scope and a global mode, the glob mode will be used (wildcarded for all histories) and the scope will be ignored.

This feature is now enhanced so that if the user specifies both a range and a glob mode, the glob mode will be applied to the specified history range.

To this point this article on IPython 8.0 Python command line interactive tools are introduced to this article, more related Python interactive tools content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!