diff --git a/lib/matplotlib/backends/backend_cairo.py b/lib/matplotlib/backends/backend_cairo.py index b05a5fc0967a..14761f0a5d81 100644 --- a/lib/matplotlib/backends/backend_cairo.py +++ b/lib/matplotlib/backends/backend_cairo.py @@ -6,6 +6,7 @@ This backend depends on cairocffi or pycairo. """ +import functools import gzip import math @@ -467,20 +468,8 @@ def _get_printed_image_surface(self): self.figure.draw(renderer) return surface - def print_pdf(self, fobj, *args, **kwargs): - return self._save(fobj, 'pdf', *args, **kwargs) - - def print_ps(self, fobj, *args, **kwargs): - return self._save(fobj, 'ps', *args, **kwargs) - - def print_svg(self, fobj, *args, **kwargs): - return self._save(fobj, 'svg', *args, **kwargs) - - def print_svgz(self, fobj, *args, **kwargs): - return self._save(fobj, 'svgz', *args, **kwargs) - @_check_savefig_extra_args - def _save(self, fo, fmt, *, orientation='portrait'): + def _save(self, fmt, fobj, *, orientation='portrait'): # save PDF/PS/SVG dpi = 72 @@ -496,22 +485,22 @@ def _save(self, fo, fmt, *, orientation='portrait'): if not hasattr(cairo, 'PSSurface'): raise RuntimeError('cairo has not been compiled with PS ' 'support enabled') - surface = cairo.PSSurface(fo, width_in_points, height_in_points) + surface = cairo.PSSurface(fobj, width_in_points, height_in_points) elif fmt == 'pdf': if not hasattr(cairo, 'PDFSurface'): raise RuntimeError('cairo has not been compiled with PDF ' 'support enabled') - surface = cairo.PDFSurface(fo, width_in_points, height_in_points) + surface = cairo.PDFSurface(fobj, width_in_points, height_in_points) elif fmt in ('svg', 'svgz'): if not hasattr(cairo, 'SVGSurface'): raise RuntimeError('cairo has not been compiled with SVG ' 'support enabled') if fmt == 'svgz': - if isinstance(fo, str): - fo = gzip.GzipFile(fo, 'wb') + if isinstance(fobj, str): + fobj = gzip.GzipFile(fobj, 'wb') else: - fo = gzip.GzipFile(None, 'wb', fileobj=fo) - surface = cairo.SVGSurface(fo, width_in_points, height_in_points) + fobj = gzip.GzipFile(None, 'wb', fileobj=fobj) + surface = cairo.SVGSurface(fobj, width_in_points, height_in_points) else: raise ValueError("Unknown format: {!r}".format(fmt)) @@ -531,7 +520,12 @@ def _save(self, fo, fmt, *, orientation='portrait'): ctx.show_page() surface.finish() if fmt == 'svgz': - fo.close() + fobj.close() + + print_pdf = functools.partialmethod(_save, "pdf") + print_ps = functools.partialmethod(_save, "ps") + print_svg = functools.partialmethod(_save, "svg") + print_svgz = functools.partialmethod(_save, "svgz") @_Backend.export diff --git a/lib/matplotlib/backends/backend_wx.py b/lib/matplotlib/backends/backend_wx.py index 9d12357d2523..ed06ed5c141d 100644 --- a/lib/matplotlib/backends/backend_wx.py +++ b/lib/matplotlib/backends/backend_wx.py @@ -7,6 +7,7 @@ Copyright (C) Jeremy O'Donoghue & John Hunter, 2003-4. """ +import functools import logging import math import pathlib @@ -829,29 +830,8 @@ def draw(self, drawDC=None): self._isDrawn = True self.gui_repaint(drawDC=drawDC) - def print_bmp(self, filename, *args, **kwargs): - return self._print_image(filename, wx.BITMAP_TYPE_BMP, *args, **kwargs) - - def print_jpeg(self, filename, *args, **kwargs): - return self._print_image(filename, wx.BITMAP_TYPE_JPEG, - *args, **kwargs) - print_jpg = print_jpeg - - def print_pcx(self, filename, *args, **kwargs): - return self._print_image(filename, wx.BITMAP_TYPE_PCX, *args, **kwargs) - - def print_png(self, filename, *args, **kwargs): - return self._print_image(filename, wx.BITMAP_TYPE_PNG, *args, **kwargs) - - def print_tiff(self, filename, *args, **kwargs): - return self._print_image(filename, wx.BITMAP_TYPE_TIF, *args, **kwargs) - print_tif = print_tiff - - def print_xpm(self, filename, *args, **kwargs): - return self._print_image(filename, wx.BITMAP_TYPE_XPM, *args, **kwargs) - @_check_savefig_extra_args - def _print_image(self, filename, filetype, *, quality=None): + def _print_image(self, filetype, filename, *, quality=None): origBitmap = self.bitmap self.bitmap = wx.Bitmap(math.ceil(self.figure.bbox.width), @@ -897,6 +877,19 @@ def _print_image(self, filename, filetype, *, quality=None): if self: self.Refresh() + print_bmp = functools.partialmethod( + _print_image, wx.BITMAP_TYPE_BMP) + print_jpeg = print_jpg = functools.partialmethod( + _print_image, wx.BITMAP_TYPE_JPEG) + print_pcx = functools.partialmethod( + _print_image, wx.BITMAP_TYPE_PCX) + print_png = functools.partialmethod( + _print_image, wx.BITMAP_TYPE_PNG) + print_tiff = print_tif = functools.partialmethod( + _print_image, wx.BITMAP_TYPE_TIF) + print_xpm = functools.partialmethod( + _print_image, wx.BITMAP_TYPE_XPM) + class FigureFrameWx(wx.Frame): def __init__(self, num, fig): diff --git a/lib/matplotlib/testing/jpl_units/Duration.py b/lib/matplotlib/testing/jpl_units/Duration.py index 9bd8d9fe71cf..fae4169f9239 100644 --- a/lib/matplotlib/testing/jpl_units/Duration.py +++ b/lib/matplotlib/testing/jpl_units/Duration.py @@ -1,5 +1,6 @@ """Duration module.""" +import functools import operator from matplotlib import _api @@ -44,38 +45,20 @@ def seconds(self): def __bool__(self): return self._seconds != 0 - def __eq__(self, rhs): - return self._cmp(rhs, operator.eq) - - def __ne__(self, rhs): - return self._cmp(rhs, operator.ne) - - def __lt__(self, rhs): - return self._cmp(rhs, operator.lt) - - def __le__(self, rhs): - return self._cmp(rhs, operator.le) - - def __gt__(self, rhs): - return self._cmp(rhs, operator.gt) - - def __ge__(self, rhs): - return self._cmp(rhs, operator.ge) - - def _cmp(self, rhs, op): + def _cmp(self, op, rhs): """ - Compare two Durations. - - = INPUT VARIABLES - - rhs The Duration to compare against. - - op The function to do the comparison - - = RETURN VALUE - - Returns op(self, rhs) + Check that *self* and *rhs* share frames; compare them using *op*. """ self.checkSameFrame(rhs, "compare") return op(self._seconds, rhs._seconds) + __eq__ = functools.partialmethod(_cmp, operator.eq) + __ne__ = functools.partialmethod(_cmp, operator.ne) + __lt__ = functools.partialmethod(_cmp, operator.lt) + __le__ = functools.partialmethod(_cmp, operator.le) + __gt__ = functools.partialmethod(_cmp, operator.gt) + __ge__ = functools.partialmethod(_cmp, operator.ge) + def __add__(self, rhs): """ Add two Durations. @@ -126,17 +109,7 @@ def __mul__(self, rhs): """ return Duration(self._frame, self._seconds * float(rhs)) - def __rmul__(self, lhs): - """ - Scale a Duration by a value. - - = INPUT VARIABLES - - lhs The scalar to multiply by. - - = RETURN VALUE - - Returns the scaled Duration. - """ - return Duration(self._frame, self._seconds * float(lhs)) + __rmul__ = __mul__ def __str__(self): """Print the Duration.""" diff --git a/lib/matplotlib/testing/jpl_units/Epoch.py b/lib/matplotlib/testing/jpl_units/Epoch.py index 25666523fbff..3808663056e7 100644 --- a/lib/matplotlib/testing/jpl_units/Epoch.py +++ b/lib/matplotlib/testing/jpl_units/Epoch.py @@ -1,5 +1,6 @@ """Epoch module.""" +import functools import operator import math import datetime as DT @@ -106,44 +107,22 @@ def secondsPast(self, frame, jd): delta = t._jd - jd return t._seconds + delta * 86400 - def __eq__(self, rhs): - return self._cmp(rhs, operator.eq) - - def __ne__(self, rhs): - return self._cmp(rhs, operator.ne) - - def __lt__(self, rhs): - return self._cmp(rhs, operator.lt) - - def __le__(self, rhs): - return self._cmp(rhs, operator.le) - - def __gt__(self, rhs): - return self._cmp(rhs, operator.gt) - - def __ge__(self, rhs): - return self._cmp(rhs, operator.ge) - - def _cmp(self, rhs, op): - """ - Compare two Epoch's. - - = INPUT VARIABLES - - rhs The Epoch to compare against. - - op The function to do the comparison - - = RETURN VALUE - - Returns op(self, rhs) - """ + def _cmp(self, op, rhs): + """Compare Epochs *self* and *rhs* using operator *op*.""" t = self if self._frame != rhs._frame: t = self.convert(rhs._frame) - if t._jd != rhs._jd: return op(t._jd, rhs._jd) - return op(t._seconds, rhs._seconds) + __eq__ = functools.partialmethod(_cmp, operator.eq) + __ne__ = functools.partialmethod(_cmp, operator.ne) + __lt__ = functools.partialmethod(_cmp, operator.lt) + __le__ = functools.partialmethod(_cmp, operator.le) + __gt__ = functools.partialmethod(_cmp, operator.gt) + __ge__ = functools.partialmethod(_cmp, operator.ge) + def __add__(self, rhs): """ Add a duration to an Epoch. diff --git a/lib/matplotlib/testing/jpl_units/UnitDbl.py b/lib/matplotlib/testing/jpl_units/UnitDbl.py index 68481a80fd57..b713ab3aba67 100644 --- a/lib/matplotlib/testing/jpl_units/UnitDbl.py +++ b/lib/matplotlib/testing/jpl_units/UnitDbl.py @@ -1,5 +1,6 @@ """UnitDbl module.""" +import functools import operator from matplotlib import _api @@ -88,99 +89,32 @@ def __bool__(self): """Return the truth value of a UnitDbl.""" return bool(self._value) - def __eq__(self, rhs): - return self._cmp(rhs, operator.eq) - - def __ne__(self, rhs): - return self._cmp(rhs, operator.ne) - - def __lt__(self, rhs): - return self._cmp(rhs, operator.lt) - - def __le__(self, rhs): - return self._cmp(rhs, operator.le) - - def __gt__(self, rhs): - return self._cmp(rhs, operator.gt) - - def __ge__(self, rhs): - return self._cmp(rhs, operator.ge) - - def _cmp(self, rhs, op): - """ - Compare two UnitDbl's. - - = ERROR CONDITIONS - - If the input rhs units are not the same as our units, - an error is thrown. - - = INPUT VARIABLES - - rhs The UnitDbl to compare against. - - op The function to do the comparison - - = RETURN VALUE - - Returns op(self, rhs) - """ + def _cmp(self, op, rhs): + """Check that *self* and *rhs* share units; compare them using *op*.""" self.checkSameUnits(rhs, "compare") return op(self._value, rhs._value) - def __add__(self, rhs): - """ - Add two UnitDbl's. - - = ERROR CONDITIONS - - If the input rhs units are not the same as our units, - an error is thrown. - - = INPUT VARIABLES - - rhs The UnitDbl to add. - - = RETURN VALUE - - Returns the sum of ourselves and the input UnitDbl. - """ - self.checkSameUnits(rhs, "add") - return UnitDbl(self._value + rhs._value, self._units) - - def __sub__(self, rhs): - """ - Subtract two UnitDbl's. - - = ERROR CONDITIONS - - If the input rhs units are not the same as our units, - an error is thrown. - - = INPUT VARIABLES - - rhs The UnitDbl to subtract. - - = RETURN VALUE - - Returns the difference of ourselves and the input UnitDbl. - """ - self.checkSameUnits(rhs, "subtract") - return UnitDbl(self._value - rhs._value, self._units) + __eq__ = functools.partialmethod(_cmp, operator.eq) + __ne__ = functools.partialmethod(_cmp, operator.ne) + __lt__ = functools.partialmethod(_cmp, operator.lt) + __le__ = functools.partialmethod(_cmp, operator.le) + __gt__ = functools.partialmethod(_cmp, operator.gt) + __ge__ = functools.partialmethod(_cmp, operator.ge) - def __mul__(self, rhs): - """ - Scale a UnitDbl by a value. + def _binop_unit_unit(self, op, rhs): + """Check that *self* and *rhs* share units; combine them using *op*.""" + self.checkSameUnits(rhs, op.__name__) + return UnitDbl(op(self._value, rhs._value), self._units) - = INPUT VARIABLES - - rhs The scalar to multiply by. + __add__ = functools.partialmethod(_binop_unit_unit, operator.add) + __sub__ = functools.partialmethod(_binop_unit_unit, operator.sub) - = RETURN VALUE - - Returns the scaled UnitDbl. - """ - return UnitDbl(self._value * rhs, self._units) + def _binop_unit_scalar(self, op, scalar): + """Combine *self* and *scalar* using *op*.""" + return UnitDbl(op(self._value, scalar), self._units) - def __rmul__(self, lhs): - """ - Scale a UnitDbl by a value. - - = INPUT VARIABLES - - lhs The scalar to multiply by. - - = RETURN VALUE - - Returns the scaled UnitDbl. - """ - return UnitDbl(self._value * lhs, self._units) + __mul__ = functools.partialmethod(_binop_unit_scalar, operator.mul) + __rmul__ = functools.partialmethod(_binop_unit_scalar, operator.mul) def __str__(self): """Print the UnitDbl."""