Archive for March, 2012

Automate Python code execution with predefined I/O

March 15, 2012 4 comments

Need to not prompt for input in program execution and store output in a variable.

If you need a program that can arbitrarily execute a piece of Python code, and get the result, then this post is made for you. Much like in a programming contest. It’s not complicated if you need this kind of functionality on a Web Server that run and breath Python- it was the Django framework for my case.

As it turns out, you can actually run a piece of code embedded inside a String much like in javascript. And thats pretty damn cool, Sir. Here’s how to do it… if you want to understand the parameters, well you don’t say! Look elsewhere :-).

code = compile(newCode, '<string>', 'exec')

newCode is a string (can be single line or multiline string- those inside ”’ statement ”’ that span multiple line). Yes, Python can compile code! I bet you didn’t know that :P. Be careful Read more…

Categories: Uncategorized