diff options
author | David Cournapeau <cournape@gmail.com> | 2011-10-10 09:10:28 +0100 |
---|---|---|
committer | David Cournapeau <cournape@gmail.com> | 2011-10-10 09:15:20 +0100 |
commit | 1dc1877493312eb5fe107729a9309e8636968891 (patch) | |
tree | 606c3962db68ff57dcbc8dbbd5c59f38e30d9bb8 /numpy/lib/npyio.py | |
parent | efe04c8205e39d1edfe206df1c865c99bbd3af66 (diff) | |
download | numpy-1dc1877493312eb5fe107729a9309e8636968891.tar.gz |
ENH: add context manager for NpzFile class.
Diffstat (limited to 'numpy/lib/npyio.py')
-rw-r--r-- | numpy/lib/npyio.py | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index 07a8ca429..3e3d00c57 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -195,6 +195,12 @@ class NpzFile(object): else: self.fid = None + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_value, traceback): + self.close() + def close(self): """ Close the file. @@ -306,6 +312,13 @@ def load(file, mmap_mode=None): - If the file is a ``.npz`` file, then a dictionary-like object is returned, containing ``{filename: array}`` key-value pairs, one for each file in the archive. + - If the file is a ``.npz`` file, the returned value supports the context + manager protocol in a similar fashion to the open function:: + + with load('foo.npz') as data: + a = data['a'] + + The underlyling file descriptor is always closed when exiting the with block. Examples -------- |