Generated code layout#
There are numerous code layout options in the options file.
################################################################################
# <Layout settings for the generated python stub code>
################################################################################
# <show the original location and or signature of elements as a comment>
original_location_flag_show = False
# if showing location, how many parent folders shall be shown
# (if -1, show the full path)
original_location_nb_parent_folders = 0
# If True, the complete C++ original signature will be show as a comment in the python stub (pyi)
original_signature_flag_show = False
# Size of an indentation in the python stubs
python_indent_size = 4
python_ident_with_tabs: bool = False
# Insert as many empty lines in the python stub as found in the header file, keep comments layout, etc.
python_reproduce_cpp_layout: bool = True
# The generated code will try to adhere to this max length (if negative, this is ignored)
python_max_line_length = 80
# Strip (remove) empty comment lines
python_strip_empty_comment_lines: bool = False
# Run black formatter
python_run_black_formatter: bool = False
python_black_formatter_line_length: int = 88
################################################################################
# <Layout settings for the C++ generated pydef code>
################################################################################
# Spacing option in C++ code
cpp_indent_size: int = 4
cpp_indent_with_tabs: bool = False
We demonstrate some of them below:
import litgen
from litgen.demo import litgen_demo
options = litgen.LitgenOptions()
cpp_code = """
int add(int a, int b); // Adds two numbers
"""
options.cpp_indent_with_tabs = True # The C++ code will be indented with
options.cpp_indent_size = 1 # one tab
options.python_indent_size = 2 # The python code will be indented with 2 spaces
options.python_run_black_formatter = False # (if black is disabled)
options.original_signature_flag_show = True # We will show the original C++ signatures in the python stubs
litgen_demo.demo(options, cpp_code, show_pydef=True)
int add(int a, int b); // Adds two numbers
# int add(int a, int b); /* original C++ signature */
def add(a: int, b: int) -> int:
""" Adds two numbers"""
pass
m.def("add",
add,
py::arg("a"), py::arg("b"),
"Adds two numbers");
m.def("add",
add,
nb::arg("a"), nb::arg("b"),
"Adds two numbers");