Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View ultisnips_py_console
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
global !p
import string, vim
import textwrap
 
_snips_fns = {}
 
 
def py(*args):
import re
old_a = vim.eval('@a')
 
cmd = ' '.join(args)
if cmd.startswith('print '):
cmd = cmd[5:]
splits = re.split('\s', cmd)
 
if py.last_cmd != cmd:
vim.command('let @a=""')
vim.command('redir @a')
 
vim.command("""py
out = []
try:
gs = globals()
out.append(str(eval(''' {0} '''.strip())))
if out[-1].startswith('<built-in'):
out.pop[0]
except Exception as e:
print e.message
try:
exec(''' {0} '''.strip())
except:
import keyword
import __builtin__
cmd = ''' {0} '''.strip()
text = ''' {1} '''.strip()
 
if text:
if text.endswith('?'):
try:
exec('help(%s)' % text.rstrip('?'))
except:
pass
elif '.' in text:
 
def get_class_members(klass):
ret = dir(klass)
if hasattr(klass,'__bases__'):
for base in klass.__bases__:
ret = ret + get_class_members(base)
return ret
 
m = re.match(r'(\w+(\.\w+)*)\.(\w*)', text)
if m:
expr, attr = m.group(1, 3)
thisobject = None
try:
thisobject = eval(expr, globals())
except Exception:
pass
 
if thisobject is not None:
# get the content of the object, except __builtins__
words = dir(thisobject)
if '__builtins__' in words:
words.remove('__builtins__')
 
if hasattr(thisobject, '__class__'):
words.append('__class__')
words.extend(get_class_members(thisobject.__class__))
matches = []
n = len(attr)
for word in words:
if word[:n] == attr and hasattr(thisobject, word):
val = getattr(thisobject, word)
word = '%s.%s' % (expr, word)
out.append(word)
else:
n = len(text)
for word in keyword.kwlist:
if word[:n] == text:
out.append(word)
for nspace in [__builtin__.__dict__, globals()]:
for word, val in nspace.items():
if word[:n] == text and word != '__builtins__':
out.append(word)
 
if len(out) == 1:
out = []
 
if out:
out = out + [''] * ((4 - (len(out) % 4)) % 4)
_out = [out[i*4:i*4+4] for i in range(len(out)) if out[i*4:i*4+4]]
longest = [max(map(lambda x: len(str(x)), o)) for o in zip(*_out)]
print '\\n'.join(' '.join(('%%-%ds' % longest[i]) % str(w) for i, w in enumerate(ws)) for ws in _out)
""".format(cmd, splits[-1].rstrip(')')))
vim.command('redir END')
r = vim.eval('@a').strip()
vim.command('let @a="{}"'.format(old_a))
 
try:
r = re.sub('(?<= at )0x[0-9A-F]*(?=>$)', 'some address', r)
except Exception as e:
r = str(e)
 
py.last_cmd = cmd
 
if r:
py.cache = r
return r
return ''
else:
py.last_cmd = cmd
return py.cache
py.last_cmd = None
py.cache = None
 
endglobal
 
 
snippet p "p"
>>> ${1: }
`!p
d = dir()
snip.rv = ''
if t[1] and t[1].strip() != 't':
try:
snip.rv = py(t[1])
except:
pass
if not snip.rv:
snip.rv = '...'
`
$2
$0
endsnippet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.