h5py文件方法覆盖

时间:2017-08-08 03:35:54

标签: python numpy h5py

我正在尝试创建一个h5py文件的子类,它在我的脚本和交互模式下很有用,所以我希望我的类可以选择提供文件名或调用弹出文件选择GUI。我相信我需要使用自己的选项覆盖h5py.File。 init (),但不能按预期工作...

例如,这有效......

import h5py
x = h5py.File('myfile.h5')
>>> x
<HDF5 file "myfile.h5" (mode r+)>

但这不是......

import h5py

class MyH5(h5py.File):

    def __init__(self, filename='', initialdir=None, gui=False):
        if gui == False:
            self = h5py.File(filename, 'r')
        elif gui == True:
            opts = {}
            opts['filetypes'] = [('HDF-Time History Files','.H5'),('all files','.*')]
            opts['title'] = 'Choose an HDF file...'
            opts['initialdir'] = initialdir
            filename = askopenfilename(**opts)  # show an "Open" dialog box and return the path to the selected file
            self = h5py.File(filename, 'r')

x = MyH5('myfile.h5')

>>> x
    ---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
    670                 type_pprinters=self.type_printers,
    671                 deferred_pprinters=self.deferred_printers)
--> 672             printer.pretty(obj)
    673             printer.flush()
    674             return stream.getvalue()

C:\ProgramData\Anaconda3\lib\site-packages\IPython\lib\pretty.py in pretty(self, obj)
    381                             if callable(meth):
    382                                 return meth(obj, self, cycle)
--> 383             return _default_pprint(obj, self, cycle)
    384         finally:
    385             self.end_group()

C:\ProgramData\Anaconda3\lib\site-packages\IPython\lib\pretty.py in _default_pprint(obj, p, cycle)
    501     if _safe_getattr(klass, '__repr__', None) not in _baseclass_reprs:
    502         # A user-provided repr. Find newlines and replace them with p.break_()
--> 503         _repr_pprint(obj, p, cycle)
    504         return
    505     p.begin_group(1, '<')

C:\ProgramData\Anaconda3\lib\site-packages\IPython\lib\pretty.py in _repr_pprint(obj, p, cycle)
    699     """A pprint that just redirects to the normal repr function."""
    700     # Find newlines and replace them with p.break_()
--> 701     output = repr(obj)
    702     for idx,output_line in enumerate(output.splitlines()):
    703         if idx:

h5py\_objects.pyx in h5py._objects.with_phil.wrapper (C:\Minonda\conda-bld\h5py_1482647201869\work\h5py\_objects.c:2866)()

h5py\_objects.pyx in h5py._objects.with_phil.wrapper (C:\Minonda\conda-bld\h5py_1482647201869\work\h5py\_objects.c:2824)()

C:\ProgramData\Anaconda3\lib\site-packages\h5py\_hl\files.py in __repr__(self)
    320     @with_phil
    321     def __repr__(self):
--> 322         if not self.id:
    323             r = six.u('<Closed HDF5 file>')
    324         else:

h5py\_objects.pyx in h5py._objects.with_phil.wrapper (C:\Minonda\conda-bld\h5py_1482647201869\work\h5py\_objects.c:2866)()

h5py\_objects.pyx in h5py._objects.with_phil.wrapper (C:\Minonda\conda-bld\h5py_1482647201869\work\h5py\_objects.c:2824)()

C:\ProgramData\Anaconda3\lib\site-packages\h5py\_hl\base.py in id(self)
    234     def id(self):
    235         """ Low-level identifier appropriate for this object """
--> 236         return self._id
    237 
    238     @property

AttributeError: 'MyH5' object has no attribute '_id'

我的目的是使用h5py File类的属性/方法创建一个对象,但也在这个子类中添加我自己的对象,但父进程的 init 可能没有运行?

白天我是工程师,对于定义课程很新,所以要温柔......

0 个答案:

没有答案