From 9468f6dafd90fdcfef1757c17f1d0e97df43997e Mon Sep 17 00:00:00 2001 From: Pierre-antoine Comby Date: Sat, 16 Mar 2024 18:56:08 +0100 Subject: [PATCH] feat: add dunder method for math operations. --- lib/mpl_toolkits/axes_grid1/axes_size.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/lib/mpl_toolkits/axes_grid1/axes_size.py b/lib/mpl_toolkits/axes_grid1/axes_size.py index e417c1a899ac..86e5f70d9824 100644 --- a/lib/mpl_toolkits/axes_grid1/axes_size.py +++ b/lib/mpl_toolkits/axes_grid1/axes_size.py @@ -7,6 +7,10 @@ class (or others) to determine the size of each Axes. The unit Note that this class is nothing more than a simple tuple of two floats. Take a look at the Divider class to see how these two values are used. + +Once created, the unit classes can be modified by simple arithmetic +operations: addition /subtraction with another unit type or a real number and scaling +(multiplication or division) by a real number. """ from numbers import Real @@ -17,14 +21,33 @@ class (or others) to determine the size of each Axes. The unit class _Base: def __rmul__(self, other): + return self * other + + def __mul__(self, other): + if not isinstance(other, Real): + return NotImplemented return Fraction(other, self) + def __div__(self, other): + return (1 / other) * self + def __add__(self, other): if isinstance(other, _Base): return Add(self, other) else: return Add(self, Fixed(other)) + def __neg__(self): + return -1 * self + + def __radd__(self, other): + # other cannot be a _Base instance, because A + B would trigger + # A.__add__(B) first. + return Add(self, Fixed(other)) + + def __sub__(self, other): + return self + (-other) + def get_size(self, renderer): """ Return two-float tuple with relative and absolute sizes.