Sublime Forum

Evaluate

#1

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.

0 Likes

Evaluate Selection?
#2

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…

0 Likes

#3

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:

x=10
x*100

Yet, if you try to evaluate x on its own, you’ll get an undefined error.

You can do the following, because it’s fun…

__import__('os').getcwd()

Which for me, evaluates to /

Or you can do some more advanced math functions…

__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.

0 Likes

#4

Oh, you can also concatenate strings!

'hello' + 'world'

Evaluates to helloworld :smile:

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

0 Likes

#5

[quote=“jbrooksuk”]
I don’t know why eval rounds, because it’d be nice to have 19/4 (example from Ideas and Feature Requests topic)[/quote]

Same as console, by default Python return integer for integer/integer division.
Try

19/4.

You probably better go:

tmp_global = {} code = compile('10/4.', '<string>', 'eval') out = eval(code, tmp_global)
Or more versatile:

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.

0 Likes

#6

Apparently,

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

Also from there:

0 Likes

#7

I’ve fixed decimal division bug.

For anyone interested…

from __future__ import division
0 Likes

#8

Just pushed a few additions. Evaluate now appears under the Edit menu and in the Command Palette :smile:

0 Likes