title: Validation against NumPy dtypes -- what's the least circuitous way to check values?
body: <p>I want to test an unknown value against the constraints that a given NumPy <code>dtype</code> implies -- e.g., if I have an integer value, is it small enough to fit in a <code>uint8</code>?</p>
  
  <p>As best I can ascertain, NumPy's <code>dtype</code> architecture doesn't offer a way to do something like this:</p>
  
  <pre><code>### FICTIONAL NUMPY CODE: I made this up ###
  try:
      numpy.uint8.validate(rupees)
  except numpy.dtype.ValidationError:
      print "Users can't hold more than 255 rupees."
  </code></pre>
  
  <p>My little fantasy API is based on Django's <a href="https://docs.djangoproject.com/en/dev/ref/validators/">model-field validators</a>, but that's just one example -- the best mechanism I managed to contrive was along the lines of this:</p>
  
  <pre><code>&gt;&gt;&gt; nd = numpy.array([0,0,0,0,0,0], dtype=numpy.dtype('uint8'))
  &gt;&gt;&gt; nd[0]
  0
  &gt;&gt;&gt; nd[0] = 1
  &gt;&gt;&gt; nd[0] = -1
  &gt;&gt;&gt; nd
  array([255,   0,   0,   0,   0,   0], dtype=uint8)
  &gt;&gt;&gt; nd[0] = 257
  &gt;&gt;&gt; nd
  array([1, 0, 0, 0, 0, 0], dtype=uint8)
  </code></pre>
  
  <p>Round-tripping the questionable values through a <code>numpy.ndarray</code> typed as explicitly <code>numpy.uint8</code> gives me back integers that have been wrapped to something with an appropriate size -- without tossing an exception, or raising any other sort of actionable error state.</p>
  
  <p>I'd rather not put on the architecture-astronaut flight suit, of course, but that's preferable the alternative, which looks like unmaintainable spaghetti-monster mess of <code>if dtype(this) ... elif dtype(that)</code> statements. Is there anything I can do here besides embarking on the grandiose and indulgent act of writing my own API? </p>