문제

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?

도움이 되었습니까?

해결책

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)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top