{ } Raw JSON

bundles / numpy 2.4.4 / numpy / lib / npyio / DataSource

class

numpy.lib.npyio:DataSource

source: /numpy/lib/npyio.py

Signature

class DataSource(destpath = .)

Summary

A generic data source file (file, http, ftp, ...).

Extended Summary

DataSources can be local files or remote files/URLs. The files may also be compressed or uncompressed. DataSource hides some of the low-level details of downloading the file, allowing you to simply pass in a valid file path (or URL) and obtain a file object.

Parameters

destpath : str or None, optional

Path to the directory where the source file gets downloaded to for use. If destpath is None, a temporary directory will be created. The default path is the current directory.

Notes

URLs require a scheme string (http://) to be used, without it they will fail

>>> repos = np.lib.npyio.DataSource()
>>> repos.exists('www.google.com/index.html')
False
>>> repos.exists('http://www.google.com/index.html')
True

Temporary directories are deleted when the DataSource is deleted.

Examples

::
ds = np.lib.npyio.DataSource('/home/guido')
urlname = 'http://www.google.com/'
✓
gfile = ds.open('http://www.google.com/')
⚠
Traceback (most recent call last):
  File "<doctest numpy.lib.npyio:DataSource[0]>", line 1, in <module>
    gfile = ds.open('http://www.google.com/')
  File "/opt/hostedtoolcache/Python/3.14.7/x64/lib/python3.14/site-packages/numpy/lib/_datasource.py", line 521, in open
    found = self._findfile(path)
  File "/opt/hostedtoolcache/Python/3.14.7/x64/lib/python3.14/site-packages/numpy/lib/_datasource.py", line 367, in _findfile
    name = self._cache(name)
  File "/opt/hostedtoolcache/Python/3.14.7/x64/lib/python3.14/site-packages/numpy/lib/_datasource.py", line 329, in _cache
    os.makedirs(os.path.dirname(upath))
    ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen os>", line 226, in makedirs
  File "<frozen os>", line 236, in makedirs
PermissionError: [Errno 13] Permission denied: '/home/guido'
ds.abspath(urlname)
✗
'/home/guido/www.google.com/'
ds = np.lib.npyio.DataSource(None)  # use with temporary file
✓
ds.open('/home/guido/foobar.txt')
⚠
Traceback (most recent call last):
  File "<doctest numpy.lib.npyio:DataSource[0]>", line 1, in <module>
    ds.open('/home/guido/foobar.txt')
    ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.14.7/x64/lib/python3.14/site-packages/numpy/lib/_datasource.py", line 529, in open
    raise FileNotFoundError(f"{path} not found.")
FileNotFoundError: /home/guido/foobar.txt not found.
ds.abspath('/home/guido/foobar.txt')
✓
'/tmp/tmp66gbtpeh/home/guido/foobar.txt'

Aliases

  • numpy.lib._datasource.DataSource