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.
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
| file | what it is |
|---|---|
| core/stats.f90 | the numerical core: mean, standard deviation, median and quantile, all BIND(C) |
| core/test_core.f90 | a Fortran program that tests the core through its C interface |
| python/numerics.py | the ctypes wrapper: four functions, a load at import, an exception for a status code |
| python/service.py | the HTTP service: /health and /stats on http.server |
| python/test_numerics.py | nine unittest cases for the wrapper |
| python/test_service.py | four unittest cases that start the service on a free port |
| build.sh | the one compile command that makes libstats.so |
| test_core.sh | builds the library, builds the Fortran test, runs it |
| run-tests.sh | both 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.