Home Download Buy Blog Forum Support

Evaluate

Evaluate

Postby jbrooksuk on Tue Sep 20, 2011 2:53 pm

So somebody asked about an Evaluate plugin, such as that in Textmate. I decided to take it on, and actually got around to looking into how wbond wrote the Prefixr plugin (awesome by the way). I've been meaning to check it out for some time now, and this has given me a reason.

I wrote it because I was intrigued by Python and Sublimes API. I stripped out a few things because I couldn't find a need for them. It's all threaded for multiple selections.

Yeah, anyway, I give all credit to wbond for this plugin, even though he doesn't know about it!

Check it out.
jbrooksuk
 
Posts: 774
Joined: Sun Apr 11, 2010 10:37 am
Location: England

Re: Evaluate

Postby jbrooksuk on Tue Sep 20, 2011 2:55 pm

And yes, it's alpha. I know it doesn't work with spaces or with a '/' symbol. But I don't know why this is. So if somebody could explain?

Edit: Actually, it seems temperamental to what works and what doesn't...
jbrooksuk
 
Posts: 774
Joined: Sun Apr 11, 2010 10:37 am
Location: England

Re: Evaluate

Postby jbrooksuk on Tue Sep 20, 2011 8:39 pm

So I've been messing with this for the past few minutes and figured the following with the current implementation.

For some reason, you can not do the following:
Code: Select all
x=10
x*100

Yet, if you try to evaluate
Code: Select all
x
on its own, you'll get an undefined error.

You can do the following, because it's fun...
Code: Select all
__import__('os').getcwd()

Which for me, evaluates to /

Or you can do some more advanced math functions...
Code: Select all
__import__('math').log(10)

Which is interesting.

So yeah. Variables don't seem to work. Imports do. Anyone think of what I've missed, how to improve?

I'm thinking of implementing a simple top-down parser, with a few inbuilt functions etc etc.
jbrooksuk
 
Posts: 774
Joined: Sun Apr 11, 2010 10:37 am
Location: England

Re: Evaluate

Postby jbrooksuk on Wed Sep 21, 2011 8:08 am

Oh, you can also concatenate strings!
Code: Select all
'hello' + 'world'

Evaluates to helloworld :)

I don't know why eval rounds, because it'd be nice to have 19/4 (example from Ideas and Feature Requests topic)
jbrooksuk
 
Posts: 774
Joined: Sun Apr 11, 2010 10:37 am
Location: England

Re: Evaluate

Postby bizoo on Wed Sep 21, 2011 8:35 am

jbrooksuk wrote:I don't know why eval rounds, because it'd be nice to have 19/4 (example from Ideas and Feature Requests topic)

Same as console, by default Python return integer for integer/integer division.
Try
Code: Select all
19/4.


You probably better go:
Code: Select all
tmp_global = {}
code = compile('10/4.', '<string>', 'eval')
out = eval(code, tmp_global)

Or more versatile:
Code: Select all
tmp_global = {}
code = compile('x = 10\ny = 4.\nout = x/y\n', '<string>', 'exec')
eval(code, tmp_global)
out = tmp_global['out']

Check Python documentation for compile and eval/exec methods.
bizoo
 
Posts: 886
Joined: Wed Dec 08, 2010 6:53 am
Location: Switzerland

Re: Evaluate

Postby jbjornson on Wed Sep 21, 2011 8:37 am

Apparently,
The current division (/) operator has an ambiguous meaning for numerical arguments: it returns the floor of the mathematical result of division if the arguments are ints or longs, but it returns a reasonable approximation of the division result if the arguments are floats or complex. This makes expressions expecting float or complex results error-prone when integers are not expected but possible as inputs.


from http://www.python.org/dev/peps/pep-0238/.

Also from there:

The future division statement, spelled "from __future__ import division", will change the / operator to mean true division throughout the module.
jbjornson
 
Posts: 350
Joined: Tue Oct 27, 2009 4:25 pm
Location: Zurich, Switzerland

Re: Evaluate

Postby jbrooksuk on Thu Sep 22, 2011 1:31 pm

I've fixed decimal division bug.

For anyone interested...

Code: Select all
from __future__ import division
jbrooksuk
 
Posts: 774
Joined: Sun Apr 11, 2010 10:37 am
Location: England

Re: Evaluate

Postby jbrooksuk on Mon Sep 26, 2011 10:36 am

Just pushed a few additions. Evaluate now appears under the Edit menu and in the Command Palette :)
jbrooksuk
 
Posts: 774
Joined: Sun Apr 11, 2010 10:37 am
Location: England


Return to Plugin Announcements

Who is online

Users browsing this forum: No registered users and 5 guests