A Fortran core behind a modern layer

This group is one small project built end to end in this container: a statistics core written in Fortran and compiled into a shared library, a Python module that calls it through ctypes, a service on http.server that publishes it as JSON, and a test suite on each side. Every file is shown on the pages, every command is the one that was run, and the output is what came back.

The point of the shape is that the Fortran part is a plain C library and the Python part knows nothing about Fortran. Nothing in the Python files imports a Fortran-specific package, and nothing in the Fortran files knows that a Python program exists: the boundary is BIND(C) on one side and argtypes on the other. The ctypes lesson of the course writes the first version of that boundary; this project is the version with a build, a service and tests.

Core The Fortran core and its shared library Four functions over double arrays with BIND(C), built with -shared -fPIC, the four exported symbols, and a Fortran test program that calls the library through its C interface. Open Core The Python wrapper with ctypes A wrapper module with argtypes and restype for every function, an empty sequence refused before the call, a status code turned into an exception, nine unittest cases and the same numbers checked against numpy. Open Core The HTTP service python3 http.server over the same core: two routes, a JSON body, a 400 for a bad value, a 404 for an unknown path, the curl session that shows all four, and four tests against a server on port 0. Open Core Tests on both sides The Fortran test program with its TAP lines and status, the Python suite with unittest, one runner script that runs both, and what the two sides prove that the other cannot. Open Core The build, the lints and what is missing build.sh and the three scripts, ruff, mypy and pylint on the four Python files, the numpy check and the gunicorn run in a virtual environment, and the project layout a reader can copy. Open

The project

Nine files in three directories, about five hundred lines together, of which the Fortran core is eighty-seven and the four Python files are three hundred and ten:

.
├── build.sh
├── core
│   ├── stats.f90
│   └── test_core.f90
├── python
│   ├── numerics.py
│   ├── service.py
│   ├── test_numerics.py
│   └── test_service.py
├── run-tests.sh
└── test_core.sh

filewhat it is
core/stats.f90the numerical core: mean, standard deviation, median and quantile, all BIND(C)
core/test_core.f90a Fortran program that tests the core through its C interface
python/numerics.pythe ctypes wrapper: four functions, a load at import, an exception for a status code
python/service.pythe HTTP service: /health and /stats on http.server
python/test_numerics.pynine unittest cases for the wrapper
python/test_service.pyfour unittest cases that start the service on a free port
build.shthe one compile command that makes libstats.so
test_core.shbuilds the library, builds the Fortran test, runs it
run-tests.shboth sides: the Fortran test, then python3 -m unittest

The two commands

A reader who copies the files into one directory needs two commands, and both were run here to produce the transcripts on the next pages:

./run-tests.sh
python3 python/service.py 8000

... 6 checks, 0 failures
... Ran 13 tests ... OK
listening on http://127.0.0.1:8000

The first builds the library, builds the Fortran test, runs both suites and returns 0 when everything passed. The second starts the service and prints the address it is listening on. The next pages take the two sides in turn: the core, the wrapper, the service, the tests and the build and the lints.