{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Generated code layout" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are numerous code layout options in the [options](https://github.com/pthom/litgen/blob/main/src/litgen/options.py) file.\n", "\n", "```python\n", " ################################################################################\n", " # \n", " ################################################################################\n", " # \n", " original_location_flag_show = False\n", " # if showing location, how many parent folders shall be shown\n", " # (if -1, show the full path)\n", " original_location_nb_parent_folders = 0\n", " # If True, the complete C++ original signature will be show as a comment in the python stub (pyi)\n", " original_signature_flag_show = False\n", " # Size of an indentation in the python stubs\n", " python_indent_size = 4\n", " python_ident_with_tabs: bool = False\n", " # Insert as many empty lines in the python stub as found in the header file, keep comments layout, etc.\n", " python_reproduce_cpp_layout: bool = True\n", " # The generated code will try to adhere to this max length (if negative, this is ignored)\n", " python_max_line_length = 80\n", " # Strip (remove) empty comment lines\n", " python_strip_empty_comment_lines: bool = False\n", " # Run black formatter\n", " python_run_black_formatter: bool = False\n", " python_black_formatter_line_length: int = 88\n", "\n", " ################################################################################\n", " # \n", " ################################################################################\n", " # Spacing option in C++ code\n", " cpp_indent_size: int = 4\n", " cpp_indent_with_tabs: bool = False\n", "``````\n", "\n", "We demonstrate some of them below:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "
\n", "
\n", " \n", "\n", "\n", " \n", " \n", "
\n", "
\n", " \n", "
\n", "
    int add(int a, int b); // Adds two numbers\n",
       "
\n", "\n", "
\n", " \n", " \n", " \n", " \n", " \n", "
\n", "
\n", "
\n", " \n", "\n", "\n", " \n", " \n", "
\n", "
\n", " \n", "
\n", "
# int add(int a, int b);     /* original C++ signature */\n",
       "def add(a: int, b: int) -> int:\n",
       "  """ Adds two numbers"""\n",
       "  pass\n",
       "
\n", "\n", "
\n", " \n", " \n", " \n", " \n", " \n", "
\n", "
\n", "
\n", "\n", "\n", " \n", " \n", "
\n", "
\n", " \n", "
\n", "
m.def("add",\n",
       "\tadd, \n",
       "\tpy::arg("a"), py::arg("b"), \n",
       "\t"Adds two numbers");\n",
       "
\n", "\n", "
\n", " \n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import litgen\n", "from litgen.demo import litgen_demo\n", "\n", "options = litgen.LitgenOptions()\n", "cpp_code = \"\"\"\n", " int add(int a, int b); // Adds two numbers\n", "\"\"\"\n", "\n", "options.cpp_indent_with_tabs = True # The C++ code will be indented with \n", "options.cpp_indent_size = 1 # one tab\n", "\n", "options.python_indent_size = 2 # The python code will be indented with 2 spaces\n", "options.python_run_black_formatter = False # (if black is disabled)\n", "\n", "options.original_signature_flag_show = True # We will show the original C++ signatures in the python stubs\n", "\n", "\n", "litgen_demo.demo(options, cpp_code, show_pydef=True)" ] } ], "metadata": { "kernelspec": { "display_name": "venv311", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.0" } }, "nbformat": 4, "nbformat_minor": 2 }