• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Python builtins.is_bytes函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中mom.builtins.is_bytes函数的典型用法代码示例。如果您正苦于以下问题:Python is_bytes函数的具体用法?Python is_bytes怎么用?Python is_bytes使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了is_bytes函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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)
开发者ID:RoboTeddy,项目名称:mom,代码行数:60,代码来源:base85.py


示例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
开发者ID:blightzero,项目名称:SO-Fi,代码行数:58,代码来源:base85.py


示例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)))
开发者ID:blightzero,项目名称:SO-Fi,代码行数:10,代码来源:test_mom_codec_text.py


示例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)))
开发者ID:RoboTeddy,项目名称:mom,代码行数:10,代码来源:test_mom_codec_text.py


示例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))
开发者ID:RoboTeddy,项目名称:mom,代码行数:10,代码来源:test_mom_builtins.py


示例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))
开发者ID:rajeshvv,项目名称:mom,代码行数:10,代码来源:test_mom_builtins.py


示例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)
开发者ID:blightzero,项目名称:SO-Fi,代码行数:14,代码来源:test_mom_codec_text.py


示例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)
开发者ID:RoboTeddy,项目名称:mom,代码行数:14,代码来源:test_mom_codec_text.py


示例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
开发者ID:RoboTeddy,项目名称:mom,代码行数:25,代码来源:_base.py


示例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
开发者ID:blightzero,项目名称:SO-Fi,代码行数:34,代码来源:_alt_base.py


示例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
开发者ID:blightzero,项目名称:SO-Fi,代码行数:34,代码来源:_alt_base.py


示例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
开发者ID:blightzero,项目名称:SO-Fi,代码行数:26,代码来源:text.py


示例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)
开发者ID:RoboTeddy,项目名称:mom,代码行数:30,代码来源:base85.py


示例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.")
开发者ID:Eah300muse,项目名称:pyoauth,代码行数:7,代码来源:test_pyoauth_url.py


示例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
开发者ID:blightzero,项目名称:SO-Fi,代码行数:31,代码来源:data_uri.py


示例16: ipv6_b85decode

def ipv6_b85decode(encoded,
                   _base85_ords=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.

    :param encoded:
        RFC1924 Base85-encoded string.
    :param _base85_ords:
        (Internal) Look up table.
    :param _whitespace:
        (Internal) Whitespace characters.
    :returns:
        A 128-bit unsigned integer.
    """
    if not is_bytes(encoded):
        raise TypeError(
            "Encoded sequence must be bytes: got %r" % type(encoded).__name__
        )
    if len(encoded) != 20:
        raise ValueError(
            "Encoded IPv6 value must be exactly 20 characters long: got %r" %
            encoded
        )
    #uint128 = 0L
    #for char in encoded:
    #    uint128 = uint128 * 85 + _base85_ords[char]
    # Above loop unrolled to process 4 5-tuple chunks instead:
    #v, w, x, y, z = encoded[0:5]
    # v = encoded[0]..z = encoded[4]
    uint128 = ((((_base85_ords[encoded[0]] *
                85 + _base85_ords[encoded[1]]) *
                85 + _base85_ords[encoded[2]]) *
                85 + _base85_ords[encoded[3]]) *
                85 + _base85_ords[encoded[4]])
    #v, w, x, y, z = encoded[5:10]
    # v = encoded[5]..z = encoded[9]
    uint128 = (((((uint128 * 85 + _base85_ords[encoded[5]]) *
                85 + _base85_ords[encoded[6]]) *
                85 + _base85_ords[encoded[7]]) *
                85 + _base85_ords[encoded[8]]) *
                85 + _base85_ords[encoded[9]])
    #v, w, x, y, z = encoded[10:15]
    # v = encoded[10]..z = encoded[14]
    uint128 = (((((uint128 * 85 + _base85_ords[encoded[10]]) *
                85 + _base85_ords[encoded[11]]) *
                85 + _base85_ords[encoded[12]]) *
                85 + _base85_ords[encoded[13]]) *
                85 + _base85_ords[encoded[14]])
    #v, w, x, y, z = encoded[15:20]
    # v = encoded[15]..z = encoded[19]
    uint128 = (((((uint128 * 85 + _base85_ords[encoded[15]]) *
                85 + _base85_ords[encoded[16]]) *
                85 + _base85_ords[encoded[17]]) *
                85 + _base85_ords[encoded[18]]) *
                85 + _base85_ords[encoded[19]])
    return uint128
开发者ID:AlonDaks,项目名称:glass-app-backend,代码行数:57,代码来源:base85.py


示例17: base_decode

def base_decode(encoded, base, base_ords, base_zero, powers):
  """Decode from base to base 256."""
  if not is_bytes(encoded):
    raise TypeError("encoded data must be bytes: got %r" %
                    type(encoded).__name__)
    # Ignore whitespace.
  encoded = EMPTY_BYTE.join(encoded.split())
  # Convert to big integer.
  number = base_to_uint(encoded, base, base_ords, powers)
  return uint_to_base256(number, encoded, base_zero)
开发者ID:RoboTeddy,项目名称:mom,代码行数:10,代码来源:_base.py


示例18: json_decode

def json_decode(encoded):
  """
  Decodes a JSON string into its equivalent Python value.

  :param encoded:
      JSON string.
  :returns:
      Decoded Python value.
  """
  if builtins.is_bytes(encoded):
    raise TypeError("Cannot work with bytes.")
  return _json_compat.json_loads(encoded)
开发者ID:blightzero,项目名称:SO-Fi,代码行数:12,代码来源:json.py


示例19: hex_decode

def hex_decode(encoded):
  """
  Decodes hexadecimal-encoded bytes into raw bytes.

  :param encoded:
      Hex representation.
  :returns:
      Raw bytes.
  """
  if not is_bytes(encoded):
    raise TypeError("argument must be bytes: got %r" %
                    type(encoded).__name__)
  return binascii.a2b_hex(encoded)
开发者ID:RoboTeddy,项目名称:mom,代码行数:13,代码来源:__init__.py


示例20: base64_decode

def base64_decode(encoded):
  """
  Decodes base64-encoded bytes into raw bytes. Not URL-safe.

  :param encoded:
      Base-64 encoded representation.
  :returns:
      Raw bytes.
  """
  if not is_bytes(encoded):
    raise TypeError("argument must be bytes: got %r" %
                    type(encoded).__name__)
  return binascii.a2b_base64(encoded)
开发者ID:RoboTeddy,项目名称:mom,代码行数:13,代码来源:__init__.py



注:本文中的mom.builtins.is_bytes函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python moment.date函数代码示例发布时间:2022-05-27
下一篇:
Python builtins.b函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap