|
1 | 1 | """UnitDbl module.""" |
2 | 2 |
|
| 3 | +import functools |
3 | 4 | import operator |
4 | 5 |
|
5 | 6 | from matplotlib import _api |
@@ -88,99 +89,32 @@ def __bool__(self): |
88 | 89 | """Return the truth value of a UnitDbl.""" |
89 | 90 | return bool(self._value) |
90 | 91 |
|
91 | | - def __eq__(self, rhs): |
92 | | - return self._cmp(rhs, operator.eq) |
93 | | - |
94 | | - def __ne__(self, rhs): |
95 | | - return self._cmp(rhs, operator.ne) |
96 | | - |
97 | | - def __lt__(self, rhs): |
98 | | - return self._cmp(rhs, operator.lt) |
99 | | - |
100 | | - def __le__(self, rhs): |
101 | | - return self._cmp(rhs, operator.le) |
102 | | - |
103 | | - def __gt__(self, rhs): |
104 | | - return self._cmp(rhs, operator.gt) |
105 | | - |
106 | | - def __ge__(self, rhs): |
107 | | - return self._cmp(rhs, operator.ge) |
108 | | - |
109 | | - def _cmp(self, rhs, op): |
110 | | - """ |
111 | | - Compare two UnitDbl's. |
112 | | -
|
113 | | - = ERROR CONDITIONS |
114 | | - - If the input rhs units are not the same as our units, |
115 | | - an error is thrown. |
116 | | -
|
117 | | - = INPUT VARIABLES |
118 | | - - rhs The UnitDbl to compare against. |
119 | | - - op The function to do the comparison |
120 | | -
|
121 | | - = RETURN VALUE |
122 | | - - Returns op(self, rhs) |
123 | | - """ |
| 92 | + def _cmp(self, op, rhs): |
| 93 | + """Check that *self* and *rhs* share units; compare them using *op*.""" |
124 | 94 | self.checkSameUnits(rhs, "compare") |
125 | 95 | return op(self._value, rhs._value) |
126 | 96 |
|
127 | | - def __add__(self, rhs): |
128 | | - """ |
129 | | - Add two UnitDbl's. |
130 | | -
|
131 | | - = ERROR CONDITIONS |
132 | | - - If the input rhs units are not the same as our units, |
133 | | - an error is thrown. |
134 | | -
|
135 | | - = INPUT VARIABLES |
136 | | - - rhs The UnitDbl to add. |
137 | | -
|
138 | | - = RETURN VALUE |
139 | | - - Returns the sum of ourselves and the input UnitDbl. |
140 | | - """ |
141 | | - self.checkSameUnits(rhs, "add") |
142 | | - return UnitDbl(self._value + rhs._value, self._units) |
143 | | - |
144 | | - def __sub__(self, rhs): |
145 | | - """ |
146 | | - Subtract two UnitDbl's. |
147 | | -
|
148 | | - = ERROR CONDITIONS |
149 | | - - If the input rhs units are not the same as our units, |
150 | | - an error is thrown. |
151 | | -
|
152 | | - = INPUT VARIABLES |
153 | | - - rhs The UnitDbl to subtract. |
154 | | -
|
155 | | - = RETURN VALUE |
156 | | - - Returns the difference of ourselves and the input UnitDbl. |
157 | | - """ |
158 | | - self.checkSameUnits(rhs, "subtract") |
159 | | - return UnitDbl(self._value - rhs._value, self._units) |
| 97 | + __eq__ = functools.partialmethod(_cmp, operator.eq) |
| 98 | + __ne__ = functools.partialmethod(_cmp, operator.ne) |
| 99 | + __lt__ = functools.partialmethod(_cmp, operator.lt) |
| 100 | + __le__ = functools.partialmethod(_cmp, operator.le) |
| 101 | + __gt__ = functools.partialmethod(_cmp, operator.gt) |
| 102 | + __ge__ = functools.partialmethod(_cmp, operator.ge) |
160 | 103 |
|
161 | | - def __mul__(self, rhs): |
162 | | - """ |
163 | | - Scale a UnitDbl by a value. |
| 104 | + def _binop_unit_unit(self, op, rhs): |
| 105 | + """Check that *self* and *rhs* share units; combine them using *op*.""" |
| 106 | + self.checkSameUnits(rhs, op.__name__) |
| 107 | + return UnitDbl(op(self._value, rhs._value), self._units) |
164 | 108 |
|
165 | | - = INPUT VARIABLES |
166 | | - - rhs The scalar to multiply by. |
| 109 | + __add__ = functools.partialmethod(_binop_unit_unit, operator.add) |
| 110 | + __sub__ = functools.partialmethod(_binop_unit_unit, operator.sub) |
167 | 111 |
|
168 | | - = RETURN VALUE |
169 | | - - Returns the scaled UnitDbl. |
170 | | - """ |
171 | | - return UnitDbl(self._value * rhs, self._units) |
| 112 | + def _binop_unit_scalar(self, op, scalar): |
| 113 | + """Combine *self* and *scalar* using *op*.""" |
| 114 | + return UnitDbl(op(self._value, scalar), self._units) |
172 | 115 |
|
173 | | - def __rmul__(self, lhs): |
174 | | - """ |
175 | | - Scale a UnitDbl by a value. |
176 | | -
|
177 | | - = INPUT VARIABLES |
178 | | - - lhs The scalar to multiply by. |
179 | | -
|
180 | | - = RETURN VALUE |
181 | | - - Returns the scaled UnitDbl. |
182 | | - """ |
183 | | - return UnitDbl(self._value * lhs, self._units) |
| 116 | + __mul__ = functools.partialmethod(_binop_unit_scalar, operator.mul) |
| 117 | + __rmul__ = functools.partialmethod(_binop_unit_scalar, operator.mul) |
184 | 118 |
|
185 | 119 | def __str__(self): |
186 | 120 | """Print the UnitDbl.""" |
|
0 commit comments