Skip to content

Instantly share code, notes, and snippets.

@thomasballinger
Created December 23, 2013 23:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomasballinger/8106585 to your computer and use it in GitHub Desktop.
Save thomasballinger/8106585 to your computer and use it in GitHub Desktop.
bpython pastebin script: uploads to Online Python Tutor
#!/usr/bin/env python
# Add the line
# pastebin_helper = opt.py
# to .bpython/config
# under a section called
# [general]
# to enable
import sys
import urllib
import webbrowser
def get_opt_url(s, from_bpython=True):
""" Use json to post to github. """
def fix(text):
return ('\n'.join(line[4:] if line[:4] in ('... ', '>>> ') else '### '+line
for line in text.split('\n')).encode('utf8'))
s = fix(s)
num_instructions = len([line for line in s.split('\n') if not line.startswith('#')])
url = 'http://www.pythontutor.com/visualize.html'
s = s[:-1] if s.endswith('\x0A') else s
#Doing encoding manually instead of urllib.encode because order
data = [
('mode', 'display'),
('cumulative', 'false'),
('heapPrimitives', 'false'),
('drawParentPointers', 'false'),
('textReferences', 'false'),
('showOnlyOutputs', 'false'),
('py', '2'),
('curInstr', str(num_instructions))
]
req = url + '#code=' + urllib.quote_plus(s) + '&' + urllib.urlencode(data)
return req
def view_in_opt(s):
webbrowser.open_new_tab(get_opt_url(s))
if __name__ == "__main__":
s = sys.stdin.read()
view_in_opt(s)
print get_opt_url(s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment