class documentation

NpzFile(fid)

A dictionary-like object with lazy-loading of files in the zipped archive provided on construction.

NpzFile is used to load files in the NumPy .npz data archive format. It assumes that files in the archive have a .npy extension, other files are ignored.

The arrays and file strings are lazily loaded on either getitem access using obj['key'] or attribute lookup using obj.f.key. A list of all files (without .npy extensions) can be obtained with obj.files and the ZipFile object itself using obj.zip.

Examples

>>> from tempfile import TemporaryFile
>>> outfile = TemporaryFile()
>>> x = np.arange(10)
>>> y = np.sin(x)
>>> np.savez(outfile, x=x, y=y)
>>> _ = outfile.seek(0)
>>> npz = np.load(outfile)
>>> isinstance(npz, np.lib.npyio.NpzFile)
True
>>> sorted(npz.files)
['x', 'y']
>>> npz['x']  # getitem access
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> npz.f.x  # attribute lookup
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
Parameters
fidThe zipped archive to open. This is either a file-like object or a string containing the path to the archive.
own_fidWhether NpzFile should close the file handle. Requires that fid is a file-like object.
Method __del__ Undocumented
Method __enter__ Undocumented
Method __exit__ Undocumented
Method __getitem__ Undocumented
Method __init__ Undocumented
Method __iter__ Undocumented
Method __len__ Undocumented
Method close Close the file.
Instance Variable allow_pickle Allow loading pickled data. Default: False
Instance Variable f An object on which attribute can be performed as an alternative to getitem access on the NpzFile instance itself.
Instance Variable fid Undocumented
Instance Variable files List of all files in the archive with a .npy extension.
Instance Variable max_header_size Maximum allowed size of the header. Large headers may not be safe to load securely and thus require explicitly passing a larger value. See ast.literal_eval() for details. This option is ignored when allow_pickle...
Instance Variable pickle_kwargs Additional keyword arguments to pass on to pickle.load. These are only useful when loading object arrays saved on Python 2 when using Python 3.
Instance Variable zip The ZipFile object initialized with the zipped archive.
Instance Variable _files Undocumented
def __del__(self): (source)

Undocumented

def __enter__(self): (source)

Undocumented

def __exit__(self, exc_type, exc_value, traceback): (source)

Undocumented

def __getitem__(self, key): (source)

Undocumented

def __init__(self, fid, own_fid=False, allow_pickle=False, pickle_kwargs=None, *, max_header_size=format._MAX_HEADER_SIZE): (source)

Undocumented

def __iter__(self): (source)

Undocumented

def __len__(self): (source)

Undocumented

def close(self): (source)

Close the file.

allow_pickle: bool, optional = (source)

Allow loading pickled data. Default: False

Changed in version 1.16.3: Made default False in response to CVE-2019-6446.
f: BagObj instance = (source)

An object on which attribute can be performed as an alternative to getitem access on the NpzFile instance itself.

fid: file or str = (source)

Undocumented

List of all files in the archive with a .npy extension.

max_header_size: int, optional = (source)

Maximum allowed size of the header. Large headers may not be safe to load securely and thus require explicitly passing a larger value. See ast.literal_eval() for details. This option is ignored when allow_pickle is passed. In that case the file is by definition trusted and the limit is unnecessary.

pickle_kwargs: dict, optional = (source)

Additional keyword arguments to pass on to pickle.load. These are only useful when loading object arrays saved on Python 2 when using Python 3.

zip: ZipFile instance = (source)

The ZipFile object initialized with the zipped archive.

Undocumented