{"id":20315809,"url":"https://github.com/flipeador/cpp20-and-python-c-api","last_synced_at":"2025-03-04T09:12:47.913Z","repository":{"id":157621891,"uuid":"379304566","full_name":"flipeador/CPP20-and-Python-C-API","owner":"flipeador","description":"C++20 and Python/C API: Using Python from C++20.","archived":false,"fork":false,"pushed_at":"2021-06-22T15:10:52.000Z","size":13,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-01-14T12:53:14.453Z","etag":null,"topics":["cpp","cpp20","python","python3","pythoncapi","visual-studio","visual-studio-2019"],"latest_commit_sha":null,"homepage":"","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/flipeador.png","metadata":{"files":{"readme":"README.md","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":"2021-06-22T14:47:12.000Z","updated_at":"2024-06-21T03:40:19.000Z","dependencies_parsed_at":null,"dependency_job_id":"993c63d3-f044-4764-bbd5-c0721bcdbf3c","html_url":"https://github.com/flipeador/CPP20-and-Python-C-API","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/flipeador%2FCPP20-and-Python-C-API","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flipeador%2FCPP20-and-Python-C-API/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flipeador%2FCPP20-and-Python-C-API/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flipeador%2FCPP20-and-Python-C-API/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/flipeador","download_url":"https://codeload.github.com/flipeador/CPP20-and-Python-C-API/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241818902,"owners_count":20025210,"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":["cpp","cpp20","python","python3","pythoncapi","visual-studio","visual-studio-2019"],"created_at":"2024-11-14T18:21:59.169Z","updated_at":"2025-03-04T09:12:47.891Z","avatar_url":"https://github.com/flipeador.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CPP20-and-Python-C-API\nC++20 and Python/C API.\n\nRequires: Python 3.\n\n`C:\\Program Files\\Python\\include`\n\n`C:\\Program Files\\Python\\libs`\n\n## Example\n### consoleapp.cpp\n```cpp\n#include \"python.h\"\n\n#include \u003ciostream\u003e\n\nusing namespace std;\n\nPyObject* f_hello_world(PyObject* pdata, PyObject* pargs, PyObject* pkwargs)\n{\n    Py_KwArgsFromCFunc(pargs, pkwargs);\n\n    string hello = Py_GetItemStr(args, 0);  // 'hello'\n    string world = Py_GetItemStr(args, 1);  // 'world'\n    string end   = Py_GetItemStr(args, 2);  // '!'\n\n    string space = Py_GetValueStr(kwargs, \"space\");  // ' '\n\n    return Py::Str(hello + space + world + end)\n        .AddRef();\n};\n\nint main()\n{\n    Py::Initialize();\n\n    // Load module 'main.py'.\n    Py::Module mod(\"main\");\n\n    // Generate 1000 digits of Pi.\n    Py::Callable pi_digits_str = mod.GetAttr(\"pi_digits_str\");\n    Py::Str result = pi_digits_str(Py::Int(1000l));\n    cout \u003c\u003c result.GetUTF8() \u003c\u003c endl;\n\n    cout \u003c\u003c endl;\n\n    // Print *args and **kwargs.\n    Py::Callable print_args = mod.GetAttr(\"print_args\");\n    print_args(\n        Py::Tuple::FromValues(\n            Py::Str(\"value #1\"),\n            Py::Str(\"value #2\")\n        ),\n        Py::Dict::FromValues(\n            Py::Str(\"key #1\"), Py::Int(256l),\n            Py::Str(\"key #2\"), Py::Float(3.14)\n        )\n    );\n\n    cout \u003c\u003c endl;\n\n    // Print 'Hello World!'.\n    Py::Callable hello_world = mod.GetAttr(\"hello_world\");\n    Py::Str hello_world_str = hello_world(Py::Callable(f_hello_world));\n    cout \u003c\u003c hello_world_str.GetUTF8() \u003c\u003c endl;\n\n    return 0;\n}\n```\n### main.py\n```py3\ndef pi_digits(n):\n    \"\"\"\n    Generate n digits of Pi.\n    https://gist.github.com/deeplook/4947835\n    \"\"\"\n    k, a, b, a1, b1 = 2, 4, 1, 12, 4\n    while n \u003e 0:\n        p, q, k = k * k, 2 * k + 1, k + 1\n        a, b, a1, b1 = a1, b1, p * a + q * a1, p * b + q * b1\n        d, d1 = a / b, a1 / b1\n        while d == d1 and n \u003e 0:\n            yield int(d)\n            n -= 1\n            a, a1 = 10 * (a % b), 10 * (a1 % b1)\n            d, d1 = a / b, a1 / b1\n\ndef pi_digits_str(n):\n    digits = [str(n) for n in list(pi_digits(n))]\n\n    return '{}.{}'.format(\n        digits.pop(0),\n        ''.join(digits)\n    )\n\ndef print_args(*args, **kwargs):\n    print('\\n'.join(args))\n    for key, value in kwargs.items():\n        print(f'{key}={value}')\n\ndef hello_world(f_hello_world):\n    return f_hello_world('Hello', 'World', '!', space=' ')\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflipeador%2Fcpp20-and-python-c-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fflipeador%2Fcpp20-and-python-c-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflipeador%2Fcpp20-and-python-c-api/lists"}