Вопрос

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