Skip to content

Commit 34bdcdc

Browse files
committed
Small cleanups.
1 parent 4cab2ec commit 34bdcdc

File tree

3 files changed

+20
-45
lines changed

3 files changed

+20
-45
lines changed

lib/matplotlib/backends/backend_pgf.py

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
_Backend, FigureCanvasBase, FigureManagerBase, GraphicsContextBase,
1818
RendererBase)
1919
from matplotlib.backends.backend_mixed import MixedModeRenderer
20-
from matplotlib.cbook import is_writable_file_like
2120
from matplotlib.path import Path
2221
from matplotlib.figure import Figure
2322
from matplotlib._pylab_helpers import Gcf
@@ -379,8 +378,8 @@ def __init__(self, figure, fh, dummy=False):
379378
Matplotlib figure to initialize height, width and dpi from.
380379
fh : file-like
381380
File handle for the output of the drawing commands.
382-
383381
"""
382+
384383
RendererBase.__init__(self)
385384
self.dpi = figure.dpi
386385
self.fh = fh
@@ -836,16 +835,10 @@ def print_pgf(self, fname_or_fh, *args, **kwargs):
836835
if kwargs.get("dryrun", False):
837836
self._print_pgf_to_fh(None, *args, **kwargs)
838837
return
839-
840-
# figure out where the pgf is to be written to
841-
if isinstance(fname_or_fh, str):
842-
with open(fname_or_fh, "w", encoding="utf-8") as fh:
843-
self._print_pgf_to_fh(fh, *args, **kwargs)
844-
elif is_writable_file_like(fname_or_fh):
845-
fh = codecs.getwriter("utf-8")(fname_or_fh)
846-
self._print_pgf_to_fh(fh, *args, **kwargs)
847-
else:
848-
raise ValueError("filename must be a path")
838+
with cbook.open_file_cm(fname_or_fh, "w", encoding="utf-8") as file:
839+
if not cbook.file_requires_unicode(file):
840+
file = codecs.getwriter("utf-8")(file)
841+
self._print_pgf_to_fh(file, *args, **kwargs)
849842

850843
def _print_pdf_to_fh(self, fh, *args, **kwargs):
851844
w, h = self.figure.get_figwidth(), self.figure.get_figheight()
@@ -890,21 +883,12 @@ def _print_pdf_to_fh(self, fh, *args, **kwargs):
890883
TmpDirCleaner.add(tmpdir)
891884

892885
def print_pdf(self, fname_or_fh, *args, **kwargs):
893-
"""
894-
Use LaTeX to compile a Pgf generated figure to PDF.
895-
"""
886+
"""Use LaTeX to compile a Pgf generated figure to PDF."""
896887
if kwargs.get("dryrun", False):
897888
self._print_pgf_to_fh(None, *args, **kwargs)
898889
return
899-
900-
# figure out where the pdf is to be written to
901-
if isinstance(fname_or_fh, str):
902-
with open(fname_or_fh, "wb") as fh:
903-
self._print_pdf_to_fh(fh, *args, **kwargs)
904-
elif is_writable_file_like(fname_or_fh):
905-
self._print_pdf_to_fh(fname_or_fh, *args, **kwargs)
906-
else:
907-
raise ValueError("filename must be a path or a file-like object")
890+
with cbook.open_file_cm(fname_or_fh, "wb") as file:
891+
self._print_pdf_to_fh(file, *args, **kwargs)
908892

909893
def _print_png_to_fh(self, fh, *args, **kwargs):
910894
converter = make_pdf_to_png_converter()
@@ -926,21 +910,13 @@ def _print_png_to_fh(self, fh, *args, **kwargs):
926910
except:
927911
TmpDirCleaner.add(tmpdir)
928912

929-
def print_png(self, fname_or_fh, *args, **kwargs):
930-
"""
931-
Use LaTeX to compile a pgf figure to pdf and convert it to png.
932-
"""
913+
def print_png(self, fname_or_fh, *args, dryrun=False, **kwargs):
914+
"""Use LaTeX to compile a pgf figure to pdf and convert it to png."""
933915
if kwargs.get("dryrun", False):
934916
self._print_pgf_to_fh(None, *args, **kwargs)
935917
return
936-
937-
if isinstance(fname_or_fh, str):
938-
with open(fname_or_fh, "wb") as fh:
939-
self._print_png_to_fh(fh, *args, **kwargs)
940-
elif is_writable_file_like(fname_or_fh):
941-
self._print_png_to_fh(fname_or_fh, *args, **kwargs)
942-
else:
943-
raise ValueError("filename must be a path or a file-like object")
918+
with cbook.open_file_cm(fname_or_fh, "wb") as file:
919+
self._print_png_to_fh(file, *args, **kwargs)
944920

945921
def get_renderer(self):
946922
return RendererPgf(self.figure, None, dummy=True)

lib/matplotlib/cbook/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,14 +344,14 @@ def is_hashable(obj):
344344

345345

346346
def is_writable_file_like(obj):
347-
"""return true if *obj* looks like a file object with a *write* method"""
347+
"""Return whether *obj* looks like a file object with a *write* method."""
348348
return callable(getattr(obj, 'write', None))
349349

350350

351351
def file_requires_unicode(x):
352352
"""
353-
Returns `True` if the given writable file-like object requires Unicode
354-
to be written to it.
353+
Returns whether the given writable file-like object requires Unicode to be
354+
written to it.
355355
"""
356356
try:
357357
x.write(b'')

lib/matplotlib/image.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -717,9 +717,7 @@ def set_interpolation(self, s):
717717
self.stale = True
718718

719719
def can_composite(self):
720-
"""
721-
Returns `True` if the image can be composited with its neighbors.
722-
"""
720+
"""Return whether the image can be composited with its neighbors."""
723721
trans = self.get_transform()
724722
return (
725723
self._interpolation != 'none' and
@@ -728,19 +726,20 @@ def can_composite(self):
728726

729727
def set_resample(self, v):
730728
"""
731-
Set whether or not image resampling is used.
729+
Set whether image resampling is used.
732730
733731
Parameters
734732
----------
735-
v : bool
733+
v : bool or None
734+
If None, use :rc:`image.resample`.
736735
"""
737736
if v is None:
738737
v = rcParams['image.resample']
739738
self._resample = v
740739
self.stale = True
741740

742741
def get_resample(self):
743-
"""Return the image resample boolean."""
742+
"""Return whether image resampling is used."""
744743
return self._resample
745744

746745
def set_filternorm(self, filternorm):

0 commit comments

Comments
 (0)