Python Forum

Full Version: list all functions in a package
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I am used to computing but not in python. Can please someone help me ?

I am trying to use a big package and I need to search some functions inside, so, I need these names.

A sample code begins like

from mypackage import module1
from mypackage.api import module2, module3
from mypackage.models import module4, module5, module6

I found things like help(module1) or print(dir(module1)) but impossible for me to scan all "mypackage". I in fact would like to know all the functions that come with

pip install git+http://github.com/my/package-sdk.git

Thanks :)

aka
You could start with this script list_subpackges.py to list all the subpackages of a package.

By modifying the script, it should be easy to list all the functions of each subpackage.
Does this package not have any documentation?
import sys
from importlib import import_module
from inspect import isclass, isfunction

if len(sys.argv) != 2:
    raise SystemExit("Module name is required")


def get_functions(module, private=False):
    module = import_module(module)
    functions = []
    classes = []
    for name, object in vars(module).items():
        if not private and name.startswith("_"):
            continue

        if isfunction(object):
            functions.append(name)
        elif isclass(object):
            classes.append(name)

    return functions, classes


if __name__ == "__main__":
    functions, classes = get_functions(sys.argv[1])

    indent = " " * 4
    print()
    print("Functions:")
    for func in functions:
        print(indent, func)

    print()
    print("Classes:")
    for cls in classes:
        print(indent, cls)

    print()
Output:
[andre@andre-Fujitsu-i5 ~]$ python scan_func.py pathlib Functions: urlquote_from_bytes Classes: Sequence attrgetter PurePath PurePosixPath PureWindowsPath Path PosixPath WindowsPath
Here you find more functions, to inspect the code: https://docs.python.org/3/library/inspect.html
Gone look at fantastic Rich✨ and pdir.
For me a fast way and what i always use when inspect a package or help someone with make package(many often get trouble) is ptpython.
Show all method automatic.
[Image: LKnPan.png]

Rich using inspect.
[Image: E3YMte.png]

Pdir2.
[Image: 62mkHr.png]

Rich example all string method with help in one page.
[Image: gNOIDB.png]
(Mar-10-2022, 09:35 AM)ndc85430 Wrote: [ -> ]Does this package not have any documentation?

I don't thik so..

https://github.com/pfpayments/python-sdk

I don't see any one (for exactly python code).