2

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!

2 Answers 2

1

You don't need to create a tempfile, you can simply use exec. create_decision would then look so:

A

def create_decision(user_decision, conditions):
    f = open(conditions, 'r+')
    contents = f.readlines()
    contents.append(user_decision)
    # join list entries with a newline between and return result as string
    output = eval('\n'.join(contents))
    return output

B

import sys
import StringIO
import contextlib

@contextlib.contextmanager
def stdoutIO(stdout=None):
    old = sys.stdout
    if stdout is None:
        stdout = StringIO.StringIO()
    sys.stdout = stdout
    yield stdout
    sys.stdout = old

def create_decision(user_decision, conditions):
    f = open(conditions, 'r+')
    contents = f.readlines()
    contents.append(user_decision)
    with stdoutIO() as output:
        #exec('\n'.join(contents)) # Python 3
        exec '\n'.join(contents) # Python 2
    return output.getvalue()

You should also use str.join() to make one string out of the list of conditions. Otherwise you couldn't write it to a file or execute it (I've done this already in the above function).

Edit: There was a bug in my code (exec doesn't return anything) so I've added a method with eval (but that won't work with print, because it evaluates and returns the result of one expression, more info here). The second method captures the output of print and stdoutIO is from an other question/answer (here). This method returns the output from print, but is a bit more complicated.

Sign up to request clarification or add additional context in comments.

4 Comments

I do not understand what the problem is. After starting your example, displays the following error on letter "c". Python v.2.7.5 output = exec('\n'.join(contents)) SyntaxError: invalid syntax
My code was for Python 3. Now I have changed it to Python 2 (the python 3 line is commented out)
Thank you for this, but it still does not work. shows the same error. I started it, on pyton3 , but the result is not written to the variable output and return None
Oh yes. I've found the problem and think it should now work. The second option from the answer is probably the better solution.
0

You can execute a string like so: exec("code")

1 Comment

Just in case one is using python 2.x, this would be exec "code".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.