""" Qpy example module. This one uses a variety of patterns to test the compiler. """ # f, an xml template def f:xml(x): {"": 3} [''] '<%s %s>' % ('<>', 1) # f_equivalent, a function with the same behavior as f import sys if sys.version.startswith("3"): def get_code(g): return g.__code__ else: def get_code(g): return g.func_code from qpy import xml as _qpy_xml, join_xml as _qpy_join_xml def f_equivalent(x): qpy_accumulation = [] qpy_append = qpy_accumulation.append qpy_append({_qpy_xml(""): 3}) qpy_append([_qpy_xml("")]) qpy_append(_qpy_xml("<%s %s>") % (_qpy_xml('<>'), 1)) return _qpy_join_xml(qpy_accumulation) if not sys.platform.startswith("java"): import dis sys.stdout.write("Here is the byte-code for the template f:\n") print(get_code(f)) dis.disassemble(get_code(f)) sys.stdout.write("\nHere is the byte-code for the function f_equivalent:\n") dis.disassemble(get_code(f_equivalent)) sys.stdout.write("\n") for x in (None, 1, 'ok', '
', str): sys.stdout.write("f(%r) -> %r\n" % (x, f(x))) assert f(x) == f_equivalent(x), "\n%s\n%s\n" % (f(x), f_equivalent(x)) assert isinstance(f(x), _qpy_xml) sys.stdout.write("Success!\n")