{"id":18549375,"url":"https://github.com/lopez86/ctypes-tutorial","last_synced_at":"2025-07-14T10:07:45.561Z","repository":{"id":161776518,"uuid":"255990008","full_name":"lopez86/ctypes-tutorial","owner":"lopez86","description":"Tutorial on How to Incorporate C \u0026 C++ Code into Python Projects","archived":false,"fork":false,"pushed_at":"2020-04-16T03:06:50.000Z","size":14,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-15T09:12:39.963Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lopez86.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2020-04-15T17:30:15.000Z","updated_at":"2020-04-16T03:06:53.000Z","dependencies_parsed_at":null,"dependency_job_id":"4e152b30-814c-4d90-be49-b42536fb14aa","html_url":"https://github.com/lopez86/ctypes-tutorial","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/lopez86/ctypes-tutorial","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lopez86%2Fctypes-tutorial","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lopez86%2Fctypes-tutorial/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lopez86%2Fctypes-tutorial/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lopez86%2Fctypes-tutorial/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lopez86","download_url":"https://codeload.github.com/lopez86/ctypes-tutorial/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lopez86%2Fctypes-tutorial/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265277557,"owners_count":23739408,"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-06T20:38:48.481Z","updated_at":"2025-07-14T10:07:45.540Z","avatar_url":"https://github.com/lopez86.png","language":"C++","readme":"# ctypes-tutorial\n## Tutorial on Running C \u0026amp; C++ Code in Python\n\n### Intro\n\nThis project provides a tutorial on how to use the python `ctypes` package to use C-compatible binaries in your python code.\n\nThe simple example here is a system to run basic banking transactions like account creation, checking balances, withdrawing money, and depositing money.\nThe code is written in C++ with a provided C API that is then used in some Python code. It uses some complex (but still simple) types\nto show how you can get your python code to talk to things like C++ objects.\n\n### Why?\n\nIf you're like me and come from a background of doing scientific computing mostly in C++, you may have run into cases where you\nhave some C++ code that you'd like to run in a python program. Maybe it's some high-performance mathematical operations. \nMaybe it's a 3rd party C or C++ API to run a piece of equipment that you bought. There are a number of things you can do from here.\n\nIn some cases, you can just rewrite the code in python and run it. That's going to be the simplest way to do it but in cases like\nI just mentioned above that may not be feasible. In that case, you can also rewrite it in Cython to still get the performance of C.\nHowever, Cython is also a pain to deal with and is sort of like its own language, so there's a lot of mental overhead in terms of \njust having to learn Cython and also having to learn the more complicated build system.\n\nThe other thing that you can do is to use a python package to just call your code directly as if it were python. \nThe ctypes package is an official python package that does exactly this. It's not the only package though. I haven't looked too\nmuch into other packages but there are others with slightly different features that might be more appropriate depending on your use case.\nFor example, the particle physics analysis package ROOT comes with its own method of automatically generating python bindings for\nmuch of your code.\n\n### Requirements\n 1) python3\n 1) make\n 1) gcc and g++\n\n### Using the code\nI have not set this up as a python package, so once you download, you need to make everything yourself with:\n```bash\nmake\n```\n\nCurrently, there are two examples, which do more or less the same thing. `make` will build an example in C, which you can run with:\n```bash\nLD_LIBRARY_PATH:$PWD/banking:$LD_LIBRARY_PATH ./bank_example.exe\n```\nand a python example, which you can run with\n```bash\npython bank_example.py\n```\n\n### Future improvements\n\nA few future improvements I hope to make are:\n 1) Add a simple flask server so that users can add arbitrary data\n 1) Maybe make a Rust implementation too since that is going to be more complicated than C++\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flopez86%2Fctypes-tutorial","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flopez86%2Fctypes-tutorial","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flopez86%2Fctypes-tutorial/lists"}