Binary data handling in Python using dictionaries
Project description
pycstruct
pycstruct is a python library for converting binary data to and from ordinary python dictionaries.
Data is defined similar to what is done in C language structs.
Typical usage of this library is read/write binary files or binary data transmitted over a network.
It supports all traditional data types (integer, unsigned integer, boolean and float) between 1 to 8 bytes large, arrays (lists), strings (UTF-8), bitfields and enums.
Structs can be embedded inside other structs.
Individual elements can be stored / read in any byte order.
Checkout the full documentation here.
Installation
Simply install the package using pip:
python3 -m pip install pycstruct
Example
Following C has a structure (person) with a set of elements that are written to a binary file.
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#pragma pack(1) // To secure no padding is added in struct
struct person
{
char name[50];
unsigned int age;
float height;
bool is_male;
unsigned int nbr_of_children;
unsigned int child_ages[10];
};
void main(void) {
struct person p;
memset(&p, 0, sizeof(struct person));
strcpy(p.name, "Foo Bar");
p.age = 42;
p.height = 1.75; // m
p.is_male = true;
p.nbr_of_children = 2;
p.child_ages[0] = 7;
p.child_ages[1] = 9;
FILE *f = fopen("simple_example.dat", "w");
fwrite(&p, sizeof(struct person), 1, f);
fclose(f);
}
To read the binary file using pycstruct following code required.
import pycstruct
person = pycstruct.StructDef()
person.add('utf-8', 'name', length=50)
person.add('uint32', 'age')
person.add('float32','height')
person.add('bool8', 'is_male')
person.add('uint32', 'nbr_of_children')
person.add('uint32', 'child_ages', length=10)
f = open('simple_example.dat','rb')
inbytes = f.read()
result = person.deserialize(inbytes)
f.close()
print(str(result))
The produced output will be::
{'name': 'Foo Bar', 'is_male': True, 'nbr_of_children': 2,
'age': 42, 'child_ages': [7, 9, 0, 0, 0, 0, 0, 0, 0, 0],
'height': 1.75}
To write a binary file from python using the same structure using pycstruct following code is required.
import pycstruct
person = pycstruct.StructDef()
person.add('utf-8', 'name', length=50)
person.add('uint32', 'age')
person.add('float32','height')
person.add('bool8', 'is_male')
person.add('uint32', 'nbr_of_children')
person.add('uint32', 'child_ages', length=10)
mrGreen = {}
mrGreen['name'] = "MR Green"
mrGreen['age'] = 50
mrGreen['height'] = 1.93
mrGreen['is_male'] = True
mrGreen['nbr_of_children'] = 3
mrGreen['child_ages'] = [13,24,12]
buffer = person.serialize(mrGreen)
f = open('simple_example_mr_green.dat','wb')
f.write(buffer)
f.close()
Full documentation
Checkout the full documentation here.
Author and license
This application is written by Joel Midstjärna and is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for pycstruct-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7263cc7c308805fd125695f0bd528077c69c0fcad57f2f54e8f514e1573f42cd |
|
MD5 | 0962077e8ffc83ee68feabb5ecbd05ae |
|
BLAKE2b-256 | c8fbfbe5a5981bfb67d0aadd01a04c109f06d428d39a3362054271b8ff4729e1 |