'Cython read large binary file using class definition

My code tries to read a binary file using a Structure class:

import os
import mmap
from ctypes import Structure, c_uint

class StructFile(Structure):
    _pack_ = 1
    _fields_ = [('Xmin', c_uint),('Ymax', c_uint)]

fd = os.open(filePath, os.O_RDWR | os.O_BINARY)
mmap_file = mmap.mmap(fd, length=ln, access=mmap.ACCESS_WRITE, offset=0)
d_arrayFile = StructFile * numItems
data = d_arrayFile.from_buffer(mmap_file)
for i in data:
    print i.Xmin, i.Ymax

It works correctly, but I would like to speed it up using a cython class. The problem is when I use cdef class StructFile(Structure): the compilation says: "'Structure' is not a type name".

How could I do in order to cython recognise 'Structure' as a type name like 'int', 'object', etc.? Or any other solution to optimise the code (not using struct.unpack).

Thanks!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source