Tools for Source Code Generation

class pytools.codegen.CodeGenerator[source]

Language-agnostic functionality for source code generation.

extend(sub_generator: pytools.codegen.CodeGenerator) None[source]
get() str[source]
add_to_preamble(s: str) None[source]
__call__(s: str) None[source]

Call self as a function.

indent() None[source]
dedent() None[source]
class pytools.codegen.Indentation(generator: pytools.codegen.CodeGenerator)[source]

A context manager for indentation for use with CodeGenerator.

__enter__() None[source]
__exit__(exc_type: Any, exc_val: Any, exc_tb: Any) None[source]
pytools.codegen.remove_common_indentation(code: str, require_leading_newline: bool = True)[source]

Remove leading indentation from one or more lines of code.

Removes an amount of indentation equal to the indentation level of the first nonempty line in code.

  • code – Input string.

  • require_leading_newline – If True, only remove indentation if code starts with \n.


A copy of code stripped of leading common indentation.