MCPcopy Index your code
hub / github.com/numpy/numpy / open

Method open

numpy/lib/_datasource.py:483–529  ·  view source on GitHub ↗

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

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

Source from the content-addressed store, hash-verified

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

Callers 15

openFunction · 0.95
test_ValidHTTPMethod · 0.95
test_InvalidHTTPMethod · 0.95
test_ValidFileMethod · 0.95
test_ValidGzipFileMethod · 0.95
test_ValidBz2FileMethod · 0.95
scan_fileFunction · 0.45
test_legacy_pickleMethod · 0.45
test_legacy_pickleFunction · 0.45
test_tofile_fromfileMethod · 0.45
__getitem__Method · 0.45
_savezFunction · 0.45

Calls 5

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

Tested by 15

test_ValidHTTPMethod · 0.76
test_InvalidHTTPMethod · 0.76
test_ValidFileMethod · 0.76
test_ValidGzipFileMethod · 0.76
test_ValidBz2FileMethod · 0.76
test_legacy_pickleMethod · 0.36
test_legacy_pickleFunction · 0.36
test_tofile_fromfileMethod · 0.36
test_DataSourceOpenMethod · 0.36
test_load_non_npyMethod · 0.36
test_warning_callsFunction · 0.36
test_input_encodingMethod · 0.36