""" Qpy example module. """ # f, an xml template def f:xml(x): "
" x "
" # 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("
")) qpy_append(x) qpy_append(_qpy_xml("
")) return _qpy_join_xml(qpy_accumulation) from qpy import xml 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) assert isinstance(f(x), xml) sys.stdout.write("Success!\n")