I'm creating a factory class that is able to dynamically build out an object based on a bunch of individual blocks (small functions of code). It takes a JSON object that it traverses through, and based on the key of what is being asked it will dynamically select a function from a file.
The benefit from this approach is that you won't have to update the factory class when adding new functionality, just add a new file with the specific function you're looking to add.
What I'm not sure how to do is dynamically get and populate the list of parameters for a given function, which may vary in length and what type of parameter they are.
For example lets say I have 2 files:
file #1: foo.py
def foo(a, b):
return a + b
file #2 bar.py
def bar(a, b, c):
return [a, b, c]
Both these files exist in a subfolder called blocks to the factory. The factory file should be able to find those files and call the functions with the correct parameters given a name.
class Factory():
a: str
b: str
c: str
factory_directory: str
__init__(self):
self.factory_directory = Path().absolute()
get_block(self, name):
path = self.factory_directory + '/block/' + name + '.py'
if os.path.exists(path):
function = importlib.import_module('path')
else:
raise Exception
Is there a way to dynamically build out what the parameters are assuming they already exist as properties on the factory class? One idea I had was to create a class for each function, and have that class include information about what parameters it needs but I'm wondering if there's a better way.