Compile-Time Metaprogramming and Python

2007-05-31 at 18:09 | In devel, lang:en, media, review | Leave a Comment

Recently I’ve listened to an episode of SE Radio named Compile-Time Metaprogramming. It was quite nice. There is a discussion on the comments page between me and Laurence Tratt who was the guest of Markus Völter in this episode. I asked him about implementing the idea he’d talked about in run-time, and he replied that it could be a significant performance penalty. But I think run-time pre-compilation may reduce time of eval()uating very much.

I’ve written an example to show the idea. Actually, it is a prototype of JSON to Python bytecode compiler. I’ve used an Earley parser from SPARK library by John Aycock to build a parse tree from a JSON string and a BNF grammar of JSON. If you’re interested in getting the source, please leave a comment. The example of run-time pre-compilation (although the compiled code object is still interpreted) is shown below:

def main():
    # Pre-compilation, code is a Python code object
    code = generate_json_code("""
    {
        "GeoLoc":
        {
            "Latitude":  60.5667,
            "Longitude": 5.3959,
	}
        "Date": [2007, 05, 31]
    }
    """)
    call_json = lambda: eval(code)
    # Execution
    json = call_json()
    print "Today is %s" % str(json.Date)
    print "My geoloc is %.4f, %.4f" % (json.GeoLoc.Latitude,
        json.GeoLoc.Longitude)

def generate_json_code(str):
    tokens = tokenize(str)
    ast = JSONParser().parse(tokens)
    code = JSONTranslator(ast).translate()
    return code

And here is my outline of this interview:

Continue reading Compile-Time Metaprogramming and Python…

Yokozuna Musashimaru Koyo

2007-05-03 at 04:15 | In fun, lang:en, media | Leave a Comment

According to Wikipedia:

Musashimaru Koyo was declared the most successful foreign sumo wrestler ever during his retirement ceremony on 2004-10-02.

This yokozuna is a character of a famous Russian radio joke. An announcer just couldn’t pronounce his name and rank without laughing out loud because it sounded somewhat silly in Russian :) Listen to a record of this radio show :D

Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.