@staticmethod<EOL><INDENT>def CoerceValue(value, value_type):<DEDENT>
if isinstance(value, tuple):<EOL><INDENT>if (len(value) not in [<NUM_LIT:2>, <NUM_LIT:3>] or<EOL>(len(value) == <NUM_LIT:3> and not isinstance(value[<NUM_LIT:2>], dict))):<EOL><INDENT>raise DataTableException("<STR_LIT>" %<EOL>str(value))<EOL><DEDENT>if not isinstance(value[<NUM_LIT:1>], six.string_types + (type(None),)):<EOL><INDENT>raise DataTableException("<STR_LIT>" %<EOL>type(value[<NUM_LIT:1>]))<EOL><DEDENT>js_value = DataTable.CoerceValue(value[<NUM_LIT:0>], value_type)<EOL>return (js_value,) + value[<NUM_LIT:1>:]<EOL><DEDENT>t_value = type(value)<EOL>if value is None:<EOL><INDENT>return value<EOL><DEDENT>if value_type == "<STR_LIT>":<EOL><INDENT>return bool(value)<EOL><DEDENT>elif value_type == "<STR_LIT>":<EOL><INDENT>if isinstance(value, six.integer_types + (float,)):<EOL><INDENT>return value<EOL><DEDENT>raise DataTableException("<STR_LIT>" % t_value)<EOL><DEDENT>elif value_type == "<STR_LIT:string>":<EOL><INDENT>if isinstance(value, six.text_type):<EOL><INDENT>return value<EOL><DEDENT>if isinstance(value, bytes):<EOL><INDENT>return six.text_type(value, encoding="<STR_LIT:utf-8>")<EOL><DEDENT>else:<EOL><INDENT>return six.text_type(value)<EOL><DEDENT><DEDENT>elif value_type == "<STR_LIT:date>":<EOL><INDENT>if isinstance(value, datetime.datetime):<EOL><INDENT>return datetime.date(value.year, value.month, value.day)<EOL><DEDENT>elif isinstance(value, datetime.date):<EOL><INDENT>return value<EOL><DEDENT>else:<EOL><INDENT>raise DataTableException("<STR_LIT>" % t_value)<EOL><DEDENT><DEDENT>elif value_type == "<STR_LIT>":<EOL><INDENT>if isinstance(value, datetime.datetime):<EOL><INDENT>return datetime.time(value.hour, value.minute, value.second)<EOL><DEDENT>elif isinstance(value, datetime.time):<EOL><INDENT>return value<EOL><DEDENT>else:<EOL><INDENT>raise DataTableException("<STR_LIT>" % t_value)<EOL><DEDENT><DEDENT>elif value_type == "<STR_LIT>":<EOL><INDENT>if isinstance(value, datetime.datetime):<EOL><INDENT>return value<EOL><DEDENT>else:<EOL><INDENT>raise DataTableException("<STR_LIT>" %<EOL>t_value)<EOL><DEDENT><DEDENT>raise DataTableException("<STR_LIT>" % value_type)<EOL>
Coerces a single value into the type expected for its column.

        Internal helper method.

        Args:
          value: The value which should be converted
          value_type: One of "string", "number", "boolean", "date", "datetime" or
                      "timeofday".

        Returns:
          An item of the Python type appropriate to the given value_type. Strings
          are also converted to Unicode using UTF-8 encoding if necessary.
          If a tuple is given, it should be in one of the following forms:
            - (value, formatted value)
            - (value, formatted value, custom properties)
          where the formatted value is a string, and custom properties is a
          dictionary of the custom properties for this cell.
          To specify custom properties without specifying formatted value, one can
          pass None as the formatted value.
          One can also have a null-valued cell with formatted value and/or custom
          properties by specifying None for the value.
          This method ignores the custom properties except for checking that it is a
          dictionary. The custom properties are handled in the ToJSon and ToJSCode
          methods.
          The real type of the given value is not strictly checked. For example,
          any type can be used for string - as we simply take its str( ) and for
          boolean value we just check "if value".
          Examples:
            CoerceValue(None, "string") returns None
            CoerceValue((5, "5$"), "number") returns (5, "5$")
            CoerceValue(100, "string") returns "100"
            CoerceValue(0, "boolean") returns False

        Raises:
          DataTableException: The value and type did not match in a not-recoverable
                              way, for example given value 'abc' for type 'number'.