Instantly share code, notes, and snippets.

# keflavich/testqty.py Last active Dec 19, 2015

What would you like to do?
Quantity sublcassing tests
 #http://stackoverflow.com/questions/17444657/how-to-prevent-numpy-from-splitting-up-an-array-like-object class Quantity(object): def __init__(self, value, unit): self.unit = unit self.value = value def __getitem__(self, key): return Quantity(self.value[key], unit=self.unit) def __len__(self): return len(self.value) def __array__(self): return self.value def __array_wrap__(self, arr, context=None): return Quantity( np.ndarray.__array_wrap__(self.value, arr, context), self.unit) __array_priority__ = 1000 def __repr__(self): return "" import numpy as np q = Quantity(np.array([1,2,3],dtype='float'), 'degree') print "array(q): ", print(repr(np.array(q))) print "q * [1,3,5]:", print(repr(q * np.array([1,3,5]))) print "[1,3,5] * q:", print(repr(np.array([1,3,5]) * q)) print "q: ", print(repr(q)) """ \$ python testqty.py array(q): array([ 1., 2., 3.]) q * [1,3,5]: [1,3,5] * q: q: """