10 examples of 'python to_bytes' in Python

Every line of 'python to_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
41def to_bytes(data):
42 """Takes an input str or bytes object and returns an equivalent bytes object.
43
44 :param data: Input data
45 :type data: str or bytes
46 :returns: Data normalized to bytes
47 :rtype: bytes
48 """
49 if isinstance(data, six.string_types) and not isinstance(data, bytes):
50 return codecs.encode(data, TEXT_ENCODING)
51 return data
43def to_bytes(s):
44 if bytes != str:
45 if type(s) == str:
46 return s.encode('utf-8')
47 return s
95def to_bytes(data, encoding="UTF-8"):
96 """
97 Converts the given string to an array of bytes.
98 Returns the first parameter if it is already an array of bytes.
99
100 :param data: A unicode string
101 :param encoding: The encoding of data
102 :return: The corresponding array of bytes
103 """
104 if type(data) is bytes:
105 # Nothing to do
106 return data
107 return data.encode(encoding)
40def to_bytes(s):
41 if PY2:
42 if isinstance(s, unicode):
43 return s.encode('utf8')
44 return s
45 else:
46 if isinstance(s, str):
47 return s.encode('utf8')
48 return s
18def to_bytes(text):
19 if isinstance(text, string_type):
20 text = text.encode('utf-8')
21 return text
37def to_bytes(str_or_bytes, encoding=ENCODING):
38 if isinstance(str_or_bytes, str):
39 return str_or_bytes.encode(encoding)
40 return str_or_bytes
99def to_bytes(x, charset=sys.getdefaultencoding(), errors='strict'):
100 if x is None:
101 return None
102 if isinstance(x, (bytes, bytearray, buffer)):
103 return bytes(x)
104 if isinstance(x, unicode):
105 return x.encode(charset, errors)
106 raise TypeError('Expected bytes')
53def bytes(string, encoding=None):
54 """."""
55 return str(string)
104def strtobyte(s):
105 return bytes(s, encoding="ascii")
22def to_native_str(some_bytes):
23 return some_bytes

Related snippets