Python Forum
list all functions in a package
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list all functions in a package
#4
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
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
list all functions in a package - by aka - Mar-10-2022, 08:59 AM
RE: list all functions in a package - by ndc85430 - Mar-10-2022, 09:35 AM
RE: list all functions in a package - by aka - Mar-16-2022, 07:38 AM
RE: list all functions in a package - by DeaD_EyE - Mar-10-2022, 09:44 AM
RE: list all functions in a package - by snippsat - Mar-10-2022, 11:19 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  pip list to show all versions of a package installed pvbadiger05b 4 2,750 May-21-2020, 06:48 PM
Last Post: jameshonest
  Multiple lambda functions in zipped list not executing psolar 0 1,673 Feb-13-2020, 12:53 PM
Last Post: psolar
  list of functions trazom 3 2,708 Nov-19-2018, 02:57 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020