本文整理汇总了Python中interpreter.Interpreter类的典型用法代码示例。如果您正苦于以下问题:Python Interpreter类的具体用法?Python Interpreter怎么用?Python Interpreter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Interpreter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: download_planning
def download_planning(uuid):
try:
planning = _get_planning(uuid, g.user_id)
except CAPException as e:
return e.res
moodle_archive_path = planning.mbz_fullpath
planning_txt = planning.planning_txt
if not planning_txt:
return _bad_request()
# Make tmp directory for MBZ extraction and ics download
with tempfile.TemporaryDirectory() as tmp_path:
# Download calendar to tmp folder
calendar = CalendarReader(planning.ics_fullpath)
calendar_meetings = calendar.get_all_meetings()
# Extract Moodle course to tmp folder
with tarfile.open(moodle_archive_path) as tar_file:
tar_file.extractall(tmp_path)
course = MoodleCourse(tmp_path)
interpreter = Interpreter(calendar_meetings, course)
for line in planning_txt.split('\n'):
event = interpreter.get_new_event_from_string(line)
course.replace_event(event)
folder = os.path.join(app.config['UPLOAD_FOLDER'], uuid)
latest_mbz_path = os.path.join(folder, 'latest.mbz')
course.write(latest_mbz_path)
return send_from_directory(
folder, 'latest.mbz', as_attachment=True)
示例2: compiler
def compiler():
text = request.POST.get("text")
interpreter = Interpreter(text)
interpreter.compile()
result = str(interpreter)
return {'result':result}
示例3: main
def main():
raw_code = """
Input>>Number(num1) Integer
Input>>Number(num2) Integer
num1,num2>>Number(ans) Added
ans>>Screen(Output) Demo ConsolePlus
"""
a = Interpreter()
a.parser(raw_code)
print(a.info)
b = SMLHandler(a.source)
b.build_dom_tree()
print(b.info)
ss = b.build_sml()
print(ss)
c = SMLHandler()
c.read_sml(ss)
c.read_dom_tree()
c.dom = c.builder.getDOMImplementation()
c.tree = c.dom.createDocument("http://cp-web.appspot.com/source", "Source", None)
c.build_dom_tree()
print(c.build_sml())
示例4: index
def index(request):
if not request.POST:
return install(request)
response = HttpResponse()
try:
active_xml = request.POST.get('activexml')
if not active_xml:
response.write("Failure - Please post with valid XML string to process.")
return response
interp = Interpreter()
interp.processXML(active_xml)
interpList = interp.topPhraseList(count = 3)
outputList = [interp.word_count]
for tup in interpList:
outputList.append({'keyPhrase':tup[0], "iCount" : tup[1], "weight":tup[2]})
jsonString = simplejson.dumps( outputList )
print outputList
response.write(jsonString)
return response
except Exception, e:
print e
response.write(str(e))
return response
示例5: test_inttrait
def test_inttrait():
builtincode = """
object inttrait:
x = 1
def maybe_fortytwo:
if self:
42
else:
x
"""
interpreter = Interpreter(builtincode)
w_module = interpreter.make_module()
# the parent of a normal module is the builtin module
builtins = w_module.getparents()[0]
inttrait = builtins.getvalue("inttrait")
ast = parse("""
x = 5 x # this returns 1, because it looks in the inttrait defined above
m0 = 0 maybe_fortytwo
m1 = x maybe_fortytwo
inttrait x = 2
m2 = 0 maybe_fortytwo
tr = inttrait
""")
interpreter.eval(ast, w_module)
x = w_module.getvalue("x")
assert w_module.getvalue("tr") is inttrait
# the inttrait is defined in the builtin module, so its __parent__ is that
# module
assert inttrait.getparents() == [builtins]
assert x.value == 1
assert x.getparents() == [inttrait]
assert w_module.getvalue("m0").value == 1
assert w_module.getvalue("m1").value == 42
assert w_module.getvalue("m2").value == 2
示例6: test_args_order
def test_args_order():
ast = parse("""
def f(a, b, c):
if a:
4
else:
if b:
8
else:
if c:
15
else:
16
w = f(1, 1, 1)
x = f(0, 1, 1)
y = f(0, 0, 1)
z = f(0, 0, 0)
""")
interpreter = Interpreter()
w_module = interpreter.make_module()
interpreter.eval(ast, w_module)
assert w_module.getvalue("w").value == 4
assert w_module.getvalue("x").value == 8
assert w_module.getvalue("y").value == 15
assert w_module.getvalue("z").value == 16
示例7: test_builtin_simple
def test_builtin_simple():
builtincode = """
x = 1
object None:
1
def pass:
None
"""
# construct the builtin module by running builtincode within the context of
# a new empty module
interpreter = Interpreter(builtincode)
w_module = interpreter.make_module()
# the parent of a normal module is the builtin module
builtins = w_module.getparents()[0]
assert builtins.getvalue('x').value == 1
ast = parse("""
tx = x
object a:
pass
ax = a x
""")
interpreter.eval(ast, w_module)
assert w_module.getvalue("ax").value == 1
assert w_module.getvalue("tx").value == 1
示例8: download_planning
def download_planning(uuid):
planning = _get_planning(uuid)
if not planning:
return jsonify({"message": 'Planning with uuid "%s" not found' % uuid}), 404
moodle_archive_path = planning.mbz_fullpath
planning_txt = planning.planning_txt
if not planning_txt:
return _bad_request()
# Make tmp directory for MBZ extraction and ics download
with tempfile.TemporaryDirectory() as tmp_path:
# Download calendar to tmp folder
calendar_path = _dl_and_save_ics_file(planning.ics_url, tmp_path)
calendar = CalendarReader(calendar_path)
calendar_meetings = calendar.get_all_meetings()
# Extract Moodle course to tmp folder
with tarfile.open(moodle_archive_path) as tar_file:
tar_file.extractall(tmp_path)
course = MoodleCourse(tmp_path)
interpreter = Interpreter(calendar_meetings, course)
for line in planning_txt.split("\n"):
event = interpreter.get_new_event_from_string(line)
course.replace_event(event)
folder = os.path.join(app.config["UPLOAD_FOLDER"], uuid)
latest_mbz_path = os.path.join(folder, "latest.mbz")
course.write(latest_mbz_path)
return send_from_directory(folder, "latest.mbz", as_attachment=True)
示例9: test_if_example
def test_if_example(self):
example = """addi R1,R0,3
addi R2,R0,2
beq R1,R2,EQ
jmp NE
EQ:
addi R1,R0,5
jmp END
NE:
addi R1,R0,7
END:"""
text = [
"00100000000000010000000000000011 ; I1: addi R1,R0,3",
"00100000000000100000000000000010 ; I2: addi R2,R0,2",
"00010100001000100000000000001000 ; I3: beq R1,R2,8",
"00001000000000000000000000011000 ; I4: jmp 24",
"00100000000000010000000000000101 ; I5: addi R1,R0,5",
"00001000000000000000000000011100 ; I6: jmp 28",
"00100000000000010000000000000111 ; I7: addi R1,R0,7",
]
interpreter = Interpreter(example)
interpreter.compile()
for compiled, correct in zip(interpreter.instructions, text):
self.assertEqual(compiled, correct)
示例10: test_example
def test_example(self):
example = """addi R10,R0,100
sw R0,24(R0)
sw R0,28(R0)
LOOP:
lw R6,28(R0)
mul R7,R6,R6
lw R1,24(R0)
add R9,R1,R7
sw R9,24(R0)
addi R6,R6,1
sw R6,28(R0)
ble R6,R10,LOOP"""
text = [
"00100000000010100000000001100100 ; I1: addi R10,R0,100",
"10101100000000000000000000011000 ; I2: sw R0,24(R0)",
"10101100000000000000000000011100 ; I3: sw R0,28(R0)",
"10001100000001100000000000011100 ; I4: lw R6,28(R0)",
"00000000110001100011100000011000 ; I5: mul R7,R6,R6",
"10001100000000010000000000011000 ; I6: lw R1,24(R0)",
"00000000001001110100100000100000 ; I7: add R9,R1,R7",
"10101100000010010000000000011000 ; I8: sw R9,24(R0)",
"00100000110001100000000000000001 ; I9: addi R6,R6,1",
"10101100000001100000000000011100 ; I10: sw R6,28(R0)",
"00011100110010100000000000001100 ; I11: ble R6,R10,12",
]
interpreter = Interpreter(example)
interpreter.compile()
for compiled, correct in zip(interpreter.instructions, text):
self.assertEqual(compiled, correct)
示例11: test_override__parent__
def test_override__parent__():
ast = parse("""
k = 10
object a:
x = 1
y = 2
z = k
object b:
__parent__ = a
y = 5
z = y
ax = a x
ay = a y
az = a z
bx = b x
by = b y
bz = b z
""")
interpreter = Interpreter()
w_module = interpreter.make_module()
interpreter.eval(ast, w_module)
a = w_module.getvalue("a")
assert a.getvalue('__parent__') is w_module
assert w_module.getvalue("b").getvalue('__parent__') is a
assert w_module.getvalue("b").getparents() == [a]
assert w_module.getvalue("ax").value == 1
assert w_module.getvalue("ay").value == 2
assert w_module.getvalue("az").value == 10
assert w_module.getvalue("bx").value == 1
assert w_module.getvalue("by").value == 5
assert w_module.getvalue("bz").value == 5
示例12: compile_pascal
def compile_pascal(source, dest, is_debug = False, is_interpret = False, out_stream = sys.stdout, output_tokens = False, output_bytecodes = False, lib = ['.'], in_stream = sys.stdin):
'''
DID YOU KNOW that compile() is a built in function?
'''
set_debug(is_debug)
debug("Compiling %s into %s" % (source, dest))
scanner = Scanner(source)
tokens = scanner.scan()
if output_tokens:
write(tokens, source + "_tokenized")
debug('scanning complete')
parser = Parser(tokens, source, lib = lib)
bytecodes, success = parser.parse()
if output_bytecodes:
if is_debug:
write(prettify(bytecodes), source + "_unassembled")
else:
write(bytecodes, source + "_unassembled")
if not success:
print 'Parsing error'
return
debug('parsing complete')
assembler = Assembler(bytecodes)
assembled = assembler.assemble()
if is_debug:
write(prettify(assembled), dest + '_debug')
write(assembled, dest)
debug('assembly complete.' )
if is_interpret:
interp = Interpreter(out_stream, in_stream, code = assembled)
interp.interpret()
else:
debug('run program now with `python interpreter.py %s`' % dest)
示例13: test_ignoring_label_to_string
def test_ignoring_label_to_string(self):
""" Bytecode should not have label names."""
code = """nop
label:
nop"""
interpreter = Interpreter(code)
interpreter.compile()
self.assertFalse("label" in str(interpreter))
示例14: test_jmp_forward
def test_jmp_forward(self):
code = """jmp label
label:"""
interpreter = Interpreter(code)
interpreter.compile()
self.assertEqual(len(interpreter.instructions), 1)
self.assertEqual(interpreter.instructions[0], "00001000000000000000000000000100 ; I1: jmp 4")
self.assertEqual(interpreter.labels.get("label"), 4)
示例15: test_jmp_back
def test_jmp_back(self):
code = """label:
jmp label"""
interpreter = Interpreter(code)
interpreter.compile()
self.assertEqual(len(interpreter.instructions), 1)
self.assertEqual(interpreter.instructions[0], "00001000000000000000000000000000 ; I1: jmp 0")
self.assertEqual(interpreter.labels.get("label"), 0)