Advertisement
igendel

Python exec external file with params demo

Dec 8th, 2023
955
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.78 KB | None | 0 0
  1. # This is the "plugin" file to be executed. Copy and paste it as "amazingPlugin.py"
  2. # It expects the variables: 'data' (string) and 'result' (dictionary)
  3. # 'result' will contain the entries 'first_char', 'last_char', 'length'
  4.  
  5. result["first_char"] = data[0]
  6. result["last_char"] = data[-1]
  7. result["length"] = len(data)
  8.  
  9. #---------------------------------------------------------------------------------------
  10.  
  11. # This is the demo code. Copy and paste it as a different .py file in the same directory
  12.  
  13. s = "Hello plugin!"
  14.  
  15. with open("amazingPlugin.py") as f:
  16.     plugin = f.read()
  17. f.close()
  18.  
  19.  
  20. return_dict = {"data": s, "result" : {}}
  21. try:
  22.     exec(plugin, {}, return_dict)
  23. except:
  24.     print("something bad happened!")
  25.  
  26. print("Plugin processing result:", return_dict)
  27.  
  28.  
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement