How to use 'python from_bytes' in Python

Every line of 'python from_bytes' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
109def from_bytes(value):
110 """Converts bytes to a string value, if necessary.
111
112 Args:
113 value (Union[str, bytes]): The value to be converted.
114
115 Returns:
116 str: The original value converted to unicode (if bytes) or as passed in
117 if it started out as unicode.
118
119 Raises:
120 ValueError: If the value could not be converted to unicode.
121 """
122 result = value.decode("utf-8") if isinstance(value, six.binary_type) else value
123 if isinstance(result, six.text_type):
124 return result
125 else:
126 raise ValueError("{0!r} could not be converted to unicode".format(value))

Related snippets