Cog 2.3: Python 3 plus a few extras

Monday 27 February 2012This is close to 13 years old. Be careful.

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.

Comments

Add a comment:

Ignore this:
Leave this empty:
Name is required. Either email or web are required. Email won't be displayed and I won't spam you. Your web site won't be indexed by search engines.
Don't put anything here:
Leave this empty:
Comment text is Markdown.