473,372 Members | 1,015 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

an eval()-like exec()

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

Aug 27 '07 #1
3 1651
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:

Expand|Select|Wrap|Line Numbers
  1. def myeval(statement, globals_=None, locals_=None):
  2. try:
  3. return eval(statement, globals_, locals_)
  4. except SyntaxError:
  5. if locals_ is None:
  6. import inspect
  7. locals_ = inspect.currentframe().f_back.f_locals
  8. exec statement in globals_, locals_
  9.  
It seems to work for me.

Matt
Aug 27 '07 #2
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

Aug 28 '07 #3
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:

Expand|Select|Wrap|Line Numbers
  1. def myeval(statement, globals_=None, locals_=None):
  2.     try:
  3.         return eval(statement, globals_, locals_)
  4.     except SyntaxError:
  5.         if locals_ is None:
  6.             import inspect
  7.             locals_ = inspect.currentframe().f_back.f_locals
  8.         exec statement in globals_, locals_
  9.  

It seems to work for me.

Matt
Unless it's something like:

raise_(SyntaxError)

where raise_ is a function equivalent to the corresponding statement.

Aug 28 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
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...
9
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...
33
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...
9
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...
0
by: Michelle Keys | last post by:
Subject: DataBinder.Eval Error! Server Error in '/MSPOS' Application. ------------------------------------------------------------------------ -------- DataBinder.Eval:...
3
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...
8
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...
13
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...
6
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...
0
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.