在Python中,是否可以重载Numpy';当不再引用memmap对象时,是否要删除memmap自身?

在Python中,是否可以重载Numpy';当不再引用memmap对象时,是否要删除memmap自身?,python,numpy,Python,Numpy,我试图在某些数据不适合内存时使用memmap,并利用memmap的能力欺骗代码,使其认为这只是一个数组。为了进一步扩展使用memmap的方法,我想知道是否有可能重载memmap的解引用操作符来删除memmap文件 例如: from tempfile import mkdtemp import os.path as path filename = path.join(mkdtemp(), 'tmpfile.dat') { out = np.memmap(filename, dtype=a.

我试图在某些数据不适合内存时使用memmap,并利用memmap的能力欺骗代码,使其认为这只是一个数组。为了进一步扩展使用memmap的方法,我想知道是否有可能重载memmap的解引用操作符来删除memmap文件

例如:

from tempfile import mkdtemp
import os.path as path
filename = path.join(mkdtemp(), 'tmpfile.dat')
{
    out = np.memmap(filename, dtype=a.dtype, mode='w+', shape=a.shape)
}
# At this point out is out of scope, so the overloaded 
# dereference function would delete tmpfile.dat
这听起来可行吗?有什么我没想到的吗


谢谢大家!

只需在文件被
np.memmap
在关闭对文件描述符的最后一次引用后,系统将删除该文件

python临时文件是这样工作的,可以非常方便地与
with
上下文管理器结构一起使用:

with tempfile.NamedTemporaryFile() as f:
    # file gone now from the filesystem 
    # but f still holds a reference so it still exists and uses space (see /prof<pid>/fd)
    # open it again (will not work on windows)
    x = np.memmap(f.name, dtype=np.float64, mode='w+', shape=(3,4))
# file path is unlinked but exists on disk with the open file reference in x
del x
# now all references are gone and the file is properly deleted
将tempfile.NamedTemporaryFile()作为f:
#文件已从文件系统中删除
#但是f仍然持有一个引用,因此它仍然存在并使用空格(参见/prof/fd)
#再次打开它(在windows上不起作用)
x=np.memmap(f.name,dtype=np.float64,mode='w+',shape=(3,4))
#文件路径未链接,但存在于x中具有打开文件引用的磁盘上
德尔克斯
#现在所有引用都消失了,文件也被正确删除了

如果我们不想与一起使用,而只想让某个类为我们处理它,则需要一个案例:

class tempmap(np.memmap):
    """
    Extension of numpy memmap to automatically map to a file stored in temporary directory.
    Usefull as a fast storage option when numpy arrays become large and we just want to do some quick experimental stuff.
    """
    def __new__(subtype, dtype=np.uint8, mode='w+', offset=0,
                shape=None, order='C'):
        ntf = tempfile.NamedTemporaryFile()
        self = np.memmap.__new__(subtype, ntf, dtype, mode, offset, shape, order)
        self.temp_file_obj = ntf
        return self

    def __del__(self):
        if hasattr(self,'temp_file_obj') and self.temp_file_obj is not None:
            self.temp_file_obj.close()
            del self.temp_file_obj

def np_as_tmp_map(nparray):
    tmpmap = tempmap(dtype=nparray.dtype, mode='w+', shape=nparray.shape)
    tmpmap[...] = nparray
    return tmpmap


def test_memmap():
    """Test, that deleting a temp memmap also deletes the file."""
    x = np_as_tmp_map(np.zeros(10, 10), np.float))
    name = copy(x.temp_file_obj.name)
    del x
    x = None
    assert not os.path.isfile(name)

“解引用运算符”?
{
}
>
从uuu future uuuu导入大括号
…您确定不希望将
上下文管理器一起使用吗
with
是Python如何进行基于范围的资源管理;这是最接近C++的RAII的东西。这是不正确的。甚至在调用
del x
之前,
os.path.exists(x.filename)
就是
False
x
仅引用了
f.name
,一个字符串,而不是
f
对象本身。路径不再存在,但在删除对该文件的最后一次引用之前,不会删除该文件
x
保存对文件的引用,这就是np.memmap的工作方式,它打开
f.name
指向的内容,因此需要删除它。它与被上下文管理器删除的python对象
f
无关,这是OS文件系统的一个属性。啊,这很有意义。你能把文字改清楚吗?可能
#f reference现在已消失,但x仍有一个引用
->
#文件路径未链接,但存在于具有打开的文件引用的磁盘上