Skip to content

Commit

Permalink
fix code to only list callables once
Browse files Browse the repository at this point in the history
  • Loading branch information
bbelderbos committed Oct 5, 2023
1 parent 9413ba9 commit bfe55d0
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions notes/20231005125327.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,24 @@ def get_callables_from_file(filename):
callables = []
def visit_callables(node):
# For top-level functions
if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
callables.append(node.name)
# For class methods
elif isinstance(node, ast.ClassDef):
# Inside a class, continue checking for methods
for child in ast.iter_child_nodes(node):
if isinstance(child, (ast.FunctionDef, ast.AsyncFunctionDef)):
callables.append(f"{node.name}.{child.name}")
for child in ast.iter_child_nodes(node):
visit_callables(child)
visit_callables(tree)
# Initiate the traversal with the top-level nodes
for top_level_node in tree.body:
visit_callables(top_level_node)
return callables
# Example usage:
filename = 'path_to_your_file.py'
filename = 'test_file.py'
print(get_callables_from_file(filename))
```

Expand Down

0 comments on commit bfe55d0

Please sign in to comment.