1

I have the following problem:

Let's say I have a directory and inside this directory is another directory that contains a hello_world.py file with the following function:

def print_hello_world():
   print("Hello World")

Hierarchy: folder1/folder2/hello_world.py

Now I have in folder1 a print_function_names.py file that checks if folder2 exists and after that I iterate over the .py files in folder2 and print the function names. In this test scenario the output is:

print_hello_world

My first approach was to open the file and use a regex expression to find the function name.

folder1/print_function_names.py

with open("folder1/folder2/hello_world.py", "r") as file:
    for line in file.readlines():
        if re.match("def (.*)\(", line):
            function_name = re.match("def (.*)\(", line)
            print(function_name.group(1))

Is there maybe a more pythonic or maybe easier way?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Neal Mc Beal
  • 245
  • 3
  • 16
  • Let Python itself do the parsing. Either import the directory as a package and iterate over its contents recursively, or use the [`ast`](https://docs.python.org/3/library/ast.html) module – mkrieger1 Nov 19 '19 at 13:23
  • See also https://stackoverflow.com/questions/139180/how-to-list-all-functions-in-a-python-module or https://stackoverflow.com/questions/40406148/parsing-python-file-for-all-methods-and-classes. – mkrieger1 Nov 19 '19 at 13:25

3 Answers3

2

as stated in the comments, you can use the ast module to parse the python files without using regex/any other workaround to find all the function. here's an example that parse all function defined in a given python file

import ast

def find_methods_in_python_file(file_path):
    methods = []
    o = open(file_path, "r")
    text = o.read()
    p = ast.parse(text)
    for node in ast.walk(p):
        if isinstance(node, ast.FunctionDef):
            methods.append(node.name)

    print(methods)


find_methods_in_python_file('folder2/wtf.py')

to find all python files in a directory, you can use this question, then iterate and call find_methods_in_python_file

nonamer92
  • 1,887
  • 1
  • 13
  • 24
1

You can use Abstract Syntax Trees to find function definitions (and not only!) within python code. Here's a short code snippet that should print all function definitions in a file:

import ast

class FuncLister(ast.NodeVisitor):

    def visit_FunctionDef(self, node):
        print('FunctionDef', node.name)
        self.generic_visit(node)

with open('path/to/python/file.py', 'r') as f:
    tree = ast.parse(f.read())
    print(FuncLister().visit(tree))

This will print all function definitions in that file. Put it inside a loop that iterates over files in a directory and you have all function definition names inside a directory.

iulian
  • 5,494
  • 3
  • 29
  • 39
0

You can import the hello_world file as a module in python and then inspect elements of it, checking for functions :

In [1]: import types
   ...: import folder1.folder2.hello_world as hello_world
   ...: for name in dir(hello_world):
   ...:     elt = getattr(hello_world, name)
   ...:     if type(elt) == types.FunctionType:
   ...:         print(elt.__name__)
   ...:
print_hello_world

if you have the module path as a string, e.g. file_path = 'folder1/folder2/hello_world.py', then you can use

import importlib
module_path = file_path.replace('/', '.')[:-3]
module = importlib.import_module(module_path)
Horace
  • 1,024
  • 7
  • 12