File size: 3,243 Bytes
60ab5f6 | 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 53 54 55 56 57 58 59 60 61 62 63 64 | import os
import inspect
import importlib.util
# Define the path to the directory containing the subdirectories
directory_path = 'optuna/optuna'
with open('output.txt', 'w') as f:
print("Printing the files from which classes could not be extracted:-")
# Loop over the subdirectories
for root, dirs, files in os.walk(directory_path):
# Loop over the files in the directory
for file in files:
# Check if the file is a Python file
try:
if file.endswith('.py'):
# Get the module name
module_name = os.path.splitext(file)[0]
# Get the full path to the module
module_path = os.path.join(root, file)
# Load the module as a spec
print(module_path)
module_spec = importlib.util.spec_from_file_location(module_name, module_path)
module = importlib.util.module_from_spec(module_spec)
module_spec.loader.exec_module(module)
f.write("----------------------------------------------------------\n")
f.write(f"{module_path}\n")
f.write("----------------------------------------------------------\n")
# Get the classes defined in the module
classes = inspect.getmembers(module, inspect.isclass)
# Loop over the classes
for class_tuple in classes:
# Get the class name and object
class_name = class_tuple[0]
class_obj = class_tuple[1]
# Check if the class was defined in the current module
if class_obj.__module__ == module_name:
# Get the functions in the class
functions = inspect.getmembers(class_obj, inspect.isfunction)
# Print the class name and associated functions
f.write(f'Class: {class_name}\n')
for function in functions:
if(function[0]=="__init__"):
f.write(f'{function[0]}: ')
sig = inspect.signature(function[1])
for param in sig.parameters.values():
if param.name != 'self':
f.write(f"{param.name}, ")
f.write("\n")
else:
f.write(f'{function[0]}, ')
f.write("\n")
f.write("\n")
only_functions = inspect.getmembers(module, inspect.isfunction)
f.write("Functions Outside Class: \n")
for func in only_functions:
if(func[1].__module__==module_name):
f.write(f' {func[0]}\n')
f.write("\n")
except Exception as e:
print(module_path, e) |