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
#1
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
Reply
#2
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.
Reply
#3
Does this package not have any documentation?
Reply
#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
#5
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]
DeaD_EyE and ibreeden like this post
Reply
#6
(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).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pip list to show all versions of a package installed pvbadiger05b 4 2,707 May-21-2020, 06:48 PM
Last Post: jameshonest
  Multiple lambda functions in zipped list not executing psolar 0 1,662 Feb-13-2020, 12:53 PM
Last Post: psolar
  list of functions trazom 3 2,685 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