How to exec() a string like interactive python does?

N

News123

If I start Python in interactive mode,
and I yype the commands,
'a=3', 'a', 'print a'


Then the output would look like:3


Now within an application I'd like to achieve exactly this behaviour
Meaning, that
- for assignments nothing is displayed
- for expressions the result of the exprission displayed
- and statements like print statements would be executed


The only thing, that I came up with is following code and that would
even print out results for 'a=3', where the normal interactive python
would not echo any result.

for cmd in [ 'a=3', 'a', 'print a' ] :
try:
print('>>> ' + cmd)
exec('__rslt = ' + cmd)
if __rslt is not None:
print repr(__rslt)
except SyntaxError:
exec(cmd)

The result would look like:

Is There anything better?
 
N

News123

If I start Python in interactive mode,
and I yype the commands,
'a=3', 'a', 'print a'


Then the output would look like:3


Now within an application I'd like to achieve exactly this behaviour
Meaning, that
- for assignments nothing is displayed
- for expressions the result of the exprission displayed
- and statements like print statements would be executed


The only thing, that I came up with is following code and that would
even print out results for 'a=3', where the normal interactive python
would not echo any result.

for cmd in [ 'a=3', 'a', 'print a' ] :
try:
print('>>> ' + cmd)
exec('__rslt = ' + cmd)
if __rslt is not None:
print repr(__rslt)
except SyntaxError:
exec(cmd)

The result would look like:

Is There anything better?

Thanks a lot Devin,
Following line does the trick:

eval(compile(s, '<string>', 'single'))
 
N

News123

Before you reinvent the wheel, see the cmd and code modules.

http://docs.python.org/library/cmd.html
http://docs.python.org/library/code.html


Thanks a lot.

The cmd library looks great at a first glance
However somehow I fail to get it working (meaning, that it executes
following strings identical to the python interactive shell
'a = 3'
'a'
'print a'

It reports errors for each line,

The code snippet I tried:
import cmd

class MyCmd(cmd.Cmd):
pass

my_cmd = MyCmd()
my_cmd.cmdloop()



I assume the part, that I don't understand is
Cmd.identchars and the concept of a command prefix.

Perhaps it's the fact, that my native language is not English, but
folowing sentence doesn't make a lot of sense to me:

" A command is parsed out of each line by collecting the prefix composed
of characters in the identchars member."

It almost seems as if the part of executing valid 'python strings' is
missing and had to be added manually to the default() method
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top