forked from jcubic/try-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
55 lines (49 loc) · 1.34 KB
/
main.py
File metadata and controls
55 lines (49 loc) · 1.34 KB
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
import sys
import traceback
from browser import window, alert, console
def write(str):
if str.endswith("\n"):
str = str[:-1]
term.echo(str)
sys.stdout.write = sys.stderr.write = write
code = ''
env = {}
def evaluate(code):
global env
try:
ret = eval(code, env)
if ret != None:
write(repr(ret))
except:
try:
exec(code, env)
except:
traceback.print_exc()
def interpreter(command, term):
global code
try:
if command.endswith(":"):
term.set_prompt("... ")
code = code + command + "\n"
elif code != "":
if command == "":
term.set_prompt(">>> ")
evaluate(code)
code = ""
else:
code = code + command + "\n"
else:
evaluate(command)
except SyntaxError as msg:
if str(msg) == 'invalid syntax : triple string end not found' or \
str(msg).startswith('Unbalanced bracket'):
term.set_prompt("... ")
code = code + command
v = sys.implementation.version
term = window.terminal(interpreter, {
"prompt": ">>> ",
"greetings": False,
"onInit": lambda term: term.echo("Brython %s.%s.%s" % (v[0], v[1], v[2]), {"formatters": False}),
"height": 255
})
window.pydicator.stop()