Python Forum
Convert a string to a function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert a string to a function
#1
Hello, is there a possibility or library to convert the following string to a function?

func_str = '''def test(event, x, y, flags, data):
    if event == cv2.EVENT_LBUTTONDOWN:
        print('LEFT MOUSE BTN DOWN!!!')
    if event == cv2.EVENT_RBUTTONDOWN:
        print('RIGHT MOUSE BTN DOWN!!!')
    if event == cv2.EVENT_MBUTTONDOWN:
        print('MIDDLE MOUSE BTN DOWN!!!')'''
Reply
#2
Why? Why do you want to write code in strings, rather than just store the code in a file?
buran likes this post
Reply
#3
Self modifying code, execution of user supplied code, all generally frowned upon as unsafe. Still, people ride barrels over Niagara Falls (not lately).

You are probably looking for the exec() function.
See this article for instruction and usage.
ndc85430 likes this post
Reply
#4
(May-13-2022, 05:30 PM)mikepy Wrote: Hello, is there a possibility or library to convert the following string to a function?

func_str = '''def test(event, x, y, flags, data):
    if event == cv2.EVENT_LBUTTONDOWN:
        print('LEFT MOUSE BTN DOWN!!!')
    if event == cv2.EVENT_RBUTTONDOWN:
        print('RIGHT MOUSE BTN DOWN!!!')
    if event == cv2.EVENT_MBUTTONDOWN:
        print('MIDDLE MOUSE BTN DOWN!!!')'''

I need this to provide the possibility to the user to write his own callbacks.
Reply
#5
(May-13-2022, 05:52 PM)jefsummers Wrote: Self modifying code, execution of user supplied code, all generally frowned upon as unsafe. Still, people ride barrels over Niagara Falls (not lately).

You are probably looking for the exec() function.
See this article for instruction and usage.

The exec function as far as I know executes the function immidiately but I want to save the function in a dictionary as a callback for the cv2 library.
Reply
#6
The exec function executes the code. If the code defines a function, exec defines the function. Calling exec with your string will create a new variable named "test" which is assigned to reference the new function.

Why didn't you just try that instead of offering a rebuttal?
exec('''def test(event, x, y, flags, data):
    if event == 1:
        print('LEFT MOUSE BTN DOWN!!!')
    if event == 2:
        print('RIGHT MOUSE BTN DOWN!!!')
    if event == 3:
        print('MIDDLE MOUSE BTN DOWN!!!')''')

test(2, 0, 0, 0, 0)
Output:
RIGHT MOUSE BTN DOWN!!!
Reply
#7
(May-13-2022, 06:08 PM)mikepy Wrote: but I want to save the function in a dictionary as a callback for the cv2 library.

Why don't you do that then? You know functions are first-class in Python, right?
Reply
#8
exec('''def test(event, x, y, flags, data):
    if event == 1:
        print('LEFT MOUSE BTN DOWN!!!')
    if event == 2:
        print('RIGHT MOUSE BTN DOWN!!!')
    if event == 3:
        print('MIDDLE MOUSE BTN DOWN!!!')''')
 
functions = {"test":test}
functions["test"](3, 0, 0, 0, 0)
Output:
MIDDLE MOUSE BTN DOWN!!!
Reply
#9
Thank you guys I will try your solutions!!!

EDIT:
After trying both of your solutions it works like a charm. Thank you very much :).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  convert string to float in list jacklee26 6 1,935 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  how to convert tuple value into string mg24 2 2,361 Oct-06-2022, 08:13 AM
Last Post: DeaD_EyE
  Convert string to float problem vasik006 8 3,426 Jun-03-2022, 06:41 PM
Last Post: deanhystad
Question How to convert string to variable? chatguy 5 2,442 Apr-12-2022, 08:31 PM
Last Post: buran
  Convert string to int Frankduc 8 2,496 Feb-13-2022, 04:50 PM
Last Post: menator01
  Convert string to path using Python 2.7 tester_V 10 6,474 Nov-20-2021, 02:20 PM
Last Post: snippsat
  Convert each element of a list to a string for processing tester_V 6 5,362 Jun-16-2021, 02:11 AM
Last Post: tester_V
Question convert unlabeled list of tuples to json (string) masterAndreas 4 7,483 Apr-27-2021, 10:35 AM
Last Post: masterAndreas
  Convert String of an int array to a Numpy array of ints mdsousa 5 5,710 Apr-08-2021, 08:00 PM
Last Post: mdsousa
  Convert string to JSON using a for loop PG_Breizh 3 2,988 Jan-08-2021, 06:10 PM
Last Post: PG_Breizh

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020