Skip to content

Commit 7875bc5

Browse files
authored
Merge pull request #12899 from anntzer/cleanup
Small cleanups.
2 parents 4d1380f + 9162616 commit 7875bc5

File tree

3 files changed

+19
-44
lines changed

3 files changed

+19
-44
lines changed

lib/matplotlib/backends/backend_pgf.py

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
_Backend, FigureCanvasBase, FigureManagerBase, GraphicsContextBase,
1919
RendererBase)
2020
from matplotlib.backends.backend_mixed import MixedModeRenderer
21-
from matplotlib.cbook import is_writable_file_like
2221
from matplotlib.path import Path
2322
from matplotlib.figure import Figure
2423
from matplotlib._pylab_helpers import Gcf
@@ -385,8 +384,8 @@ def __init__(self, figure, fh, dummy=False):
385384
Matplotlib figure to initialize height, width and dpi from.
386385
fh : file-like
387386
File handle for the output of the drawing commands.
388-
389387
"""
388+
390389
RendererBase.__init__(self)
391390
self.dpi = figure.dpi
392391
self.fh = fh
@@ -842,16 +841,10 @@ def print_pgf(self, fname_or_fh, *args, **kwargs):
842841
if kwargs.get("dryrun", False):
843842
self._print_pgf_to_fh(None, *args, **kwargs)
844843
return
845-
846-
# figure out where the pgf is to be written to
847-
if isinstance(fname_or_fh, str):
848-
with open(fname_or_fh, "w", encoding="utf-8") as fh:
849-
self._print_pgf_to_fh(fh, *args, **kwargs)
850-
elif is_writable_file_like(fname_or_fh):
851-
fh = codecs.getwriter("utf-8")(fname_or_fh)
852-
self._print_pgf_to_fh(fh, *args, **kwargs)
853-
else:
854-
raise ValueError("filename must be a path")
844+
with cbook.open_file_cm(fname_or_fh, "w", encoding="utf-8") as file:
845+
if not cbook.file_requires_unicode(file):
846+
file = codecs.getwriter("utf-8")(file)
847+
self._print_pgf_to_fh(file, *args, **kwargs)
855848

856849
def _print_pdf_to_fh(self, fh, *args, **kwargs):
857850
w, h = self.figure.get_figwidth(), self.figure.get_figheight()
@@ -896,21 +889,12 @@ def _print_pdf_to_fh(self, fh, *args, **kwargs):
896889
TmpDirCleaner.add(tmpdir)
897890

898891
def print_pdf(self, fname_or_fh, *args, **kwargs):
899-
"""
900-
Use LaTeX to compile a Pgf generated figure to PDF.
901-
"""
892+
"""Use LaTeX to compile a Pgf generated figure to PDF."""
902893
if kwargs.get("dryrun", False):
903894
self._print_pgf_to_fh(None, *args, **kwargs)
904895
return
905-
906-
# figure out where the pdf is to be written to
907-
if isinstance(fname_or_fh, str):
908-
with open(fname_or_fh, "wb") as fh:
909-
self._print_pdf_to_fh(fh, *args, **kwargs)
910-
elif is_writable_file_like(fname_or_fh):
911-
self._print_pdf_to_fh(fname_or_fh, *args, **kwargs)
912-
else:
913-
raise ValueError("filename must be a path or a file-like object")
896+
with cbook.open_file_cm(fname_or_fh, "wb") as file:
897+
self._print_pdf_to_fh(file, *args, **kwargs)
914898

915899
def _print_png_to_fh(self, fh, *args, **kwargs):
916900
converter = make_pdf_to_png_converter()
@@ -933,20 +917,12 @@ def _print_png_to_fh(self, fh, *args, **kwargs):
933917
TmpDirCleaner.add(tmpdir)
934918

935919
def print_png(self, fname_or_fh, *args, **kwargs):
936-
"""
937-
Use LaTeX to compile a pgf figure to pdf and convert it to png.
938-
"""
920+
"""Use LaTeX to compile a pgf figure to pdf and convert it to png."""
939921
if kwargs.get("dryrun", False):
940922
self._print_pgf_to_fh(None, *args, **kwargs)
941923
return
942-
943-
if isinstance(fname_or_fh, str):
944-
with open(fname_or_fh, "wb") as fh:
945-
self._print_png_to_fh(fh, *args, **kwargs)
946-
elif is_writable_file_like(fname_or_fh):
947-
self._print_png_to_fh(fname_or_fh, *args, **kwargs)
948-
else:
949-
raise ValueError("filename must be a path or a file-like object")
924+
with cbook.open_file_cm(fname_or_fh, "wb") as file:
925+
self._print_png_to_fh(file, *args, **kwargs)
950926

951927
def get_renderer(self):
952928
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
@@ -360,14 +360,14 @@ def is_hashable(obj):
360360

361361

362362
def is_writable_file_like(obj):
363-
"""return true if *obj* looks like a file object with a *write* method"""
363+
"""Return whether *obj* looks like a file object with a *write* method."""
364364
return callable(getattr(obj, 'write', None))
365365

366366

367367
def file_requires_unicode(x):
368368
"""
369-
Returns `True` if the given writable file-like object requires Unicode
370-
to be written to it.
369+
Returns whether the given writable file-like object requires Unicode to be
370+
written to it.
371371
"""
372372
try:
373373
x.write(b'')

lib/matplotlib/image.py

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

746746
def can_composite(self):
747-
"""
748-
Returns `True` if the image can be composited with its neighbors.
749-
"""
747+
"""Return whether the image can be composited with its neighbors."""
750748
trans = self.get_transform()
751749
return (
752750
self._interpolation != 'none' and
@@ -755,19 +753,20 @@ def can_composite(self):
755753

756754
def set_resample(self, v):
757755
"""
758-
Set whether or not image resampling is used.
756+
Set whether image resampling is used.
759757
760758
Parameters
761759
----------
762-
v : bool
760+
v : bool or None
761+
If None, use :rc:`image.resample` = True.
763762
"""
764763
if v is None:
765764
v = rcParams['image.resample']
766765
self._resample = v
767766
self.stale = True
768767

769768
def get_resample(self):
770-
"""Return the image resample boolean."""
769+
"""Return whether image resampling is used."""
771770
return self._resample
772771

773772
def set_filternorm(self, filternorm):

0 commit comments

Comments
 (0)