I have a python module installed on my system and I\'d like to be able to see what functions/classes/methods are available in it.
I want to call the doc function
The inspect module. Also see the pydoc module, the help()
function in the interactive interpreter and the pydoc
command-line tool which generates the documentation you are after. You can just give them the class you wish to see the documentation of. They can also generate, for instance, HTML output and write it to disk.
r = globals()
sep = '\n'+100*'*'+'\n' # To make it clean to read.
for k in list(r.keys()):
try:
if str(type(r[k])).count('function'):
print(sep+k + ' : \n' + str(r[k].__doc__))
except Exception as e:
print(e)
Output :
******************************************************************************************
GetNumberOfWordsInTextFile :
Calcule et retourne le nombre de mots d'un fichier texte
:param path_: le chemin du fichier à analyser
:return: le nombre de mots du fichier
******************************************************************************************
write_in :
Ecrit les donnees (2nd arg) dans un fichier txt (path en 1st arg) en mode a,
:param path_: le path du fichier texte
:param data_: la liste des données à écrire ou un bloc texte directement
:return: None
******************************************************************************************
write_in_as_w :
Ecrit les donnees (2nd arg) dans un fichier txt (path en 1st arg) en mode w,
:param path_: le path du fichier texte
:param data_: la liste des données à écrire ou un bloc texte directement
:return: None
import types
import yourmodule
print([getattr(yourmodule, a) for a in dir(yourmodule)
if isinstance(getattr(yourmodule, a), types.FunctionType)])
For completeness' sake, I'd like to point out that sometimes you may want to parse code instead of importing it. An import
will execute top-level expressions, and that could be a problem.
For example, I'm letting users select entry point functions for packages being made with zipapp. Using import
and inspect
risks running astray code, leading to crashes, help messages being printed out, GUI dialogs popping up and so on.
Instead I use the ast module to list all the top-level functions:
import ast
import sys
def top_level_functions(body):
return (f for f in body if isinstance(f, ast.FunctionDef))
def parse_ast(filename):
with open(filename, "rt") as file:
return ast.parse(file.read(), filename=filename)
if __name__ == "__main__":
for filename in sys.argv[1:]:
print(filename)
tree = parse_ast(filename)
for func in top_level_functions(tree.body):
print(" %s" % func.name)
Putting this code in list.py
and using itself as input, I get:
$ python list.py list.py
list.py
top_level_functions
parse_ast
Of course, navigating an AST can be tricky sometimes, even for a relatively simple language like Python, because the AST is quite low-level. But if you have a simple and clear use case, it's both doable and safe.
Though, a downside is that you can't detect functions that are generated at runtime, like foo = lambda x,y: x*y
.
You can use the following method to get list all the functions in your module from shell:
import module
module.*?