You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, the code will just read the model .py file, and check for the first class that starts with 'Model'.
However, this might lead to undesired behaviours (eg. the user may have defined many classes starting with Model, etc.).
Need a more robust way of doing it, or force a nomenclature.
def import_class_from_file(file_path: str) -> type:
# Extract directory path and file name
directory, file_name = os.path.split(file_path)
module_name = os.path.splitext(file_name)[0] # Remove extension to get module name
# Create a module from the file path
# In summary, these three lines of code are responsible for creating a module specification based on a file location, creating a module object from that specification, and then executing the module's code to populate the module object with the definitions from the Python file.
spec = importlib.util.spec_from_file_location(module_name, file_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
# Find the class dynamically
for name in dir(module):
model_class = getattr(module, name)
if isinstance(model_class, type) and name.startswith('Model'):
return model_class
# Class not found
raise ImportError("No class starting with 'Model' found in the file.")
The text was updated successfully, but these errors were encountered:
Currently, the code will just read the model .py file, and check for the first class that starts with 'Model'.
However, this might lead to undesired behaviours (eg. the user may have defined many classes starting with Model, etc.).
Need a more robust way of doing it, or force a nomenclature.
The text was updated successfully, but these errors were encountered: