子类化tables.Group类

时间:2013-12-02 16:42:11

标签: pytables

是否允许子类tables.Group?

以下代码可以正常使用

In [1]: import tables

In [2]: class Friendly_group(tables.Group):
   ...:     def __repr__(self):
   ...:         return 'hello!'
   ...:     

In [3]: hf = tables.open_file('data', mode='w')

In [4]: fgroup = Friendly_group(hf.root, 'fgroup', new=True)

In [5]: hf
Out[5]: 
File(filename=data, title='', mode='w', root_uep='/', filters=Filters(complevel=0, shuffle=False, fletcher32=False))
/ (RootGroup) ''
/fgroup (Friendly_group) ''


In [6]: hf.root.fgroup
Out[6]: hello!

但回过头来看,那群人不再友好了

In [7]: hf.close()

In [8]: hf = tables.open_file('data', mode='r')

In [9]: hf
Out[9]: 
File(filename=data, title='', mode='r', root_uep='/', filters=Filters(complevel=0, shuffle=False, fletcher32=False))
/ (RootGroup) ''
/fgroup (Group) ''


In [10]: hf.root.fgroup
Out[10]: 
/fgroup (Group) ''
  children := []

SO检查员强迫我在这篇文章中添加一些细节,但我真的不知道如何才能提高我的问题的清晰度,所以请原谅我这篇虚假的文章。

1 个答案:

答案 0 :(得分:1)

是的,这是可能的。丢失所需的缺失部分是覆盖_c_classid类属性。您可能希望查看tables/group.py中存在的其他组子类。例如,采用TransactionGroupG(剥离了一些向后兼容性功能),

class TransactionGroupG(NotLoggedMixin, Group):
    _c_classid = 'TRANSGROUP'

    def _g_width_warning(self):
        warnings.warn("""\
the number of transactions is exceeding the recommended maximum (%d);\
be ready to see PyTables asking for *lots* of memory and possibly slow I/O"""
                      % (self._v_max_group_width,), PerformanceWarning)

这是相当小的。

相关问题