本文整理汇总了Python中mom.builtins.is_bytes函数的典型用法代码示例。如果您正苦于以下问题:Python is_bytes函数的具体用法?Python is_bytes怎么用?Python is_bytes使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了is_bytes函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: b85decode
def b85decode(encoded,
prefix=None,
suffix=None,
_base85_bytes=ASCII85_BYTES,
_base85_ords=ASCII85_ORDS,
_uncompact_zero=True,
_compact_char=ZERO_GROUP_CHAR):
"""
Decodes an ASCII85-encoded string into raw bytes.
:param encoded:
Encoded ASCII string.
:param prefix:
The prefix used by the encoded text. None by default.
:param suffix:
The suffix used by the encoded text. None by default.
:param _base85_bytes:
(Internal) Character set to use.
:param _base85_ords:
(Internal) A function to convert a base85 character to its ordinal
value. You should not need to use this.
:param _uncompact_zero:
(Internal) Treats 'z' (a zero-group (\x00\x00\x00\x00)) as a '!!!!!'
if ``True`` (default).
:param _compact_char:
(Internal) Character used to represent compact groups ('z' default)
:returns:
ASCII85-decoded raw bytes.
"""
prefix = prefix or EMPTY_BYTE
suffix = suffix or EMPTY_BYTE
if not (is_bytes(prefix) and is_bytes(suffix)):
raise TypeError(
"Prefix/suffix must be bytes: got prefix %r, %r" %
(type(prefix).__name__, type(suffix).__name__)
)
if not is_bytes(_compact_char):
raise TypeError("compat character must be raw byte: got %r" %
type(_compact_char).__name__)
if not is_bytes(encoded):
raise TypeError(
"Encoded sequence must be bytes: got %r" % type(encoded).__name__
)
# ASCII-85 ignores whitespace.
encoded = EMPTY_BYTE.join(encoded.split())
# Strip the prefix and suffix.
if prefix and encoded.startswith(prefix):
encoded = encoded[len(prefix):]
if suffix and encoded.endswith(suffix):
encoded = encoded[:-len(suffix)]
# Replace all the 'z' occurrences with '!!!!!'
if _uncompact_zero:
_check_compact_char_occurrence(encoded, _compact_char)
encoded = encoded.replace(_compact_char, EXCLAMATION_CHUNK)
return _b85decode_chunks(encoded, _base85_bytes, _base85_ords)
示例2: b85encode
def b85encode(raw_bytes,
prefix=None,
suffix=None,
_base85_bytes=ASCII85_BYTES,
_padding=False,
_compact_zero=True,
_compact_char=ZERO_GROUP_CHAR):
"""ASCII-85 encodes a sequence of raw bytes.
The character set in use is::
ASCII 33 ("!") to ASCII 117 ("u")
If the number of raw bytes is not divisible by 4, the byte sequence
is padded with up to 3 null bytes before encoding. After encoding,
as many bytes as were added as padding are removed from the end of the
encoded sequence if ``padding`` is ``False`` (default).
Encodes a zero-group (\x00\x00\x00\x00) as "z" instead of "!!!!!".
The resulting encoded ASCII string is *not URL-safe* nor is it
safe to include within SGML/XML/HTML documents. You will need to escape
special characters if you decide to include such an encoded string
within these documents.
:param raw_bytes:
Raw bytes.
:param prefix:
The prefix used by the encoded text. None by default.
:param suffix:
The suffix used by the encoded text. None by default.
:param _base85_bytes:
(Internal) Character set to use.
:param _compact_zero:
(Internal) Encodes a zero-group (\x00\x00\x00\x00) as "z" instead of
"!!!!!" if this is ``True`` (default).
:param _compact_char:
(Internal) Character used to represent compact groups ("z" default)
:returns:
ASCII-85 encoded bytes.
"""
prefix = prefix or EMPTY_BYTE
suffix = suffix or EMPTY_BYTE
if not (builtins.is_bytes(prefix) and builtins.is_bytes(suffix)):
raise TypeError("Prefix/suffix must be bytes: got prefix %r, %r" %
(type(prefix).__name__, type(suffix).__name__))
if not builtins.is_bytes(_compact_char):
raise TypeError("compat character must be raw byte: got %r" %
type(_compact_char).__name__)
if not builtins.is_bytes(raw_bytes):
raise TypeError("data must be raw bytes: got %r" %
type(raw_bytes).__name__)
# Encode into ASCII85 characters.
encoded = _b85encode_chunks(raw_bytes, _base85_bytes, _padding)
encoded = (encoded.replace(EXCLAMATION_CHUNK, _compact_char)
if _compact_zero else encoded)
return prefix + encoded + suffix
示例3: test_does_not_encode_bytes_or_None_to_utf8
def test_does_not_encode_bytes_or_None_to_utf8(self):
self.assertEqual(text.utf8_encode(None), None)
self.assertEqual(text.utf8_encode(constants.UTF8_BYTES), constants.UTF8_BYTES)
self.assertTrue(builtins.is_bytes(text.utf8_encode(constants.UTF8_BYTES)))
self.assertEqual(text.utf8_encode(constants.LATIN1_BYTES), constants.LATIN1_BYTES)
self.assertTrue(builtins.is_bytes(text.utf8_encode(constants.LATIN1_BYTES)))
self.assertEqual(text.utf8_encode(constants.UTF8_BYTES2), constants.UTF8_BYTES2)
self.assertTrue(builtins.is_bytes(text.utf8_encode(constants.UTF8_BYTES2)))
示例4: test_does_not_encode_bytes_or_None_to_utf8
def test_does_not_encode_bytes_or_None_to_utf8(self):
self.assertEqual(utf8_encode(None), None)
self.assertEqual(utf8_encode(utf8_bytes), utf8_bytes)
self.assertTrue(is_bytes(utf8_encode(utf8_bytes)))
self.assertEqual(utf8_encode(latin1_bytes), latin1_bytes)
self.assertTrue(is_bytes(utf8_encode(latin1_bytes)))
self.assertEqual(utf8_encode(utf8_bytes2), utf8_bytes2)
self.assertTrue(is_bytes(utf8_encode(utf8_bytes2)))
示例5: test_rejects_non_bytes
def test_rejects_non_bytes(self):
self.assertFalse(is_bytes(unicode_string))
self.assertFalse(is_bytes(unicode_string2))
self.assertFalse(is_bytes(False))
self.assertFalse(is_bytes(5))
self.assertFalse(is_bytes(None))
self.assertFalse(is_bytes([]))
self.assertFalse(is_bytes(()))
self.assertFalse(is_bytes([]))
self.assertFalse(is_bytes(object))
示例6: test_rejects_non_bytes
def test_rejects_non_bytes(self):
self.assertFalse(builtins.is_bytes(constants.UNICODE_STRING))
self.assertFalse(builtins.is_bytes(constants.UNICODE_STRING2))
self.assertFalse(builtins.is_bytes(False))
self.assertFalse(builtins.is_bytes(5))
self.assertFalse(builtins.is_bytes(None))
self.assertFalse(builtins.is_bytes([]))
self.assertFalse(builtins.is_bytes(()))
self.assertFalse(builtins.is_bytes([]))
self.assertFalse(builtins.is_bytes(object))
示例7: test_does_not_encode_else_to_utf8
def test_does_not_encode_else_to_utf8(self):
self.assertEqual(text.utf8_encode_if_unicode(constants.UTF8_BYTES), constants.UTF8_BYTES)
self.assertTrue(builtins.is_bytes(text.utf8_encode_if_unicode(constants.UTF8_BYTES)))
self.assertEqual(text.utf8_encode_if_unicode(constants.UTF8_BYTES2), constants.UTF8_BYTES2)
self.assertTrue(builtins.is_bytes(text.utf8_encode_if_unicode(constants.UTF8_BYTES2)))
self.assertEqual(text.utf8_encode_if_unicode(None), None)
self.assertEqual(text.utf8_encode_if_unicode(False), False)
self.assertEqual(text.utf8_encode_if_unicode(5), 5)
self.assertEqual(text.utf8_encode_if_unicode([]), [])
self.assertEqual(text.utf8_encode_if_unicode(()), ())
self.assertEqual(text.utf8_encode_if_unicode({}), {})
self.assertEqual(text.utf8_encode_if_unicode(object), object)
示例8: test_does_not_encode_else_to_utf8
def test_does_not_encode_else_to_utf8(self):
self.assertEqual(utf8_encode_if_unicode(utf8_bytes), utf8_bytes)
self.assertTrue(is_bytes(utf8_encode_if_unicode(utf8_bytes)))
self.assertEqual(utf8_encode_if_unicode(utf8_bytes2), utf8_bytes2)
self.assertTrue(is_bytes(utf8_encode_if_unicode(utf8_bytes2)))
self.assertEqual(utf8_encode_if_unicode(None), None)
self.assertEqual(utf8_encode_if_unicode(False), False)
self.assertEqual(utf8_encode_if_unicode(5), 5)
self.assertEqual(utf8_encode_if_unicode([]), [])
self.assertEqual(utf8_encode_if_unicode(()), ())
self.assertEqual(utf8_encode_if_unicode({}), {})
self.assertEqual(utf8_encode_if_unicode(object), object)
示例9: base_encode
def base_encode(raw_bytes, base, base_bytes, base_zero, padding=True):
"""
Encodes raw bytes given a base.
:param raw_bytes:
Raw bytes to encode.
:param base:
Unsigned integer base.
:param base_bytes:
The ASCII bytes used in the encoded string. "Character set" or "alphabet".
:param base_zero:
"""
if not is_bytes(raw_bytes):
raise TypeError("data must be raw bytes: got %r" %
type(raw_bytes).__name__)
number = bytes_to_uint(raw_bytes)
encoded = EMPTY_BYTE
while number > 0:
number, remainder = divmod(number, base)
encoded = base_bytes[remainder] + encoded
if padding:
zero_leading = bytes_leading(raw_bytes)
encoded = encoded.rjust(len(encoded) + zero_leading, base_zero)
return encoded
示例10: b62encode_naive
def b62encode_naive(raw_bytes,
base_bytes=base62.ASCII62_BYTES,
_padding=True,
_zero_byte=ZERO_BYTE):
"""
Base62 encodes a sequence of raw bytes. Zero-byte sequences are
preserved by default.
:param raw_bytes:
Raw bytes to encode.
:param base_bytes:
The character set to use. Defaults to ``ASCII62_CHARSET``
that uses natural ASCII order.
:param _padding:
(Internal) ``True`` (default) to include prefixed zero-byte sequence
padding converted to appropriate representation.
:returns:
Base-62 encoded bytes.
"""
if not builtins.is_bytes(raw_bytes):
raise TypeError("data must be raw bytes: got %r" %
type(raw_bytes).__name__)
number = integer.bytes_to_uint(raw_bytes)
encoded = EMPTY_BYTE
while number > 0:
encoded = base_bytes[number % 62] + encoded
number //= 62
# The following makes more divmod calls but is 2x faster.
# number, remainder = divmod(number, 62)
# encoded = _charset[remainder] + encoded
if _padding:
zero_leading = functional.leading(lambda w: w == _zero_byte[0], raw_bytes)
encoded = (base_bytes[0] * zero_leading) + encoded
return encoded
示例11: ipv6_b85decode_naive
def ipv6_b85decode_naive(encoded,
_base85_ords=base85.RFC1924_ORDS):
"""
Decodes an RFC1924 Base-85 encoded string to its 128-bit unsigned integral
representation. Used to base85-decode IPv6 addresses or 128-bit chunks.
Whitespace is ignored. Raises an ``OverflowError`` if stray characters
are found.
:param encoded:
RFC1924 Base85-encoded string.
:param _base85_ords:
(Internal) Look up table.
:returns:
A 128-bit unsigned integer.
"""
if not builtins.is_bytes(encoded):
raise TypeError("Encoded sequence must be bytes: got %r" %
type(encoded).__name__)
# Ignore whitespace.
encoded = EMPTY_BYTE.join(encoded.split())
if len(encoded) != 20:
raise ValueError("Not 20 encoded bytes: %r" % encoded)
uint128 = 0
try:
for char in encoded:
uint128 = uint128 * 85 + _base85_ords[char]
except KeyError:
raise OverflowError("Cannot decode `%r -- may contain stray "
"ASCII bytes" % encoded)
if uint128 > UINT128_MAX:
raise OverflowError("Cannot decode `%r` -- may contain stray "
"ASCII bytes" % encoded)
return uint128
示例12: bytes_to_unicode_recursive
def bytes_to_unicode_recursive(obj, encoding="utf-8"):
"""
Walks a simple data structure, converting byte strings to unicode.
Supports lists, tuples, and dictionaries.
:param obj:
The Python data structure to walk recursively looking for
byte strings.
:param encoding:
The encoding to use when decoding the byte string into Unicode.
Default UTF-8.
:returns:
obj with all the byte strings converted to Unicode strings.
"""
if isinstance(obj, dict):
return dict((bytes_to_unicode_recursive(k),
bytes_to_unicode_recursive(v)) for (k, v) in obj.items())
elif isinstance(obj, list):
return list(bytes_to_unicode_recursive(i) for i in obj)
elif isinstance(obj, tuple):
return tuple(bytes_to_unicode_recursive(i) for i in obj)
elif builtins.is_bytes(obj):
return bytes_to_unicode(obj, encoding=encoding)
else:
return obj
示例13: rfc1924_b85encode
def rfc1924_b85encode(raw_bytes,
_padding=False):
"""
Base85 encodes using the RFC1924 character set.
The character set is::
0–9, A–Z, a–z, and then !#$%&()*+-;<=>[email protected]^_`{|}~
These characters are specifically not included::
"',./:[]\\
This is the encoding method used by Mercurial (and git?) to generate
binary diffs, for example. They chose the IPv6 character set and encode
using the ASCII85 encoding method while not compacting zero-byte sequences.
:see: http://tools.ietf.org/html/rfc1924
:param raw_bytes:
Raw bytes.
:param _padding:
(Internal) Whether padding should be included in the encoded output.
(Default ``False``, which is usually what you want.)
:returns:
RFC1924 base85 encoded string.
"""
if not is_bytes(raw_bytes):
raise TypeError("data must be raw bytes: got %r" %
type(raw_bytes).__name__)
return _b85encode_chunks(raw_bytes, RFC1924_BYTES, _padding)
示例14: test_single_value_lists_are_not_flattened
def test_single_value_lists_are_not_flattened(self):
d = parse_qs("a=1&a=2&a=3&b=c")
for n, v in d.items():
self.assertTrue(is_bytes(n),
"Dictionary key is not bytes.")
self.assertTrue(isinstance(v, list),
"Dictionary value is not a list.")
示例15: data_uri_parse
def data_uri_parse(data_uri):
"""
Parses a data URI into raw bytes and metadata.
:param data_uri:
The data url string.
If a mime-type definition is missing in the metadata,
"text/plain;charset=US-ASCII" will be used as default mime-type.
:returns:
A 2-tuple::
(bytes, mime_type)
See :func:`mom.http.mimeparse.mimeparse.parse_mime_type` for what ``mime_type``
looks like.
"""
if not builtins.is_bytes(data_uri):
raise TypeError("data URIs must be ASCII-encoded bytes: got %r" %
type(data_uri).__name__)
metadata, encoded = data_uri.rsplit(b(","), 1)
_, metadata = metadata.split(b("data:"), 1)
parts = metadata.rsplit(b(";"), 1)
if parts[-1] == b("base64"):
decode = codec.base64_decode
parts = parts[:-1]
else:
decode = unquote
if not parts or not parts[0]:
parts = [b("text/plain;charset=US-ASCII")]
mime_type = mimeparse.parse_mime_type(parts[0])
raw_bytes = decode(encoded)
return raw_bytes, mime_type