Skip to main content

Find and inspect python functions

Project description

pyinspect

If, like me, when coding often you know which function you need but can't quite remember its name, then I have good news!. pyinspect is here to help you out.

pyinspect allows you to search for functions and class methods based on their name and prints out a handy table with all the functions that met your search criteria.

Installing pyinspect

It's as simple as:

pip install pyinspect

Finding functions

The easiest way to grasp how pyinspect can help is with an example.

Imagine that you just can't remember which matplotlib.pyplot method you need to create a figure with subplots...

this is how you use pyinspect to find it:

# import the module whose functions you're looking for
import matplotlib.pyplot as plt

# import pyinspect
import pyinspect

# Find the functions you're looking for
funcs = pyinspect.search(plt, name='subplot')

This results in:

note: search also looks for functions in sub-modules of the module found. e.g. search(matplotlib...) will look for methods across the entire matplotlib library!


pyinspect.find can also be used to find class attributes. For example, say that you're looking for a method with export in the name in rich.console.Console:

# import the class you need to inspect
from rich.console import Console

# import pyinspect
import pyinspect

# find class methods
methods = pyinspect.search(Console, 'export')

Which gives:

note: search also looks for methods matching your query among the parents of the class you passed. Pass include_parents=False when calling search to restrict the search to just the class you've passed.

PRO TIP: if you don't pass a search string (e.g. pyinspect.find(Console)), pyinspect.find will print all functions and methods.

\

Inspecting functions

Okay, you've found the function you need, that's great. But how does it work?

What if, in addition to the exact name, you've forgotten which arguments it takes, or how it works exactly. Well, pyinspect can help you there as well! You can use pyinspect.print_function to print the source code of any function or attribute:

# import the class you're using
from rich.console import Console


# import pyinspect
import pyinspect

# Print a function's source code
pyinspect.print_function(Console.export_text)

which yields:

Tracebacks

Finally, pyinspect builds upon rich's awesome traceback functionality to print nicely formatted tracebacks and show the local variables when the exception came up.

E.g.:

# import pyinspect and install the traceback handler
import pyinspect
pyinspect.install_traceback()

# make some buggy code
import numpy as np

def sum(a, b):
    return a + b

a = np.ones(5)
b = np.zeros(4)

sum(a, b)

and this is the traceback:

Aknowledgements

pyinspect is mostly a thin wrapper on top of the awesome rich library, so a huge thank you goes to @willmcgugan for the great job done with rich.

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

pyinspect-0.0.3.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyinspect-0.0.3-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file pyinspect-0.0.3.tar.gz.

File metadata

  • Download URL: pyinspect-0.0.3.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.4

File hashes

Hashes for pyinspect-0.0.3.tar.gz
Algorithm Hash digest
SHA256 9795eed218d1defde2b81faad863d28006addf845aba7ab105fc229996498af6
MD5 9720794eb7ca22a842504775c1a35553
BLAKE2b-256 33ac1f933e6572d373e3c22d95f1f8168cf96aceef73876e512b351f312455d3

See more details on using hashes here.

File details

Details for the file pyinspect-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: pyinspect-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.4

File hashes

Hashes for pyinspect-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a784b6758a703afa4b076a2d0a800ac00dd931909e2e957cc08f5aa812828d55
MD5 e97f336f86ce4bcc502d780e4a35a7b4
BLAKE2b-256 d7962009de50f1bbd98eb5d9b4584d441bb68ab35278ea14e99c460a4154a610

See more details on using hashes here.

Supported by

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