-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.py
More file actions
35 lines (31 loc) · 974 Bytes
/
context.py
File metadata and controls
35 lines (31 loc) · 974 Bytes
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
import ast, _ast, time
from stdlib import Image, Video
from value_repr import value_repr
from upstream.codegen.codegen import to_source
class Context:
def __init__(self):
self.reset()
def reset(self):
self.globals = {
'Image': Image,
'Video': Video
}
self.locals = {}
def run_code(self, code):
result = None
t0 = time.time()
try:
for node in ast.parse(code).body:
node_code = to_source(node)
if isinstance(node, _ast.Expr):
result = eval(node_code, self.globals, self.locals)
else:
exec(node_code, self.globals, self.locals)
result = None
except Exception, e:
result = e
microseconds = int((time.time() - t0) * 1000000)
return {
'microseconds': microseconds,
'result': value_repr(result)
}