def _make_cast_fn(np_dtype):
  """Return a function to extract the typed value from the feature.

  For performance reasons it is preferred to have the cast fn
  constructed once (for each handler).

  Args:
    np_dtype: The numpy type of the Tensorflow feature.

  Returns:
    A function to extract the value field from a string depending on dtype.
  """

  def identity(x):
    return x

  # This is in agreement with Tensorflow conversions for Unicode values for both
  # Python 2 and 3 (and also works for non-Unicode objects). It is also in
  # agreement with the testWithUnicode of the Beam impl.
  def utf8(s):
    return s if isinstance(s, bytes) else s.encode('utf-8')

  vectorize = np.vectorize(utf8)

  def string_cast(x):
    if isinstance(x, list) or isinstance(x, np.ndarray) and x.ndim > 0:
      return map(utf8, x)
    elif isinstance(x, np.ndarray):
      return vectorize(x).tolist()
    return utf8(x)

  if issubclass(np_dtype, np.floating) or issubclass(np_dtype, np.integer):
    return identity

  return string_cast
Return a function to extract the typed value from the feature.

  For performance reasons it is preferred to have the cast fn
  constructed once (for each handler).

  Args:
    np_dtype: The numpy type of the Tensorflow feature.

  Returns:
    A function to extract the value field from a string depending on dtype.
  
tensorflow_transform/coders/example_proto_coder.py