struct

Reading a C-struct via sockets into python

Reading a C-struct via sockets into python Question: On an embedded device running a C application, I have defined this struct: struct TestStruct { float first; int second; char third; }; On request, I send this struct via sockets: else if(strcmp(str, “Get Stru”) == 0) { TestStruct testStruct; testStruct.first = 1.2; testStruct.second = 42; testStruct.third …

Total answers: 1

Python struct.error: integer out of range for 'q' format code

Python struct.error: integer out of range for 'q' format code Question: I’m having a problem, I want to cast a binary number into a double precission number. After some searchs, I found some way, but I’m still having a problem, I can cast “little” numbers but not the big ones (double precission), here’s my example …

Total answers: 2

Python mutable NamedTuple

Python mutable NamedTuple Question: I am looking for a struct like data structure I can create multiple instances from and have some type hinting without being immutable. So I have something like this: class ConnectionConfig(NamedTuple): name: str url: str port: int user: str = “” pwd: str = “” client: Any = None But I …

Total answers: 6

How to resolve complex struct from a JPEG file using Python?

How to resolve complex struct from a JPEG file using Python? Question: The tail of the JPEG file contains a complex structure, such as following: FFD8……………………… …………………………. …………FFD9 1C01 0000 …. …………………………. struct definations in C file are: typedef struct { short wYear; short wMonth; short wDayOfWeek; short wDay; short wHour; short wMinute; short wSecond; …

Total answers: 1

Fastest way to pack a list of floats into bytes in python

Fastest way to pack a list of floats into bytes in python Question: I have a list of say 100k floats and I want to convert it into a bytes buffer. buf = bytes() for val in floatList: buf += struct.pack(‘f’, val) return buf This is quite slow. How can I make it faster using …

Total answers: 9

Getting type/size of `time_t` using ctypes

Getting type/size of `time_t` using ctypes Question: I’m accessing a C struct which contains some time_t fields using python ctypes module. Given its non completely portable nature, I cannot define these fields statically as of c_int or c_long type. How can I define them to make my code portable? Example C struct definition: #import <sys/types.h> …

Total answers: 2

Is there a data type in Python similar to structs in C++?

Is there a data type in Python similar to structs in C++? Question: Is there a data type in Python similar to structs in C++? I like the struct feature myStruct.someName. I know that classes have this, but I don’t want to write a class everytime I need a “container” for some data. Asked By: …

Total answers: 8

packing and unpacking variable length array/string using the struct module in python

packing and unpacking variable length array/string using the struct module in python Question: I am trying to get a grip around the packing and unpacking of binary data in Python 3. Its actually not that hard to understand, except one problem: what if I have a variable length textstring and want to pack and unpack …

Total answers: 7

Using Python class as a data container

Using Python class as a data container Question: Sometimes it makes sense to cluster related data together. I tend to do so with a dict, e.g., group = dict(a=1, b=2, c=3) print(group[‘a’]) One of my colleagues prefers to create a class class groupClass: def __init__(self, a, b, c): self.a = a self.b = b self.c …

Total answers: 12

struct.error: unpack requires a string argument of length 4

struct.error: unpack requires a string argument of length 4 Question: Python says I need 4 bytes for a format code of “BH”: struct.error: unpack requires a string argument of length 4 Here is the code, I am putting in 3 bytes as I think is needed: major, minor = struct.unpack(“BH”, self.fp.read(3)) “B” Unsigned char (1 …

Total answers: 2