@staticmethod
  def CoerceValue(value, value_type):
    if isinstance(value, tuple):
      # In case of a tuple, we run the same function on the value itself and
      # add the formatted value.
      if (len(value) not in [2, 3] or
          (len(value) == 3 and not isinstance(value[2], dict))):
        raise DataTableException("Wrong format for value and formatting - %s." %
                                 str(value))
      if not isinstance(value[1], types.StringTypes + (types.NoneType,)):
        raise DataTableException("Formatted value is not string, given %s." %
                                 type(value[1]))
      js_value = DataTable.CoerceValue(value[0], value_type)
      return (js_value,) + value[1:]

    t_value = type(value)
    if value is None:
      return value
    if value_type == "boolean":
      return bool(value)

    elif value_type == "number":
      if isinstance(value, (int, long, float)):
        return value
      raise DataTableException("Wrong type %s when expected number" % t_value)

    elif value_type == "string":
      if isinstance(value, unicode):
        return value
      else:
        return str(value).decode("utf-8")

    elif value_type == "date":
      if isinstance(value, datetime.datetime):
        return datetime.date(value.year, value.month, value.day)
      elif isinstance(value, datetime.date):
        return value
      else:
        raise DataTableException("Wrong type %s when expected date" % t_value)

    elif value_type == "timeofday":
      if isinstance(value, datetime.datetime):
        return datetime.time(value.hour, value.minute, value.second)
      elif isinstance(value, datetime.time):
        return value
      else:
        raise DataTableException("Wrong type %s when expected time" % t_value)

    elif value_type == "datetime":
      if isinstance(value, datetime.datetime):
        return value
      else:
        raise DataTableException("Wrong type %s when expected datetime" %
                                 t_value)
    # If we got here, it means the given value_type was not one of the
    # supported types.
    raise DataTableException("Unsupported type %s" % value_type)
Coerces a single value into the type expected for its column.
dataset/ETHPy150Open evanbrumley/django-report-tools/report_tools/renderers/googlecharts/gviz_api/gviz_api.py/DataTable.CoerceValue