| 14 | |
| 15 | # Basic class that wraps numpy array and has units |
| 16 | class Quantity: |
| 17 | def __init__(self, data, units): |
| 18 | self.magnitude = data |
| 19 | self.units = units |
| 20 | |
| 21 | def to(self, new_units): |
| 22 | factors = {('hours', 'seconds'): 3600, ('minutes', 'hours'): 1 / 60, |
| 23 | ('minutes', 'seconds'): 60, ('feet', 'miles'): 1 / 5280., |
| 24 | ('feet', 'inches'): 12, ('miles', 'inches'): 12 * 5280} |
| 25 | if self.units != new_units: |
| 26 | mult = factors[self.units, new_units] |
| 27 | return Quantity(mult * self.magnitude, new_units) |
| 28 | else: |
| 29 | return Quantity(self.magnitude, self.units) |
| 30 | |
| 31 | def __copy__(self): |
| 32 | return Quantity(self.magnitude, self.units) |
| 33 | |
| 34 | def __getattr__(self, attr): |
| 35 | return getattr(self.magnitude, attr) |
| 36 | |
| 37 | def __getitem__(self, item): |
| 38 | if np.iterable(self.magnitude): |
| 39 | return Quantity(self.magnitude[item], self.units) |
| 40 | else: |
| 41 | return Quantity(self.magnitude, self.units) |
| 42 | |
| 43 | def __array__(self): |
| 44 | return np.asarray(self.magnitude) |
| 45 | |
| 46 | |
| 47 | @pytest.fixture |
no outgoing calls
searching dependent graphs…