MCPcopy Create free account
hub / github.com/numpy/numpy / open

Method open

numpy/lib/_datasource.py:487–533  ·  view source on GitHub ↗

Open and return file-like object. If `path` is an URL, it will be downloaded, stored in the `DataSource` directory and opened from there. Parameters ---------- path : str Local file path or URL to open. mode : {'r', 'w', 'a'}, op

(self, path, mode='r', encoding=None, newline=None)

Source from the content-addressed store, hash-verified

485 return False
486
487 def open(self, path, mode='r', encoding=None, newline=None):
488 """
489 Open and return file-like object.
490
491 If `path` is an URL, it will be downloaded, stored in the
492 `DataSource` directory and opened from there.
493
494 Parameters
495 ----------
496 path : str
497 Local file path or URL to open.
498 mode : {'r', 'w', 'a'}, optional
499 Mode to open `path`. Mode 'r' for reading, 'w' for writing,
500 'a' to append. Available modes depend on the type of object
501 specified by `path`. Default is 'r'.
502 encoding : {None, str}, optional
503 Open text file with given encoding. The default encoding will be
504 what `io.open` uses.
505 newline : {None, str}, optional
506 Newline to use when reading text file.
507
508 Returns
509 -------
510 out : file object
511 File object.
512
513 """
514
515 # TODO: There is no support for opening a file for writing which
516 # doesn't exist yet (creating a file). Should there be?
517
518 # TODO: Add a ``subdir`` parameter for specifying the subdirectory
519 # used to store URLs in self._destpath.
520
521 if self._isurl(path) and self._iswritemode(mode):
522 raise ValueError("URLs are not writeable")
523
524 # NOTE: _findfile will fail on a new file opened for writing.
525 found = self._findfile(path)
526 if found:
527 _fname, ext = self._splitzipext(found)
528 if ext == 'bz2':
529 mode.replace("+", "")
530 return _file_openers[ext](found, mode=mode,
531 encoding=encoding, newline=newline)
532 else:
533 raise FileNotFoundError(f"{path} not found.")
534
535
536class Repository (DataSource):

Callers 15

openFunction · 0.95
mainFunction · 0.45
unpack_targzFunction · 0.45
test_tofile_fromfileMethod · 0.45
mainFunction · 0.45
__getitem__Method · 0.45
_savezFunction · 0.45
_readFunction · 0.45
savetxtFunction · 0.45

Calls 5

_isurlMethod · 0.95
_iswritemodeMethod · 0.95
_findfileMethod · 0.95
_splitzipextMethod · 0.95
replaceMethod · 0.80

Tested by 15

test_tofile_fromfileMethod · 0.36
mainFunction · 0.36
test_ValidHTTPMethod · 0.36
test_InvalidHTTPMethod · 0.36
test_ValidFileMethod · 0.36
test_ValidGzipFileMethod · 0.36
test_ValidBz2FileMethod · 0.36
test_DataSourceOpenMethod · 0.36