Hi!
A python interactive interpreter works by having the user type in some
code, compiling and running that code, then printing the results. For
printing, the results are turned into strings.
I would like make an interpreter which does this, without the last
part: i.e. where the results are returned as objects, instead of as
strings. I.e. have I would like to see something that behaves like
this:
>>ip = MyInterpreter()
# this started a new interpreter
>>ip.run("import math") is None
True
>>ip.run("math.pi") is math.pi
True
Neither exec() or eval() is usable for this, as far as I see, because
eval can't handle arbitrary python code (eval("import math") ), and
exec() doesn't return the results.
Subclassing an code.InteractiveInterpreter or code.InteractiveConsole
seems like a usable idea, but I couldn't find out what to do to get
the results before they are turned into strings.
Using compile() and then eval() didn't seem usable either.
Any ideas?
Thanks in advance,
Daniel Abel 3 1660
A python interactive interpreter works by having the user type in some
code, compiling and running that code, then printing the results. For
printing, the results are turned into strings.
I would like make an interpreter which does this, without the last
part: i.e. where the results are returned as objects, instead of as
strings. I.e. have I would like to see something that behaves like
this:
>ip = MyInterpreter()
# this started a new interpreter
>ip.run("import math") is None
True
>ip.run("math.pi") is math.pi
True
Neither exec() or eval() is usable for this, as far as I see, because
eval can't handle arbitrary python code (eval("import math") ), and
exec() doesn't return the results.
Subclassing an code.InteractiveInterpreter or code.InteractiveConsole
seems like a usable idea, but I couldn't find out what to do to get
the results before they are turned into strings.
Using compile() and then eval() didn't seem usable either.
Any ideas?
Well, my first thought is that exec and eval serve two different
purposes, and you should just have both of them and use the
appropriate one based on the situation. However, I think it is
possible to enable the behavior you want: -
def myeval(statement, globals_=None, locals_=None):
-
try:
-
return eval(statement, globals_, locals_)
-
except SyntaxError:
-
if locals_ is None:
-
import inspect
-
locals_ = inspect.currentframe().f_back.f_locals
-
exec statement in globals_, locals_
-
It seems to work for me.
Matt
En Mon, 27 Aug 2007 15:59:25 -0300, Abel Daniel <ab**@freemail.hu>
escribi�:
Hi!
A python interactive interpreter works by having the user type in some
code, compiling and running that code, then printing the results. For
printing, the results are turned into strings.
This last stage is done by calling sys.displayhook - you can replace that
function with your own one, and get the object to be printed.
Any of your previous attempts (using compile(...,"single")+eval, using
exec, or creating your own code.InteractiveInterpreter) should work with
this.
--
Gabriel Genellina
On Aug 27, 6:06 pm, "Matt McCredie" <mccre...@gmail.comwrote:
A python interactive interpreter works by having the user type in some
code, compiling and running that code, then printing the results. For
printing, the results are turned into strings.
I would like make an interpreter which does this, without the last
part: i.e. where the results are returned as objects, instead of as
strings. I.e. have I would like to see something that behaves like
this:
>>ip = MyInterpreter()
# this started a new interpreter
>>ip.run("import math") is None
True
>>ip.run("math.pi") is math.pi
True
Neither exec() or eval() is usable for this, as far as I see, because
eval can't handle arbitrary python code (eval("import math") ), and
exec() doesn't return the results.
Subclassing an code.InteractiveInterpreter or code.InteractiveConsole
seems like a usable idea, but I couldn't find out what to do to get
the results before they are turned into strings.
Using compile() and then eval() didn't seem usable either.
Any ideas?
Well, my first thought is that exec and eval serve two different
purposes, and you should just have both of them and use the
appropriate one based on the situation. However, I think it is
possible to enable the behavior you want:
-
def myeval(statement, globals_=None, locals_=None):
-
try:
-
return eval(statement, globals_, locals_)
-
except SyntaxError:
-
if locals_ is None:
-
import inspect
-
locals_ = inspect.currentframe().f_back.f_locals
-
exec statement in globals_, locals_
-
It seems to work for me.
Matt
Unless it's something like:
raise_(SyntaxError)
where raise_ is a function equivalent to the corresponding statement. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Paddy |
last post by:
Hi,
I got tripped up on the way eval works with respect to modules and
so wrote a test.
It seems that a function carries around knowledge of the globals()
present
when it was defined. (The...
|
by: HikksNotAtHome |
last post by:
This is a very simplified example of an Intranet application. But, is there an
easier (or more efficient) way of getting the decimal equivalent of a fraction?
The actual function gets the select...
|
by: Stuart |
last post by:
why won't the following work
for(var i=0;i<pics;i++){
eval('img'+i) = new Image(wth,hgt)
eval('img'+i+'.src') = 'http://www.mypics/'+i+'1.gif'
}
basically I am trying to create a numer of...
|
by: Jim Washington |
last post by:
I'm still working on yet another parser for JSON (http://json.org). It's
called minjson, and it's tolerant on input, strict on output, and pretty
fast. The only problem is, it uses eval(). It's...
|
by: Michelle Keys |
last post by:
Subject: DataBinder.Eval Error!
Server Error in '/MSPOS' Application.
------------------------------------------------------------------------
--------
DataBinder.Eval:...
| |
by: Pauljh |
last post by:
Hi All,
I'm running some javascript over a server side generated web page
and have multiple generated empty select statements, that I want to
populate when the page is loaded. As HTML doesn't do...
|
by: abhishek |
last post by:
>>a,b=3,4
7
Now I want to evaluate y by substituting for the evaluated value of x.
eval(y) will try to add "a+b" to 3 and return an error. I could do
this,
10
but this becomes unwieldy if I...
|
by: My Pet Programmer |
last post by:
The way I usually set up and work with the XMLHttpRequest to execute
server side functions and get results is this:
var url = "someurl?params=" + params;
var conn = createRequest(); // gets an...
|
by: RandomElle |
last post by:
Hi there
I'm hoping someone can help me out with the use of the Eval function. I am using Access2003 under WinXP Pro. I can successfully use the Eval function and get it to call any function with...
|
by: Lie Ryan |
last post by:
On Tue, 30 Sep 2008 16:04:34 -0500, William Purcell wrote:
when you pass mydict, it is used as the global variables in the eval,
right? Then, you passed a code to eval('...', mydict), sometimes...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |