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.