8000 Allow pathlib.Path arguments, from issue #6418 by r0fls · Pull Request #6655 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

Allow pathlib.Path arguments, from issue #6418 #6655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions numpy/lib/npyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,16 @@ def load(file, mmap_mode=None, allow_pickle=True, fix_imports=True,

"""
import gzip
try:
from pathlib import Path
supports_pathlib = True
except:
supports_pathlib = False

if supports_pathlib:
if isinstance(file, Path):
file = file.__str__()


own_fid = False
if isinstance(file, basestring):
Expand Down Expand Up @@ -505,10 +515,11 @@ def savez(file, *args, **kwds):

Parameters
----------
file : str or file
Either the file name (string) or an open file (file-like object)
where the data will be saved. If file is a string, the ``.npz``
extension will be appended to the file name if it is not already there.
file : str, file, or Path (from pathlib library)
Either the file name (string), an open file (file-like object)
where the data will be saved, or a Path from the pathlib library.
If file is a string, the ``.npz`` extension will be appended to
the file name if it is not already there.
args : Arguments, optional
Arrays to save to the file. Since it is not possible for Python to
know the names of the arrays outside `savez`, the arrays will be saved
Expand Down Expand Up @@ -584,7 +595,7 @@ def savez_compressed(file, *args, **kwds):

Parameters
----------
file : str
file : str, file, or Path (from pathlib library)
File name of ``.npz`` file.
args : Arguments
Function arguments.
Expand All @@ -606,10 +617,18 @@ def _savez(file, args, kwds, compress, allow_pickle=True, pickle_kwargs=None):
import zipfile
# Import deferred for startup time improvement
import tempfile
try:
from pathlib import Path
supports_pathlib = True
except:
supports_pathlib = False

if isinstance(file, basestring):
if not file.endswith('.npz'):
file = file + '.npz'
elif supports_pathlib:
if isinstance(file, Path):
file = file.__str__()

namedict = kwds
for i, val in enumerate(args):
Expand Down
13 changes: 13 additions & 0 deletions numpy/lib/tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,19 @@ def test_compressed_roundtrip():
arr1 = np.load(npz_file)['arr']
assert_array_equal(arr, arr1)

def test_compressed_roundtrip():
arr = np.random.rand(200, 200)
npz_file = os.path.join(tempdir, 'compressed.npz')
try:
import pathlib
except:
return
npz_file = pathlib.Path(npz_file)
np.savez_compressed(npz_file, arr=arr)
arr1 = np.load(npz_file)['arr']
assert_array_equal(arr, arr1)



def test_python2_python3_interoperability():
if sys.version_info[0] >= 3:
Expand Down
0