=== Cog === .. Created. Version 1.1. Minor edits for clarity. Updated to cog 1.11, added a See Also section, and fixed a sample. Updated to cog 1.12. Updated to cog 1.2. Updated to cog 1.3. Updated to cog 1.4. Added links to other Cog implementations. Added links to 2.0 beta 2. Updating for 2.0. Added PCG. Added an explicit mention of the license: MIT. Added links to 3rd-party packages. Clarified -D value types, and fixed a 3rd-party link. Tried to explain better about indentation, and fixed an incorrect parameter name. Added -U switch from Alexander Belchenko. Fixed the russian pointer to be to a current document. Removed handyxml, files are now at pypi. Python 3 is supported! Polish up Cog 2.3 Version 2.4 Version 3.0.0 Version 3.2.0 Version 3.3.0 Cog is a file generation tool. It lets you use pieces of Python code as generators in your source files to generate whatever text you need. This page describes version 3.4.1, released March 7, 2024. What does it do? ================ Cog transforms files in a very simple way: it finds chunks of Python code embedded in them, executes the Python code, and inserts its output back into the original file. The file can contain whatever text you like around the Python code. It will usually be source code. For example, if you run this file through cog: .. code-block:: cpp // This is my C++ file. ... /*[[[cog import cog fnames = ['DoSomething', 'DoAnotherThing', 'DoLastThing'] for fn in fnames: cog.outl("void %s();" % fn) ]]]*/ //[[[end]]] ... it will come out like this: .. code-block:: cpp // This is my C++ file. ... /*[[[cog import cog fnames = ['DoSomething', 'DoAnotherThing', 'DoLastThing'] for fn in fnames: cog.outl("void %s();" % fn) ]]]*/ void DoSomething(); void DoAnotherThing(); void DoLastThing(); //[[[end]]] ... Lines with triple square brackets are marker lines. The lines between ``[[[cog`` and ``]]]`` are the generator Python code. The lines between ``]]]`` and ``[[[end]]]`` are the output from the generator. Output is written with `cog.outl()`, or if you use the ``-P`` option, normal `print()` calls. When cog runs, it discards the last generated Python output, executes the generator Python code, and writes its generated output into the file. All text lines outside of the special markers are passed through unchanged. The cog marker lines can contain any text in addition to the triple square bracket tokens. This makes it possible to hide the generator Python code from the source file. In the sample above, the entire chunk of Python code is a C++ comment, so the Python code can be left in place while the file is treated as C++ code. Installation ============ Cog requires Python 3.7 or higher. Cog is installed in the usual way, except the installation name is "cogapp", not "cog": .. code-block:: bash $ python3 -m pip install cogapp You should now have a "cog" command you can run. See the :ref:`changelog ` for the history of changes. Cog is distributed under the `MIT license`_. Use it to spread goodness through the world. .. _MIT License: http://www.opensource.org/licenses/mit-license.php More ==== .. toctree:: :maxdepth: 1 changes design source module running