Skip to main content

IPython based debugging and exploring tool

Project description

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):
        ipydex.ips_excepthook(
            call.excinfo.type, call.excinfo.value, call.excinfo.tb, frame_upcount=0
        )

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)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ipydex-0.18.0.tar.gz (125.8 kB view details)

Uploaded Source

Built Distribution

ipydex-0.18.0-py3-none-any.whl (37.7 kB view details)

Uploaded Python 3

File details

Details for the file ipydex-0.18.0.tar.gz.

File metadata

  • Download URL: ipydex-0.18.0.tar.gz
  • Upload date:
  • Size: 125.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for ipydex-0.18.0.tar.gz
Algorithm Hash digest
SHA256 f8420b8d0933f0d240b6684ace659659742f3c4a7b8e5ef3b518d7d723a60d6f
MD5 0a10b841350208dda6346a1a45f35f83
BLAKE2b-256 e1379bf3dc6bf415451b3cfd671792bed61227d7683742f2a1707aca8a0b8f57

See more details on using hashes here.

File details

Details for the file ipydex-0.18.0-py3-none-any.whl.

File metadata

  • Download URL: ipydex-0.18.0-py3-none-any.whl
  • Upload date:
  • Size: 37.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for ipydex-0.18.0-py3-none-any.whl
Algorithm Hash digest
SHA256 13b630e2ec9296a6a5ef8e0dbbd9ecd4ef24cd4262061f45c4c61472a8ec6773
MD5 a1b8eac3d9a9467c90753683c118c82c
BLAKE2b-256 593bc50d2f814fa3fedb216fe7699bf9c9808725d51f548d63e28b802975b9ce

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page