TypeConverter

The TypeConverter class allows you to convert arbitrary values into objects specified by a type code and to get the type code of an object.

Description

The TypeConverter class allows you to convert arbitrary values into objects specified by a type code and to get the type code of an object. The type codes are defined in the TypeCode class.

Static methods

to_nullable_type

Converts value into an object type specified by Type Code or returns None when conversion is not possible.

static to_nullable_type(type: TypeCode, value: Any): Any

  • type: Any - the TypeCode for the data type into which ‘value’ is to be converted.
  • value: Any - the value to convert.
  • returns: Any - object value of type corresponding to TypeCode, or None when conversion is not supported.

to_string

Converts a TypeCode into its string name.

static to_string(type: TypeCode): str

  • type: TypeCode - the TypeCode to convert into a string.
  • returns: str - the name of the TypeCode passed as a string value.

to_type

Converts value into an object type specified by Type Code or returns type default when conversion is not possible.

static to_type(type: TypeCode, value: Any): Any

  • type: TypeCode - the value to convert.
  • value: Any - the value to convert.
  • returns: Any - object value of type corresponding to TypeCode, or type default when conversion is not supported.

to_type_code

Gets TypeCode for specific value.

static to_type_code(value: Any): TypeCode

  • value: Any - value whose TypeCode is to be resolved.
  • returns: TypeCode - the TypeCode that corresponds to the passed object’s type.

to_type_with_default

Converts value into an object type specified by Type Code or returns default value when conversion is not possible.

static to_type_with_default(type: TypeCode, value: Any, default_value: Any): Any

  • type: TypeCode - the TypeCode for the data type into which ‘value’ is to be converted.
  • value: Any - the value to convert.
  • default_value: Any - the default value to return if conversion is not possible (returns None).
  • returns: Any - object value of type corresponding to TypeCode, or default value when conversion is not supported.

Examples

value1 = TypeConverter.to_type_code("Hello world") # Returns 1

See also