1

Is there a way to retrieve all the different functions/classes of a specific package?

For example, I'd like to receive something like this for scipy:

scipy.ndimage.center_of_mass
scipy.ndimage.binary_dilation
scipy.ndimage.binary_erosion
scipy.ndimage.gaussian_filter
scipy.ndimage.filters.gaussian_filter
scipy.ndimage.filters.convolve
scipy.ndimage.sobel
scipy.ndimage.zoom
scipy.ndimage.distance_transform_edt
scipy.ndimage.filters.convolve
scipy.spatial.distance.cdist
scipy.optimize.curve_fit
scipy.signal.find_peaks
scipy.signal.correlate
scipy.signal.peak_widths
scipy.signal.find_peaks
scipy.signal.peak_widths
scipy.interpolate.LinearNDInterpolator
scipy.interpolate.interp1d
scipy.interpolate.make_interp_spline
scipy.integrate.trapz
scipy.linalg.circulant

This is just a subset of scipy, but you can get the idea. I'd like to list all the different functions/classes of that package. Is there a tool that does that maybe?

David Lasry
  • 829
  • 3
  • 12
  • 31

1 Answers1

0

There are many ways:

dir(module)

or

from inspect import getmembers, isfunction

from somemodule import foo
print(getmembers(foo, isfunction))

but in your case, scipy contains other sub packages. To print all the contents of a package, you can use .__all__:

scipy.__all__

This produce a list of all submodules, methods, functions and attributes. It is possible to select the relevant for your according to the type (module, function or other). You just need to loop over them and check their corresponding types:

for i in scipy.__all__:
    print(f"{i}: {type(getattr(scipy, i))}")

For each subpackage, you can use getmembers function from inspect to get the function and the classes of each. you can specify using is function, ismodule and ismethod what you're really looking for. For more details, https://docs.python.org/3/library/inspect.html#inspect.getmembers

import inspect

inspect.getmembers(scipy.signal, inspect.ismodule)
inarighas
  • 720
  • 5
  • 24