forked from jackw01/led-control
-
Notifications
You must be signed in to change notification settings - Fork 0
/
restrictedtest.py
36 lines (29 loc) · 872 Bytes
/
restrictedtest.py
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 RestrictedPython
import math
import random
def getitem(obj, index):
if obj is not None and type(obj) in (list, tuple, dict):
return obj[index]
raise Exception()
restricted_globals = {
'__builtins__': RestrictedPython.Guards.safe_builtins,
'_print_': RestrictedPython.PrintCollector,
'_getattr_': RestrictedPython.Guards.safer_getattr,
'_getitem_': getitem,
'_write_': RestrictedPython.Guards.full_write_guard,
'math': math,
'random': random,
}
restricted_locals = {
}
source_code = """
def pattern(t, dt, x, y, prev_state):
return (t + x, 0, prev_state[0])
"""
byte_code = RestrictedPython.compile_restricted_exec(
source_code,
filename='<inline code>'
)
print(byte_code)
print(exec(byte_code[0], restricted_globals, restricted_locals))
print(restricted_locals['pattern'](0.5, 0.01, 0.2, 0.2, (1, 2)))