igendel icon

Python exec external file with params demo

igendel | PRO | 12/08/23 06:13:23 AM UTC | 0 ⭐ | 1234 👁️ | Never ⏰ | []
Python |

780 B

|

None

|

0 👍

/

0 👎

# This is the "plugin" file to be executed. Copy and paste it as "amazingPlugin.py"
# It expects the variables: 'data' (string) and 'result' (dictionary)
# 'result' will contain the entries 'first_char', 'last_char', 'length'
 
result["first_char"] = data[0]
result["last_char"] = data[-1]
result["length"] = len(data)
 
#---------------------------------------------------------------------------------------
 
# This is the demo code. Copy and paste it as a different .py file in the same directory
 
s = "Hello plugin!"
 
with open("amazingPlugin.py") as f:
    plugin = f.read()
f.close()
 
 
return_dict = {"data": s, "result" : {}}
try:
    exec(plugin, {}, return_dict)
except:
    print("something bad happened!")
 
print("Plugin processing result:", return_dict)
 
 
 

Comments