0

I'm working on a program for my school's programming outreach club, What I'm trying to do is have the user type in a string and the program searches the string. For example, they would type something like

moveInstant(4, 8)

and it would execute function

def moveInstant(targetX, targetY)
    pyautogui.moveTo(targetX, targetY)

Make sense? basically taking a long string and converting each instance of pseudo code into a predefined function with arguments. The idea is that a user could input a full txt file and it could be executed by the program.

2 Answers2

0

I am curious to know what the pseudo code will look like or how you are going to implement the 'string matching'. But:

  1. Once you got the right function names and parameters, you can just use the exec statement to execute it. If not only the side effect but the return value of a function is what you want, use eval (Differences). Or,
  2. Do some clean-up job for the text file and execute it using the execfile function.

Noted that statements like eval or exec are potentially harmful, and that's why your 'string matching' for untrusted inputs is important.

0

What you're looking for is the eval() method. Python Docs

Example:

>>> def test(x):
...   print(x)
>>> test(3)
>>> 3
>>> y = "test(124)"
>>> eval(y)
>>> 124
Jeremy
  • 1,894
  • 2
  • 13
  • 22