Fetching all the Module name, class name and method names from directory in python -


if directory contains multiple .py files say, a.py , b.py, c.py how can fetch module name , class name , method names of corresponding class, contaned in directory

to filter files in directory can use('.' refers current directory):

from os import listdir,chdir  default_path = r'c:\dev\python' modules = [ fl fl in listdir(default_path) if fl.endswith('.py') ] 

to classes , methods know can use inspect though can't use without importing module:

e.g.

from inspect import getmembers, isfunction, ismethod, isclass your_module = __import__(modules[0].split('.')[0])  class_funcs = {}     funcs = [ obj obj in getmembers(your_module) if isfunction(obj[1]) ] classes = [ obj obj in getmembers(your_module) if isclass(obj[1]) ]   cls in classes:     class_funcs[cls[0]] = [ obj obj in getmembers(cls[1]) if ismethod(obj[1]) ]  print funcs  [('function1', <function your_module.function1>), ('function2', <function your_module.function2>)]  print class_funcs  {'classname': [('__init__', <unbound method classname.__init__>), ('method_name', <unbound method classname.method_name>)]} 

that give functions, classes, classes methods in module.

if don't want import module i'm not familiar way methods(except reading file , using regex , etc.).


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -