Cog 2.3: Python 3 plus a few extras

Monday 27 February 2012

PyCon is nearly upon us, which means I am preparing slides for my talk, which means I am using Cog again. Cog runs bits of Python code in a text file, and interpolates the output of the code into the file. It's useful for writing small programs to augment text, in this case, to produce code output in my HTML file.

This year I needed some Python 3 examples, so the biggest change in Cog v2.3 is that it is runnable on Python 3. I also took the opportunity to drop support for Pythons older than 2.6.

Having both Python 2 and Python 3 examples in one file means I need to run it through Cog twice, once under each version of Python. But when running in Python 2, I need the Python 3 examples to stay as they are. So I added a new attribute to the cog module, cog.previous. This is simply a string containing the output from the last run of Cog. A code chunk can now decide to "do nothing" simply by outputting cog.previous.

A few other mini-features are in: a dash as a file name means process standard in, and Cog can be run as "python -m cogapp," which helped my 2 vs. 3 switcheroo.

Enjoy Cog 2.3.

tagged: » react


Add a comment:

Ignore this:
not displayed and no spam.
Leave this empty:
not searched.
Name and either email or www are required.
Don't put anything here:
Leave this empty:
URLs auto-link and some tags are allowed: <a><b><i><p><br><pre>.