【问题标题】:How can the shape of a pytables table column be defined by a variable?pytables 表格列的形状如何由变量定义?
【发布时间】:2013-12-23 22:29:43
【问题描述】:

我正在尝试创建一个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 = ... 会出现同样的错误。

this SO question 中,问题被列为因为IsDescription 是一个元类,但没有给出元类禁止这种行为的原因,也没有给出解决方法。

是否有解决方法允许 PyTables 中的表具有未知(但固定)大小?

最后,为了避开XY problem cmets,我想我可以使用数组或可扩展数组来做我想做的事情(将解决方案数据输出到磁盘)。我仍然很想看到上述问题的答案。

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

【问题讨论】:

    标签: python metaclass pytables


    【解决方案1】:

    使用字典来定义表而不是子类化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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-11
      • 1970-01-01
      • 2019-09-17
      • 1970-01-01
      • 2014-11-16
      • 2023-02-08
      • 1970-01-01
      相关资源
      最近更新 更多