{"id":17036764,"url":"https://github.com/bast/python-cffi-demo","last_synced_at":"2025-04-12T13:20:43.601Z","repository":{"id":72326566,"uuid":"36558479","full_name":"bast/python-cffi-demo","owner":"bast","description":"Python CFFI demo: interfacing C++, Fortran, and Python.","archived":false,"fork":false,"pushed_at":"2017-12-28T14:53:23.000Z","size":74,"stargazers_count":22,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-26T07:51:16.944Z","etag":null,"topics":["cffi","cpp","fortran","python","testing"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bast.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}},"created_at":"2015-05-30T13:10:05.000Z","updated_at":"2024-01-04T11:54:13.000Z","dependencies_parsed_at":null,"dependency_job_id":"36e41c89-d382-4414-aadb-3693928d96b6","html_url":"https://github.com/bast/python-cffi-demo","commit_stats":{"total_commits":89,"total_committers":1,"mean_commits":89.0,"dds":0.0,"last_synced_commit":"e1bb069f4f11af4589230ee64eb1faa15eaea371"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bast%2Fpython-cffi-demo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bast%2Fpython-cffi-demo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bast%2Fpython-cffi-demo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bast%2Fpython-cffi-demo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bast","download_url":"https://codeload.github.com/bast/python-cffi-demo/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248571837,"owners_count":21126522,"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":["cffi","cpp","fortran","python","testing"],"created_at":"2024-10-14T08:51:53.389Z","updated_at":"2025-04-12T13:20:43.580Z","avatar_url":"https://github.com/bast.png","language":"Python","readme":"[![Build Status](https://travis-ci.org/bast/python-cffi-demo.svg?branch=master)](https://travis-ci.org/bast/python-cffi-demo/builds)\n[![License](https://img.shields.io/badge/license-%20MPL--v2.0-blue.svg)](../master/LICENSE)\n\n\n# python-cffi-demo\n\nInspired by Armin Ronacher's\n[\"Beautiful Native Libraries\"](http://lucumr.pocoo.org/2013/8/18/beautiful-native-libraries/).\n\n\n## Example\n\nIn this example we imagine we are on a desert island and wish to compute pi by\nthrowing darts:\n\n![](../master/img/darts.jpg \"darts\")\n\nThis example is implemented in 3 different languages (C++, Fortran, Python) and\nwe demonstrate how to call this functionality across languages.\n\nThese 3 implementations are combined in an example Python package that we call `pi`.\nAt the same time we demonstrate how to automatically test the interface and the\n3 implementations.\n\n\n## Limitations\n\nWe do not discuss memory allocation strategies.\nFor this have a look at [this demo](https://github.com/bast/cffi-mem-alloc-example).\n\n\n## Lower-level learning goals\n\n- Approximate pi using the Monte Carlo method\n- Calling Fortran libraries from C(++)\n- Calling C(++) libraries from Fortran\n- Calling Fortran/C(++) libraries from Python using [Python CFFI](https://cffi.readthedocs.io)\n- Automatically testing Fortran/C(++) libraries on Linux and Mac OS X using\n  [pytest](https://docs.pytest.org) and [Travis CI](https://travis-ci.org)\n- Hiding CMake infrastructure behind a simple `pip install`\n\n\n## Higher-level learning goals\n\n- Automatically test dynamic Fortran/C(++) libraries\n- Write tests without recompiling the code\n- Speed up your Python code\n- Provide a Python API to your compiled library and leverage Python tools\n\n\n## Requirements\n\n- Python\n- [pytest](https://docs.pytest.org)\n- [Python CFFI](https://cffi.readthedocs.io)\n- [CMake](https://cmake.org/download/)\n- Fortran and C++ compilers\n\n\n## Installing Python dependencies\n\nIn this example using [Virtual Environments](http://python-guide.readthedocs.io/en/latest/dev/virtualenvs/)\nbut also\n[Anaconda](https://www.continuum.io/downloads) or\n[Miniconda](https://conda.io/miniconda.html) will do the job:\n\n```\nvirtualenv venv\nsource venv/bin/activate\npip install -r requirements.txt\n```\n\n\n## How to configure and build the compiled libraries\n\n```\nmkdir build\ncd build\ncmake ..\nmake\n```\n\n\n## How to test this demo\n\n```\nPI_LIBRARY_DIR=build/lib PI_INCLUDE_DIR=island pytest -vv test.py\n```\n\n\n## Installing with pip\n\nThis example comes with a full-fledged setup script which configures\nand builds the code under the hood and makes it possible to install the demo\nwith `pip`:\n\n```\nvirtualenv venv\nsource venv/bin/activate\npip install git+https://github.com/bast/python-cffi-demo.git\npython -c 'import island; print(island.approximate_pi_c(100))'\n```\n\n\n## C(++) calling Fortran and vice versa\n\n```shell\n$ cd build\n\n$ ./bin/pi_cpp.x\npi computed by c = 3.141664\npi computed by fortran = 3.141636\n\n$ ./bin/pi_fortran.x\npi computed by fortran =    3.1416358947753906\npi computed by c =    3.1416640000000000\n```\n\n\n## Timing the libraries through a Python interface\n\nDefault is 2M points but feel free to experiment by increasing the number\nof points in `test.py`.\n\n```shell\n$ PI_LIBRARY_DIR=build/lib PI_INCLUDE_DIR=island python test.py\n\nnum points: 2000000\npython  pi=3.14163 time spent: 1.749 sec\nc       pi=3.14190 time spent: 0.041 sec\nfortran pi=3.14225 time spent: 0.126 sec\n```\n\n\n## How you can contribute\n\nFeel free to improve the C++, Fortran, and Python codes.\n\nIf you know intuitive examples that we can use to demonstrate memory allocation\nstrategies, please suggest these.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbast%2Fpython-cffi-demo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbast%2Fpython-cffi-demo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbast%2Fpython-cffi-demo/lists"}