0

I am searching how to connect functions written in text file to programmed functions in source code. Here is an example

In test.txt I have

create_session("azerty", 123, "test.png", "test.yaml", True)
launch_session("abc", 695, "file.txt")

In my source code I have

def create_session(self, var1: str, var2: int, var3: str, var4: str, var5: boolean)
def launch_session(self, var1: str, var2: int, var3: str)

My goal is when I read this file containing "commands" I would like to be able to connect each command to the associated function checking number of parameters and parameters.

By example the command name is "totuti" and the correct programmed function "totuti" is executed, if it is not found an error is raised. If my command in text file contains only 4 parameters and the source code programmed function needs 5 parameters, it doesn't work. The same process is executed checking parameters type.

My question is : does any package exist to do this ? Is there any way to check parameters type ?

Meugiwara
  • 599
  • 1
  • 7
  • 13
  • 1
    Does this answer your question? [How do I execute a string containing Python code in Python?](https://stackoverflow.com/questions/701802/how-do-i-execute-a-string-containing-python-code-in-python) If the function is not defined you will get a `NameError` trying to execute it. If the wrong number of arguments was passed you will get a `TypeError` – Tomerikoo Oct 18 '20 at 15:55
  • Thank you for your answer, but how to manage parameters type ? No error is returned – Meugiwara Oct 18 '20 at 16:16
  • The error will be raised if there is a problem inside the function with the types. For example if you simply have `print` in the function most chances that there will be no error... If you want to check with the type hints that's a bit more complicated – Tomerikoo Oct 18 '20 at 17:19

0 Answers0