Sublime Forum

MiniPy - followup on EJ12N

#1

i was amazed from EJ12N screen cast and tried to implement the exec python myself, to see how far can i take it. well i came up with this:

NOTE: if you plan to use it, see better plugin bellow

[code]from math import *
from random import *
import sublime, sublimeplugin

class MiniPyCommand(sublimeplugin.TextCommand):
def run(self, view, args):
script = “”
for region in view.sel():
script = view.substr(region)
view.replace(region, str(eval(script)))

def isEnabled(self, view, args):
return True[/code]

and binding: <binding key="ctrl+e" command="miniPy"/>

so, just ctrl+e on a selected text, and tada! THANKS EJ12N !!!

anyway, i write this here because of my limited python knowledge. i thought it would be really nice if i could do something like:

$x=0 $x++ $x++ ...

or

$x = "a." $x".my_class"

etc. any idea how to implement it? if at all?

to be clear i want use variables which will be alive on this mini program, i chose to mark it with $ but that just an idea

ideas/help will be appreciated :smile:

vim.

0 Likes

#2

Nice :smiley:

BTW Guys just to make it clear, all of these customizations you see on my screencasts WILL BE available in the repository, soon… I just keep commenting stuff and making it “public” friendly so at least you guys know what do with it…

0 Likes

#3

@EJ12N: this day will be like christmas…

0 Likes

#4

this what i been able to do:

[code]from math import *
from random import *
import re
import sublime, sublimeplugin

class MiniPyCommand(sublimeplugin.TextCommand):
def run(self, view, args):
script = “”

  # sum the total number of special char $
  total = 0
  for region in view.sel():
     total += view.substr(region).count("$")
  
  # build a list from 1 to the number of special chars
  serial_number = range(1,total+1)
  serial_number.reverse()
  
  print str(serial_number)
  
  # replace each special char $ with the next index from serial_number list
  # and eval the expression
  for region in view.sel():
     script = view.substr(region)
     for n in range(script.count("$")):
        script = re.sub(r"\$", str(serial_number.pop()), script, 1)
     view.replace(region, str(eval(script)))

def isEnabled(self, view, args):
return True

[/code]

so if you mark the column with the $ sign on the bottom code, and press ctrl+e

arr$]
arr$]
arr$]
arr$]

it will result with

arr[0] arr[1] arr[2] arr[3]

or mark (and split to lines with ctrl+shift+L):

$+$+$ $+$ $

will result:

6 # the sum of 1+2+3 9 # the sum of 4+5 6 # the 6th $ char is single

:open_mouth:

of course those will work too:

random() sin(1.2) 1+2+3 34*5.5 "x" * 10 len("abracatabra")

result:

0.409628710502 0.932039085967 6 187.0 xxxxxxxxxx 11

0 Likes

#5

No, lol closer than you might actually think :wink:
You pretty much got it there vim! That’s what I got, an eval on selected text.
Good job :smile:

0 Likes