Created
November 11, 2013 18:08
-
-
Save thomasballinger/7417554 to your computer and use it in GitHub Desktop.
Pastebin helper for Online Python Tutor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import urllib | |
import webbrowser | |
def get_opt_url(s, from_bpython=True): | |
""" Use json to post to github. """ | |
if from_bpython: | |
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) | |
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'), | |
] | |
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