Question

I am using PyTables to store Python data in an HDF5 file, and it requires a helper class to create a table. Here is an example:

class PacketData(pt.IsDescription):
    data = pt.UInt8Col(shape=(128,))


# later code does this:
self.tdata = self.hfile.createTable(self.g, 'packetdata', PacketData,
                            filters=filters)

Is there a way to generalize the 128 here using a metaclass?

Was it helpful?

Solution

I don't think you need a metaclass here. In fact, since the metaclass is fixed at the point where the class is defined, I don't think it provides you an opportunity to parametrize the shape.

You could use a class factory instead:

def packetdata(n):
    class PacketData(pt.IsDescription):
        data = pt.UInt8Col(shape=(n,))
    return PacketData

self.tdata = self.hfile.createTable(self.g, 'packetdata', packetdata(128),
                            filters=filters)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top