Good day! I'm trying to implement a module for testing knowledge. The user is given the task, he wrote the decision that is being sent and executed on the server. The question in the following. There are raw data that is stored in the file. Example - a = 5 b = 7 There is a custom solution that is stored in the string. example
s = a * b
p = a + b
print s,p
Now it is all written in a separate file as a string.
'a = 5\n', 'b = 7', u's = a * b\r\np = a + b\r\nprint s,p'
How to do this so that the code used can be performed. Will be something like that.
a = 5
b = 7
s = a * b
p = a + b
print s,p
Here's my function to create a solution and executes it if necessary.
def create_decision(user_decision, conditions):
f1 = open('temp_decision.py', 'w')
f = open(conditions, 'r+')
contents = f.readlines()
contents.append(user_decision)
f1.write(str(contents))
f1.close()
output = []
child_stdin, child_stdout, child_stderr = os.popen3("python temp_decision.py")
output = child_stdout.read()
return output
Or tell me what I'm doing wrong? Thanks!