

def to_bytes_safe(text, encoding='utf-8'):
    'Convert the input value into bytes type.\n\n    If the input value is string type and could be encode as UTF-8 bytes, the\n    encoded value will be returned. Otherwise, the encoding has failed, the\n    origin value will be returned as well.\n\n    :param text: the input value which could be string or bytes.\n    :param encoding: the expected encoding be used while converting the string\n                     input into bytes.\n    :rtype: :class:`~__builtin__.bytes`\n    '
    if (not isinstance(text, (bytes, text_type))):
        raise TypeError('must be string type')
    if isinstance(text, text_type):
        return text.encode(encoding)
    return text

dataset/ETHPy150Open douban/brownant/brownant/utils.py to_bytes_safe/original