# loki.backend.pygen module

pygen(ir)

  Generate standard Python 3 code (that uses Numpy) from one or many IR objects/trees.

class PyCodegen(style, depth=0)

  Bases: `Stringifier`

  Tree visitor to generate standard Python code (with Numpy) from IR.

  visit_Sourcefile(o, **kwargs)
    Format as

      …modules… …subroutines…

  visit_Module(o, **kwargs)

    Format a `Module` as

        <repr(Module)>
          ...spec...
          ...routines...

  visit_Subroutine(o, **kwargs)
    Format as:

      …imports… def <name>(<args>):

      > …spec without imports and only declarations with initial values… …body…

  visit_GenericStmt(o, **kwargs)

    Format intrinsic nodes.

  visit_Comment(o, **kwargs)

    Format comments.

  visit_CommentBlock(o, **kwargs)

    Format comment blocks.

  visit_VariableDeclaration(o, **kwargs)
    Format declaration as

      <name> = <initial>

    and skip any arguments or scalars without an initial value

  visit_Import(o, **kwargs)

    Skip imports

  visit_Loop(o, **kwargs)
    Format loop with explicit range as
      for <var> in range(<start>, <end> + <incr>, <incr>):

        …body…

  visit_WhileLoop(o, **kwargs)
    Format loop as:
      while <condition>:

        …body…

  visit_Conditional(o, **kwargs)

    Format conditional as if <condition>:

    > …body…

    [elif <condition>:]

      […body…]

    [else:]

      […body…]

  visit_Assignment(o, **kwargs)
    Format statement as

      <target> = <expr> [<comment>]

  visit_Section(o, **kwargs)

    Format the section’s body.

  visit_CallStatement(o, **kwargs)
    Format call statement as

      <name>(<args>)

  visit_SymbolAttributes(o, **kwargs)
  visit_StatementFunction(o, **kwargs)

class PyCodeMapper(*args, **kwargs)

  Bases: `LokiStringifyMapper`

  Generate Python representation of expression trees using numpy syntax.

  map_logic_literal(expr, enclosing_prec, *args, **kwargs)
  map_float_literal(expr, enclosing_prec, *args, **kwargs)
  map_int_literal(expr, enclosing_prec, *args, **kwargs)
  map_cast(expr, enclosing_prec, *args, **kwargs)
  map_variable_symbol(expr, enclosing_prec, *args, **kwargs)
  map_meta_symbol(expr, enclosing_prec, *args, **kwargs)
  map_scalar(expr, enclosing_prec, *args, **kwargs)
  map_array(expr, enclosing_prec, *args, **kwargs)
  map_array_subscript(expr, enclosing_prec, *args, **kwargs)
  map_string_subscript(expr, enclosing_prec, *args, **kwargs)
  map_string_concat(expr, enclosing_prec, *args, **kwargs)
  map_inline_call(expr, enclosing_prec, *args, **kwargs)
  map_deferred_type_symbol(expr, *args, **kwargs)