{"id":18123284,"url":"https://github.com/jay-johnson/python-that-runs-c-plus-plus","last_synced_at":"2025-04-06T12:45:33.550Z","repository":{"id":72596553,"uuid":"65053187","full_name":"jay-johnson/python-that-runs-c-plus-plus","owner":"jay-johnson","description":"Bind C++ objects, methods and classes for use with Python 2.7, and it builds with cmake","archived":false,"fork":false,"pushed_at":"2016-08-05T22:47:43.000Z","size":10,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-12T18:49:59.315Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jay-johnson.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-08-05T22:38:37.000Z","updated_at":"2024-02-05T04:37:00.000Z","dependencies_parsed_at":"2024-07-17T06:30:36.942Z","dependency_job_id":null,"html_url":"https://github.com/jay-johnson/python-that-runs-c-plus-plus","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jay-johnson%2Fpython-that-runs-c-plus-plus","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jay-johnson%2Fpython-that-runs-c-plus-plus/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jay-johnson%2Fpython-that-runs-c-plus-plus/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jay-johnson%2Fpython-that-runs-c-plus-plus/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jay-johnson","download_url":"https://codeload.github.com/jay-johnson/python-that-runs-c-plus-plus/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247485253,"owners_count":20946398,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-01T07:08:57.152Z","updated_at":"2025-04-06T12:45:33.533Z","avatar_url":"https://github.com/jay-johnson.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"====================\nPython that runs C++\n====================\n\nHere is how to use C++ from Python 2.7\n\nThis repository builds a shared object file using cmake_. This shared object file maps and binds C++ methods, objects and classes with `boost python`_ so they can be used in python after importing the file with ``import libpyapi_wrapper_ext``\n\nI plan to use this repository for integrating a new third-party API that is distributed only in C++.\n\n.. _cmake: https://cmake.org/\n.. _boost python: https://wiki.python.org/moin/boost.python/GettingStarted\n\nHow to run C++ methods from Python\n----------------------------------\n\nHere is the python demo code:\n\n::\n\n    #!/usr/bin/python\n\n    # Import the PyAPI Wrapper shared object file (extension)\n    import libpyapi_wrapper_ext\n\n    # Pass python variables through the method\n    some_test_int = 7\n    some_test_str = \"This is how to setup Python to call a C++ API\"\n\n    # call C++ Wrapper demo method\n    print \"----------------------------\"\n    print \"Running C++ demo method from Python\"\n    print libpyapi_wrapper_ext.demo_print_method(some_test_int, some_test_str)\n\n    print \"Trying C++ Class from Python\"\n    api_key     = \"ASDFASDFASDF\"\n    api_skey    = \"APISECRET\"\n    wrapper     = libpyapi_wrapper_ext.PyAPIWrapper(\"demoapi\", api_key, api_skey)\n\n    str_value   = \"Passing a string into C++\"\n    print wrapper.perform_api_action(str_value)\n\nHow it Works\n------------\n\n#.  Import the C++ shared object file\n\n    ::\n\n        import libpyapi_wrapper_ext\n\n#.  Call the ``demo_print_method``\n\n    ::\n\n        print libpyapi_wrapper_ext.demo_print_method(some_test_int, some_test_str)\n\n    Under the hood, this code calls a C++ method `src/lib_pyapi_wrapper_ext.cpp#L24`_ that is bound by the line:\n\n    ::\n\n        def(\"demo_print_method\", demo_print_method);\n\n    Which runs the C++ method below from `src/lib_pyapi_wrapper_ext.cpp#L13`_\n\n    ::\n\n        std::string demo_print_method(int test_int, std::string test_string)\n        {\n            std::stringstream ss;\n            ss \u003c\u003c \" - Testing Conversion Integer(\" \u003c\u003c test_int \u003c\u003c \") String(\" \u003c\u003c test_string.c_str() \u003c\u003c \")\" \u003c\u003c std::endl;\n            return ss.str();\n        }\n    \n    .. _src/lib_pyapi_wrapper_ext.cpp#L24: https://github.com/jay-johnson/python-that-runs-c-plus-plus/blob/master/src/lib_pyapi_wrapper_ext.cpp#L24\n    .. _src/lib_pyapi_wrapper_ext.cpp#L13: https://github.com/jay-johnson/python-that-runs-c-plus-plus/blob/master/src/lib_pyapi_wrapper_ext.cpp#L13\n\n#.  Create a Python-C++ API Object\n\n    This creates a python wrapper object for demoing how to use a C++ API from python. The sample **PyAPIWrapper** class is defined in the `headers/pyapi_wrapper.h`_\n\n    ::\n\n        api_key     = \"ASDFASDFASDF\"\n        api_skey    = \"APISECRET\"\n        wrapper     = libpyapi_wrapper_ext.PyAPIWrapper(\"demoapi\", api_key, api_skey)\n\n    .. _headers/pyapi_wrapper.h: https://github.com/jay-johnson/python-that-runs-c-plus-plus/blob/master/headers/pyapi_wrapper.h\n\n\n#.  Once the wrapper object is instantiated in python, it can invoke methods exposed by the shared object library file `src/lib_pyapi_wrapper_ext.cpp#L25`_. Here is the code for exposing one of the PyAPIWrapper member methods:\n\n    ::\n\n        .def(\"perform_api_action\", \u0026PyAPIWrapper::perform_api_action)\n\n    This code binds the **PyAPIWrapper::perform_api_action** method from the cpp implementation file `src/pyapi_wrapper.cpp#L14`_\n\n    .. _src/pyapi_wrapper.cpp#L14: https://github.com/jay-johnson/python-that-runs-c-plus-plus/blob/master/src/pyapi_wrapper.cpp#L14\n    .. _src/pyapi_wrapper.cpp#L25: https://github.com/jay-johnson/python-that-runs-c-plus-plus/blob/master/src/pyapi_wrapper.cpp#L25\n        \n\nBuild Steps\n-----------\n\n#. Build the C++ wrapper using CMake\n\n    ::\n        \n        $ cmake .\n        -- The C compiler identification is GNU 5.1.1\n        -- The CXX compiler identification is GNU 5.1.1\n        -- Check for working C compiler: /usr/lib64/ccache/cc\n        -- Check for working C compiler: /usr/lib64/ccache/cc -- works\n        -- Detecting C compiler ABI info\n        -- Detecting C compiler ABI info - done\n        -- Detecting C compile features\n        -- Detecting C compile features - done\n        -- Check for working CXX compiler: /usr/lib64/ccache/c++\n        -- Check for working CXX compiler: /usr/lib64/ccache/c++ -- works\n        -- Detecting CXX compiler ABI info\n        -- Detecting CXX compiler ABI info - done\n        -- Detecting CXX compile features\n        -- Detecting CXX compile features - done\n        -- Boost version: 1.57.0\n        -- Found the following Boost libraries:\n        --   filesystem\n        --   system\n        --   date_time\n        --   python\n        ---- Boost Include:  /usr/include\n        ---- Boost Libs:     /usr/lib64/libboost_filesystem.so/usr/lib64/libboost_system.so/usr/lib64/libboost_date_time.so/usr/lib64/libboost_python.so\n        -- Found PythonLibs: /usr/lib64/libpython2.7.so (found version \"2.7.10\") \n        ---- Python Include: /usr/include/python2.7\n        ---- Python Libs:    /usr/lib64/libpython2.7.so\n        -- Found PythonLibs: python2.7 (found version \"2.7.10\") \n        -- Boost version: 1.57.0\n        -- Boost version: 1.57.0\n        -- Found the following Boost libraries:\n        --   python\n        -- Configuring done\n        -- Generating done\n        -- Build files have been written to: \u003cpath\u003e\n\n\n#.  Make it \n\n    ::\n\n        $ make -j4\n        Scanning dependencies of target pyapi_wrapper_ext\n        [ 33%] Building CXX object CMakeFiles/pyapi_wrapper_ext.dir/src/pyapi_wrapper.cpp.o\n        [ 66%] Building CXX object CMakeFiles/pyapi_wrapper_ext.dir/src/lib_pyapi_wrapper_ext.cpp.o\n        [100%] Linking CXX shared module libpyapi_wrapper_ext.so\n        [100%] Built target pyapi_wrapper_ext\n        $\n\n\n#.  Run the demo\n\n\n    ::\n    \n        $ ./run_api_demo.py \n        ----------------------------\n        Running C++ method from Python\n         - Testing Conversion Integer(7) String(This is how to setup Python to call a C++ API)\n\n        Trying C++ Class from Python\n        PyAPI(demoapi) running PerformAction with String(Passing a string into C++)\n\n        $\n\nAdding Custom Code\n------------------\n\n#.  Add your own cpp files to the source list found in the `CMakeLists.txt`_\n\n    .. _CMakeLists.txt: https://github.com/jay-johnson/python-that-runs-c-plus-plus/blob/master/CMakeLists.txt#L55\n\n#.  Run ``cmake .``\n\n#.  Run ``make``\n\nLicense\n-------\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjay-johnson%2Fpython-that-runs-c-plus-plus","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjay-johnson%2Fpython-that-runs-c-plus-plus","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjay-johnson%2Fpython-that-runs-c-plus-plus/lists"}