-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-function-reference-table.py
52 lines (42 loc) · 1.68 KB
/
create-function-reference-table.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import inspect
from prettytable import PrettyTable
def myfunc1(my_age: int, other_age: int):
"""
This is a function to compare 2 ages
"""
if my_age == other_age:
age_equal = True
else:
age_equal = False
return print(f'Age equal: {age_equal}')
def myfunc2(birth_town: str, country: str):
"""
This is a function to print the birth town and country of birth
"""
return print(f'I live in {birth_town} in {country}')
# Get the current module (the module where your script is running)
current_module = inspect.currentframe().f_globals
# Initialize a pretty table to store function information
function_table = PrettyTable()
function_table.field_names = ["Name", "Arguments", "Type Hints", "Docstring"]
# Filter and extract only functions
functions = [item for item in current_module.items() if inspect.isfunction(item[1])]
# Loop through the list of functions and gather information
for name, function in functions:
if name != 'main': # Exclude the 'main' function if it exists
# Get function docstring
docstring = inspect.getdoc(function)
# Get function signature (arguments and type hints)
signature = inspect.signature(function)
args = []
type_hints = []
for param_name, param in signature.parameters.items():
args.append(param_name)
type_hints.append(str(param.annotation))
# Add the function information to the table
function_table.add_row([name, ", ".join(args), ", ".join(type_hints), docstring])
# Set left alignment for all columns
function_table.align = "l"
# Print the nicely formatted table
print("Function Reference Table:")
print(function_table)