Studying the source of the library, I realized that the required behavior can be achieved by changing the default values of a small helper function called open in the numat datasource module. As suggested above, this is possible without changing the source of the library. Here is the code I came with:
import numpy
from numpy.lib._datasource import DataSource
def openm(path, mode='r', destpath=None):
ds = DataSource(destpath)
return ds.open(path, mode)
numpy.lib._datasource.open=openm
which must be enabled before calling genfromtxt or functions derived from it.
But my research also showed that these functions are quite slow and that when Windows caching is disabled, a warning is issued - this is not related to the override above, it seems to be related to how the mktemp function is implemented in Windows. In addition, the cached file and its associated temporary directories are not deleted.
, -, , , , (, , ), . datasource, , , , . datasource genfromtxt.
, genfromtxt Windows Linux, .
, numpy cache genfromtxt
npyio.py, .
genfromtxt
def genfromtxt(fname, dtype=float, comments='#', delimiter=None,
skiprows=0, skip_header=0, skip_footer=0, converters=None,
missing='', missing_values=None, filling_values=None,
usecols=None, names=None,
excludelist=None, deletechars=None, replace_space='_',
autostrip=False, case_sensitive=True, defaultfmt="f%i",
unpack=None, usemask=False, loose=True, invalid_raise=True):
to ( )
def genfromtxt(fname, dtype=float, comments='#', delimiter=None,
skiprows=0, skip_header=0, skip_footer=0, converters=None,
missing='', missing_values=None, filling_values=None,
usecols=None, names=None,
excludelist=None, deletechars=None, replace_space='_',
autostrip=False, case_sensitive=True, defaultfmt="f%i",
unpack=None, usemask=False, loose=True, invalid_raise=True,cache=True):
, /url,
if isinstance(fname, basestring):
fhd = iter(np.lib._datasource.open(fname, 'rbU')
own_fhd = True
if isinstance(fname, basestring):
ds=DataSource('.' if cache==True else None)
fhd = iter(ds.open(fname, 'rbU'if cache==True else 'rbUD'))
own_fhd = True
'D' - Windows ( Linux), , .
Wrapper
def mgenfromtxt(fname,cache=True,**karg):
if cache==False and isinstance(fname, basestring) and numpy.DataSource()._isurl(fname):
ds=numpy.DataSource(None)
fhd = iter(ds.open(fname,'rbUD'))
l1=numpy.genfromtxt(fhd,**karg)
fhd.close()
del ds
return l1
else:
return(numpy.genfromtxt(fname,**karg))