Skip to main content

ipydex – ipython based debugging and exploring

CircleCI PyPI version

The module contains two main components:

Component 1: displaytools

  • a jupyter-notebook-extension (%loadext ipydex.displaytools)
  • introduces magic comments (like ##:, ##:T, ##:S) which cause that either the return value or the right hand side of an assignment of a line is displayed (T means additional transposition and S means only .shape attribute is displayed)
  • display intermediate results (→ more readable notebooks), without introducing additional print or display statements
  • Example invocation: x = np.random.rand() ##:
    • inserts the line display("x := {}".format(x)) to the source code of the cell (before its execution)
  • see documentation-notebook

Security advice: Because the extension manipulates the source code before its execution, it might cause unwanted and strange behavior. Thus, this program is distributed in the hope that it will be useful, but without any warranty.

Component 2: Useful Python functions and classes

The following functions are meant to be used in ordinary python-scripts:

  • IPS()
    • start an embedded IPython shell in the calling scope
    • useful to explore what objects are available and what are their abilities
    • some additional features compared to IPython.embed()
  • ST()
    • start the IPython debugger
  • activate_ips_on_exception()
    • activate an embedded IPython shell in the scope where an exception occurred
    • useful to investigate what happened
    • see below how to make use of in connection with pytest
    • set magic variable __mu to 1 and exit the shell (CTRL+D) in order to move up one level in the frame stack
      • useful to determine the reason of an exception (which is often not in the same frame as where the exception finally happened)
  • dirsearch(name, obj)
    • search the keys of a dict or the attributes of an object
    • useful to explore semi known modules, classes and data-structures
  • Container
    • versatile class for debugging and convenient creation of case-specific data structures

Notes

This package has grown over more than a decade. It is only partially covered by unittests. Its internals are not exemplary for recommended coding practice. It certainly contains bugs. No warranty for any purpose is given.

Nevertheless it might be useful.

ipydex Usage in Unittests (Using pytest)

In your test directory add a file conftest.py:

# This file enables the ipydex excepthook together with pytest.
# The custom excepthook can be activated by `activate_ips_on_exception()`
# in your test-file.

# To prevent unwanted dependencies the custom excepthook is only active if a
# special environment variable is "True". Use the following command for this:
#
# export PYTEST_IPS=True


import os
if os.getenv("PYTEST_IPS") == "True":

    import ipydex

    # This function is just an optional reminder
    def pytest_runtest_setup(item):
        print("This invocation of pytest is customized")


    def pytest_exception_interact(node, call, report):
        # the option `leave_ut=True` causes the excepthook to leave functions
        # from the unittest package. This is a convenience feature such that
        # the code wakes up in your own testcode
        ipydex.ips_excepthook(
            call.excinfo.type, call.excinfo.value, call.excinfo.tb, leave_ut=True
        )

Use ipydex.Container for Debugging e.g. in Jupyter Notebooks

from ipydex import Container

# ...

def func1(x, debug_container=None):
    y = complicated_func1(x)
    res = complicated_func2(x, y)

    # convenient way to non-intrusively gather internal information
    if debug_container is not None:
        debug_container.fetch_locals()
        # now the following attributes exists:
        # debug_container.x
        # debug_container.y
        # debug_container.res

    return res

# create debug container
dc = Container()

# call the function which should be debugged, pass the container
# as keyword argument
res = func1(100, debug_container=dc)

# after the function returned dc contains new attributes which allow to
# investigate *internal* behavior of func1
print(C.x)
print(C.y)
print(C.res)

Metadata

Release files for ipydex 0.21.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ipydex 0.21.1
File Size Uploaded
ipydex-0.21.1.tar.gz 131.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ipydex 0.21.1
File Interpreter ABI Platform
ipydex-0.21.1-py3-none-any.whl Python 3 none any Details

Total release size: 172.7 kB

Release files / ipydex-0.21.1.tar.gz

Download URL ipydex-0.21.1.tar.gz
Size 131.0 kB
Tags Source
SHA-256 checksum
How to use checksums
b0c53700fd685d26be41309690190302e5776b24ee88c1856405ffeae1d15736
BLAKE2b-256 checksum
How to use checksums
2e6caaabb5da8afa0f4b9eb137acf74ca76ab7bbb892c23d92a562e7fca36dbb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.2

Release files / ipydex-0.21.1-py3-none-any.whl

Download URL ipydex-0.21.1-py3-none-any.whl
Size 41.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0af3c5f3254d007e7358b356b31b84eec62956f86e819109369b32e735f15805
BLAKE2b-256 checksum
How to use checksums
d41a9ca9eaf62f3000eaee1fa061c1dbf304a9069fd96eee9d3fc730b0597aa0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.2

Release history Release notifications | RSS feed

This release

0.21.1 This release

2 release files

0.20.0

2 release files

0.19.0

2 release files

0.18.0

2 release files

0.16.1

2 release files

0.16.0

2 release files

0.15.1

2 release files

0.15.0

2 release files

0.14.1

1 release file

0.14.0

1 release file

0.13.0

1 release file

0.12.0

1 release file

0.11.3

1 release file

0.11.2

1 release file

0.10.5

1 release file

0.10.4

1 release file

0.10.3

1 release file

0.10.2

1 release file

0.10.1

1 release file

0.8.2

1 release file

0.8.1

1 release file

0.8.0

1 release file

0.6.1

1 release file

0.6.0

1 release file

0.5.2

1 release file

0.5.1

1 release file

0.5.0

1 release file

0.2.1

1 release file

0.2

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page