I want to test an unknown value with respect to the limitations that this NumPy dtype - for example, if I have an integer value, is it enough to fit in uint8 ?
As far as I can tell, the NumPy dtype architecture dtype not offer a way to do something like this:
try: numpy.uint8.validate(rupees) except numpy.dtype.ValidationError: print "Users can't hold more than 255 rupees."
My little fantasy API is based on Django models for checking the fields of a model , but this is just one example - the best mechanism that I was able to improve was in order
>>> nd = numpy.array([0,0,0,0,0,0], dtype=numpy.dtype('uint8')) >>> nd[0] 0 >>> nd[0] = 1 >>> nd[0] = -1 >>> nd array([255, 0, 0, 0, 0, 0], dtype=uint8) >>> nd[0] = 257 >>> nd array([1, 0, 0, 0, 0, 0], dtype=uint8)
Rounding off dubious values โโwith numpy.ndarray , entered explicitly as numpy.uint8 , returns me integers that were wrapped with something of a suitable size without throwing an exception or raising any other valid error state.
I would prefer not to use the flight suit of the cosmonaut architect, of course, but this is the preferred alternative that looks like an unattainable mess-spaghetti-monster if dtype(this) ... elif dtype(that) . Is there anything I can do here besides embarking on the grandiose and condescending act of writing my own API?