Dynamic Module Loading: It dynamically imports Python modules based on the filenames in the current directory.
Function Invocation: It calls a specific function (helloworld) from each imported module.
This approach leverages the exec function to execute import statements and function calls at runtime, which allows for flexible and dynamic code execution.
in other words a DLC(downloadable/addable contents)
code example(function without parameters):
code example(function without parameters, envokes only (.py) files with DLC in their name):
code example passing string param:
code example passing an object as a parameter for the invoked function:
Function Invocation: It calls a specific function (helloworld) from each imported module.
This approach leverages the exec function to execute import statements and function calls at runtime, which allows for flexible and dynamic code execution.
in other words a DLC(downloadable/addable contents)
code example(function without parameters):
Code:
import os
def call_helloworld_in_all_files():
for file in os.listdir('.'):
if file.endswith('.py') and file != os.path.basename(__file__):
module_name = file[:-3]
exec(f"import {module_name}")
exec(f"{module_name}.helloworld()")
if __name__ == "__main__":
call_helloworld_in_all_files()
code example(function without parameters, envokes only (.py) files with DLC in their name):
Code:
import os
def call_helloworld_in_all_files():
for file in os.listdir('.'):
if file.endswith('.py') and 'DLC' in file:
module_name = file[:-3]
exec(f"import {module_name}")
exec(f"{module_name}.helloworld()")
if __name__ == "__main__":
call_helloworld_in_all_files()
code example passing string param:
Code:
import os
def call_helloworld_in_all_files(param:str):
for file in os.listdir('.'):
if file.endswith('.py') and 'DLC' in file:
module_name = file[:-3]
exec(f"import {module_name}")
exec(f"{module_name}.helloworld('{param}')")
if __name__ == "__main__":
call_helloworld_in_all_files("working")
code example passing an object as a parameter for the invoked function:
Code:
# noinspection PyUnusedLocal
def call_add_DLC_skills(brain: Brain):
for file in os.listdir('.'):
if file.endswith('.py') and 'DLC' in file:
module_name = file[:-3]
exec(f"import {module_name}")
exec(f"{module_name}.add_DLC_skills(brain)")