How to exec() a string like interactive python does?
news1234 at free.fr
Wed Jun 6 23:09:22 CEST 2012
On 06/06/2012 01:31 AM, Steven D'Aprano wrote:
> On Wed, 06 Jun 2012 00:12:00 +0200, News123 wrote:
>> If I start Python in interactive mode, and I yype the commands,
>> 'a=3', 'a', 'print a'
>> Then the output would look like:
>> >>> a = 3
>> >>> a
>> >>> print a
>> Now within an application I'd like to achieve exactly this behaviour
> Before you reinvent the wheel, see the cmd and code modules.
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'
It reports errors for each line,
The code snippet I tried:
> import cmd
> class MyCmd(cmd.Cmd):
> my_cmd = MyCmd()
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
> Interpreters constructed with this class obey the following conventions:
> 1. End of file on input is processed as the command 'EOF'.
> 2. A command is parsed out of each line by collecting the prefix composed
> of characters in the identchars member.
> 3. A command `foo' is dispatched to a method 'do_foo()'; the do_ method
> is passed a single argument consisting of the remainder of the line.
> 4. Typing an empty line repeats the last command. (Actually, it calls the
> method `emptyline', which may be overridden in a subclass.)
> 5. There is a predefined `help' method. Given an argument `topic', it
> calls the command `help_topic'. With no arguments, it lists all topics
> with defined help_ functions, broken into up to three topics; documented
> commands, miscellaneous help topics, and undocumented commands.
> 6. The command '?' is a synonym for `help'. The command '!' is a synonym
> for `shell', if a do_shell method exists.
> 7. If completion is enabled, completing commands will be done automatically,
> and completing of commands args is done by calling complete_foo() with
> arguments text, line, begidx, endidx. text is string we are matching
> against, all returned matches must begin with it. line is the current
> input line (lstripped), begidx and endidx are the beginning and end
> indexes of the text being matched, which could be used to provide
> different completion depending upon which position the argument is in.
> The `default' method may be overridden to intercept commands for which there
> is no do_ method.
> The `completedefault' method may be overridden to intercept completions for
> commands that have no complete_ method.
> The data member `self.ruler' sets the character used to draw separator lines
More information about the Python-list