fepegar
(Fernando Pérez-García)
February 9, 2021, 5:57pm
11
It seems to be the case that these libraries just tolerate pathlib
then, as all they do is covert to string as soon as they can. I’m not sure it makes sense to modify current implementations to be based on pathlib
. But if I had to, for example, write a library to read DICOM, I think it would be very handy. But that’s a different issue.
Here are the aforementioned libraries tolerating a path-like object:
# Type check here. We don't want to parse only to fail because of an
# invalid value of an integer skiprows.
if isinstance(skiprows, numbers.Integral) and skiprows < 0:
raise ValueError(
"cannot skip rows starting from the end of the "
"data (you passed a negative value)"
)
validate_header_arg(header)
io = stringify_path(io)
return _parse(
flavor=flavor,
io=io,
match=match,
header=header,
index_col=index_col,
skiprows=skiprows,
parse_dates=parse_dates,
thousands=thousands,
Py_XDECREF(type);
return NULL;
}
array_function_result = array_implement_c_array_function_creation(
"fromfile", args, keywds);
if (array_function_result != Py_NotImplemented) {
return array_function_result;
}
file = NpyPath_PathlikeToFspath(file);
if (file == NULL) {
return NULL;
}
if (offset != 0 && strcmp(sep, "") != 0) {
PyErr_SetString(PyExc_TypeError, "'offset' argument only permitted for binary files");
Py_XDECREF(type);
Py_DECREF(file);
return NULL;
}
Metadata in the image file. The supported keys depend on the output
format, see the documentation of the respective backends for more
information.
pil_kwargs : dict, optional
Keyword arguments passed to `PIL.Image.Image.save`. If the 'pnginfo'
key is present, it completely overrides *metadata*, including the
default 'Software' key.
"""
from matplotlib.figure import Figure
if isinstance(fname, os.PathLike):
fname = os.fspath(fname)
if format is None:
format = (Path(fname).suffix[1:] if isinstance(fname, str)
else mpl.rcParams["savefig.format"]).lower()
if format in ["pdf", "ps", "eps", "svg"]:
# Vector formats that are not handled by PIL.
if pil_kwargs is not None:
raise ValueError(
f"Cannot use 'pil_kwargs' when saving to {format}")
fig = Figure(dpi=dpi, frameon=False)
fig.figimage(arr, cmap=cmap, vmin=vmin, vmax=vmax, origin=origin,
:exception OSError: If the file could not be written. The file
may have been created, and may contain partial data.
"""
filename = ""
open_fp = False
if isPath(fp):
filename = fp
open_fp = True
elif isinstance(fp, Path):
filename = str(fp)
open_fp = True
if not filename and hasattr(fp, "name") and isPath(fp.name):
# only set the name for metadata purposes
filename = fp.name
# may mutate self!
self._ensure_mutable()
save_all = params.pop("save_all", False)
self.encoderinfo = params
I agree, I think modifying SetFileName
would be the change we are after.
1 Like