3.1 bytecodehacks.code_gen.method_spec

I use this module to enable me to write the execute and get_stack_bump methods for the ops module despite the latter being autogenerated.

(Gruntle: I wouldn't need this if Python had multimethods).

Let's have ahopefully illustrative example. Suppose the file ``foo_methods'' contains:

CLASS_1:
    return "one", arg
CLASS_2:
    return "two", arg

then the code

from bytecodehacks.code_gen.method_spec import MethodSpecifier

m = MethodSpecifier("foo_methods")
m.set_template("""\
    def foo(self,arg):
%s""")

print "the whole method from CLASS_1:"
print m.get_method("CLASS_1")
print "just the method body from CLASS_2:"
print m.get_method_body("CLASS_2")

yields the output

the whole method from CLASS_1:
    def foo(self,arg):
        return "one", arg
just the method body from CLASS_2:
        return "two", arg

Send comments to mwh@python.net