Created
March 29, 2014 00:35
-
-
Save thomasballinger/9845985 to your computer and use it in GitHub Desktop.
This file contains hidden or 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. """ | |
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