code: def to_bytes(value, encoding='utf-8'):
      
      result = (value.encode(encoding)
                if isinstance(value, six.text_type) else value)
      if isinstance(result, six.binary_type):
          return result
      else:
          raise ValueError('{0!r} could not be converted to bytes'.format(value))
docs: Converts a string value to bytes, if necessary.
  
      Unfortunately, ``six.b`` is insufficient for this task since in
      Python 2 because it does not modify ``unicode`` objects.
  
      Args:
          value (Union[str, bytes]): The value to be converted.
          encoding (str): The encoding to use to convert unicode to bytes.
              Defaults to "utf-8".
  
      Returns:
          bytes: The original value converted to bytes (if unicode) or as
              passed in if it started out as bytes.
  
      Raises:
          ValueError: If the value could not be converted to bytes.