Today, I want to share a small Python 3 script that I wrote in order to ease the exchange of binary data between Python 3 and C code. For me, sharing data between Python and C is a reoccurring issue, since, usually, I find it more convenient to use Python in order to convert data between different formats and representations. C, on the other hand, usually gives me the raw speed Python cannot offer, but that is needed to do calculations with bigger data sets.
The basic idea is that you write or edit the description of the data structures you want to share between Python and C, and then let the generator script create the code you need in order to load and save the structures to files in binary format for both languages. The structures are realized as
structs in C and as dictionaries in Python. For C output, the script generates convenience routines for easy allocation, freeing and comparison of the described data structures; for Python, the script generates a routine that forces the correct data type on the dictionaries representing the structure.
git clone https://github.com/alb-i/datastructures.py.git
python3 datastructures.py --help