如何用变量定义pytables表列的形状? [英] How can the shape of a pytables table column be defined by a variable?

查看:96
本文介绍了如何用变量定义pytables表列的形状?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个IsDescription子类,以便可以定义我试图创建的表的结构.子类**的属性之一需要给定一定的长度,该长度直到运行时才是未知的(取决于所解析的文件),但在运行时是固定的.

示例代码:

import tables
class MyClass(tables.IsDescription):
    def __init__(self, param):
        var1 = tables.Float64Col(shape=(param))

MyClass1 = MyClass(12)

哪个返回:TypeError: object.__new__() takes no parameters.使用self.var1 = ...会给出相同的错误.

此SO问题中,该问题已列出因为IsDescription是一个元类,但是没有给出为什么元类会禁止这种行为的原因,也没有给出解决方法.

是否存在一种变通方法,以允许PyTables中的表具有未知的(但固定的)大小?

最后,要避免出现 XY问题的评论,我想我可以可能使用数组或可扩展数组来执行我正在尝试做的事情(将解决方案数据输出到磁盘).我仍然很好奇看到上述问题的答案.

**它们在PyTables文档中被称为属性,但是编写>>> subclass.attrib会返回AttributeError: type object 'subclass' has no attribute 'attrib',所以我不知道这是否是正确的词

解决方案

使用字典来定义表,而不是对IsDescription进行子类化.

import tables
import numpy as np
param = 10
with tables.open_file('save.hdf','w') as saveFile:
    tabledef = {'var1':tables.Float64Col(shape=(param))}
    table = saveFile.create_table(saveFile.root,'test',tabledef)
    tablerow = table.row
    tablerow['var1'] = np.array([1,2,3,4,5,6,7,8,9,0])
    tablerow.append()
    table.flush()
with tables.open_file('save.hdf','r') as sv:
    sv.root.test.read()

I'm trying to create an IsDescription subclass, so that I can define the structure of a table I'm trying to create. One of the attributes of the subclass** needs to be shaped given a certain length that is unknown until runtime (it depends on a file being parsed), but is fixed at runtime.

Sample code:

import tables
class MyClass(tables.IsDescription):
    def __init__(self, param):
        var1 = tables.Float64Col(shape=(param))

MyClass1 = MyClass(12)

Which returns: TypeError: object.__new__() takes no parameters. Using self.var1 = ... gives the same error.

In this SO question, the problem is listed as being because IsDescription is a metaclass, but no reason is given why a metaclass would prohibit this behavior, and no workaround is given.

Is there a workaround to allow a table in PyTables to have unknown (but fixed) size?

Finally, to stave off XY problem comments, I think I could probably use an array or expandable array to do what I'm trying to do (which is output solution data to disk). I'm still curious to see answers to the questions above.

**They're called attributes in the PyTables docs, but writing >>> subclass.attrib returns AttributeError: type object 'subclass' has no attribute 'attrib', so I don't know if that's the right word

解决方案

Use a dictionary to define the table instead of subclassing IsDescription.

import tables
import numpy as np
param = 10
with tables.open_file('save.hdf','w') as saveFile:
    tabledef = {'var1':tables.Float64Col(shape=(param))}
    table = saveFile.create_table(saveFile.root,'test',tabledef)
    tablerow = table.row
    tablerow['var1'] = np.array([1,2,3,4,5,6,7,8,9,0])
    tablerow.append()
    table.flush()
with tables.open_file('save.hdf','r') as sv:
    sv.root.test.read()

这篇关于如何用变量定义pytables表列的形状?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆