當前位置: 首頁>>代碼示例>>Python>>正文


Python binascii.b2a_qp方法代碼示例

本文整理匯總了Python中binascii.b2a_qp方法的典型用法代碼示例。如果您正苦於以下問題:Python binascii.b2a_qp方法的具體用法?Python binascii.b2a_qp怎麽用?Python binascii.b2a_qp使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在binascii的用法示例。


在下文中一共展示了binascii.b2a_qp方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: set_bytes_content

# 需要導入模塊: import binascii [as 別名]
# 或者: from binascii import b2a_qp [as 別名]
def set_bytes_content(msg, data, maintype, subtype, cte='base64',
                     disposition=None, filename=None, cid=None,
                     params=None, headers=None):
    _prepare_set(msg, maintype, subtype, headers)
    if cte == 'base64':
        data = _encode_base64(data, max_line_length=msg.policy.max_line_length)
    elif cte == 'quoted-printable':
        # XXX: quoprimime.body_encode won't encode newline characters in data,
        # so we can't use it.  This means max_line_length is ignored.  Another
        # bug to fix later.  (Note: encoders.quopri is broken on line ends.)
        data = binascii.b2a_qp(data, istext=False, header=False, quotetabs=True)
        data = data.decode('ascii')
    elif cte == '7bit':
        # Make sure it really is only ASCII.  The early warning here seems
        # worth the overhead...if you care write your own content manager :).
        data.encode('ascii')
    elif cte in ('8bit', 'binary'):
        data = data.decode('ascii', 'surrogateescape')
    msg.set_payload(data)
    msg['Content-Transfer-Encoding'] = cte
    _finalize_set(msg, disposition, filename, cid, params) 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:23,代碼來源:contentmanager.py

示例2: write

# 需要導入模塊: import binascii [as 別名]
# 或者: from binascii import b2a_qp [as 別名]
def write(self, chunk: bytes) -> None:
        if self._compress is not None:
            if chunk:
                chunk = self._compress.compress(chunk)
                if not chunk:
                    return

        if self._encoding == 'base64':
            buf = self._encoding_buffer
            assert buf is not None
            buf.extend(chunk)

            if buf:
                div, mod = divmod(len(buf), 3)
                enc_chunk, self._encoding_buffer = (
                    buf[:div * 3], buf[div * 3:])
                if enc_chunk:
                    b64chunk = base64.b64encode(enc_chunk)
                    await self._writer.write(b64chunk)
        elif self._encoding == 'quoted-printable':
            await self._writer.write(binascii.b2a_qp(chunk))
        else:
            await self._writer.write(chunk) 
開發者ID:TouwaStar,項目名稱:Galaxy_Plugin_Bethesda,代碼行數:25,代碼來源:multipart.py

示例3: test_qp

# 需要導入模塊: import binascii [as 別名]
# 或者: from binascii import b2a_qp [as 別名]
def test_qp(self):
        # A test for SF bug 534347 (segfaults without the proper fix)
        try:
            binascii.a2b_qp("", **{1:1})
        except TypeError:
            pass
        else:
            self.fail("binascii.a2b_qp(**{1:1}) didn't raise TypeError")
        self.assertEqual(binascii.a2b_qp("= "), "= ")
        self.assertEqual(binascii.a2b_qp("=="), "=")
        self.assertEqual(binascii.a2b_qp("=AX"), "=AX")
        self.assertRaises(TypeError, binascii.b2a_qp, foo="bar")
        self.assertEqual(binascii.a2b_qp("=00\r\n=00"), "\x00\r\n\x00")
        self.assertEqual(
            binascii.b2a_qp("\xff\r\n\xff\n\xff"),
            "=FF\r\n=FF\r\n=FF"
        )
        self.assertEqual(
            binascii.b2a_qp("0"*75+"\xff\r\n\xff\r\n\xff"),
            "0"*75+"=\r\n=FF\r\n=FF\r\n=FF"
        ) 
開發者ID:ofermend,項目名稱:medicare-demo,代碼行數:23,代碼來源:test_binascii.py

示例4: write

# 需要導入模塊: import binascii [as 別名]
# 或者: from binascii import b2a_qp [as 別名]
def write(self, chunk):
        if self._compress is not None:
            if chunk:
                chunk = self._compress.compress(chunk)
                if not chunk:
                    return

        if self._encoding == 'base64':
            self._encoding_buffer.extend(chunk)

            if self._encoding_buffer:
                buffer = self._encoding_buffer
                div, mod = divmod(len(buffer), 3)
                enc_chunk, self._encoding_buffer = (
                    buffer[:div * 3], buffer[div * 3:])
                if enc_chunk:
                    enc_chunk = base64.b64encode(enc_chunk)
                    yield from self._writer.write(enc_chunk)
        elif self._encoding == 'quoted-printable':
            yield from self._writer.write(binascii.b2a_qp(chunk))
        else:
            yield from self._writer.write(chunk) 
開發者ID:skylander86,項目名稱:lambda-text-extractor,代碼行數:24,代碼來源:multipart.py

示例5: encodestring

# 需要導入模塊: import binascii [as 別名]
# 或者: from binascii import b2a_qp [as 別名]
def encodestring(s, quotetabs = 0, header = 0):
    if b2a_qp is not None:
        return b2a_qp(s, quotetabs = quotetabs, header = header)
    from cStringIO import StringIO
    infp = StringIO(s)
    outfp = StringIO()
    encode(infp, outfp, quotetabs, header)
    return outfp.getvalue() 
開發者ID:glmcdona,項目名稱:meddle,代碼行數:10,代碼來源:quopri.py

示例6: test_qp

# 需要導入模塊: import binascii [as 別名]
# 或者: from binascii import b2a_qp [as 別名]
def test_qp(self):
        # A test for SF bug 534347 (segfaults without the proper fix)
        try:
            binascii.a2b_qp("", **{1:1})
        except TypeError:
            pass
        else:
            self.fail("binascii.a2b_qp(**{1:1}) didn't raise TypeError")
        self.assertEqual(binascii.a2b_qp("= "), "= ")
        self.assertEqual(binascii.a2b_qp("=="), "=")
        self.assertEqual(binascii.a2b_qp("=AX"), "=AX")
        self.assertRaises(TypeError, binascii.b2a_qp, foo="bar")
        self.assertEqual(binascii.a2b_qp("=00\r\n=00"), "\x00\r\n\x00")
        self.assertEqual(
            binascii.b2a_qp("\xff\r\n\xff\n\xff"),
            "=FF\r\n=FF\r\n=FF"
        )
        self.assertEqual(
            binascii.b2a_qp("0"*75+"\xff\r\n\xff\r\n\xff"),
            "0"*75+"=\r\n=FF\r\n=FF\r\n=FF"
        )

        self.assertEqual(binascii.b2a_qp('\0\n'), '=00\n')
        self.assertEqual(binascii.b2a_qp('\0\n', quotetabs=True), '=00\n')
        self.assertEqual(binascii.b2a_qp('foo\tbar\t\n'), 'foo\tbar=09\n')
        self.assertEqual(binascii.b2a_qp('foo\tbar\t\n', quotetabs=True), 'foo=09bar=09\n')

        self.assertEqual(binascii.b2a_qp('.'), '=2E')
        self.assertEqual(binascii.b2a_qp('.\n'), '=2E\n')
        self.assertEqual(binascii.b2a_qp('a.\n'), 'a.\n') 
開發者ID:dxwu,項目名稱:BinderFilter,代碼行數:32,代碼來源:test_binascii.py

示例7: _ConvertAttributeValueToDict

# 需要導入模塊: import binascii [as 別名]
# 或者: from binascii import b2a_qp [as 別名]
def _ConvertAttributeValueToDict(cls, attribute_value):
    """Converts an attribute value into a JSON dictionary.

    Args:
      attribute_value (object): an attribute value.

    Returns:
      dict|list: The JSON serialized object which can be a dictionary or a list.
    """
    if isinstance(attribute_value, bytes):
      encoded_value = binascii.b2a_qp(attribute_value)
      encoded_value = codecs.decode(encoded_value, 'ascii')
      attribute_value = {
          '__type__': 'bytes',
          'stream': '{0:s}'.format(encoded_value)
      }

    elif isinstance(attribute_value, (list, tuple)):
      json_list = []
      for list_element in attribute_value:
        json_dict = cls._ConvertAttributeValueToDict(list_element)
        json_list.append(json_dict)

      if isinstance(attribute_value, list):
        attribute_value = json_list
      else:
        attribute_value = {
            '__type__': 'tuple',
            'values': json_list
        }

    elif isinstance(attribute_value, collections.Counter):
      attribute_value = cls._ConvertCollectionsCounterToDict(attribute_value)

    elif isinstance(attribute_value, dfvfs_path_spec.PathSpec):
      attribute_value = cls._ConvertPathSpecToDict(attribute_value)

    elif isinstance(attribute_value, containers_interface.AttributeContainer):
      attribute_value = cls._ConvertAttributeContainerToDict(attribute_value)

    return attribute_value 
開發者ID:log2timeline,項目名稱:plaso,代碼行數:43,代碼來源:json_serializer.py

示例8: encodestring

# 需要導入模塊: import binascii [as 別名]
# 或者: from binascii import b2a_qp [as 別名]
def encodestring(s, quotetabs=False, header=False):
    if b2a_qp is not None:
        return b2a_qp(s, quotetabs=quotetabs, header=header)
    from io import BytesIO
    infp = BytesIO(s)
    outfp = BytesIO()
    encode(infp, outfp, quotetabs, header)
    return outfp.getvalue() 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:10,代碼來源:quopri.py

示例9: test_qp

# 需要導入模塊: import binascii [as 別名]
# 或者: from binascii import b2a_qp [as 別名]
def test_qp(self):
        binascii.a2b_qp(data=b"", header=False)  # Keyword arguments allowed

        # A test for SF bug 534347 (segfaults without the proper fix)
        try:
            binascii.a2b_qp(b"", **{1:1})
        except TypeError:
            pass
        else:
            self.fail("binascii.a2b_qp(**{1:1}) didn't raise TypeError")

        self.assertEqual(binascii.a2b_qp(b"= "), b"= ")
        self.assertEqual(binascii.a2b_qp(b"=="), b"=")
        self.assertEqual(binascii.a2b_qp(b"=AX"), b"=AX")
        self.assertRaises(TypeError, binascii.b2a_qp, foo="bar")
        self.assertEqual(binascii.a2b_qp(b"=00\r\n=00"), b"\x00\r\n\x00")
        self.assertEqual(
            binascii.b2a_qp(b"\xff\r\n\xff\n\xff"),
            b"=FF\r\n=FF\r\n=FF")
        self.assertEqual(
            binascii.b2a_qp(b"0"*75+b"\xff\r\n\xff\r\n\xff"),
            b"0"*75+b"=\r\n=FF\r\n=FF\r\n=FF")

        self.assertEqual(binascii.b2a_qp(b'\0\n'), b'=00\n')
        self.assertEqual(binascii.b2a_qp(b'\0\n', quotetabs=True), b'=00\n')
        self.assertEqual(binascii.b2a_qp(b'foo\tbar\t\n'), b'foo\tbar=09\n')
        self.assertEqual(binascii.b2a_qp(b'foo\tbar\t\n', quotetabs=True),
                         b'foo=09bar=09\n')

        self.assertEqual(binascii.b2a_qp(b'.'), b'=2E')
        self.assertEqual(binascii.b2a_qp(b'.\n'), b'=2E\n')
        self.assertEqual(binascii.b2a_qp(b'a.\n'), b'a.\n') 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:34,代碼來源:test_binascii.py

示例10: process_buffers_for_display

# 需要導入模塊: import binascii [as 別名]
# 或者: from binascii import b2a_qp [as 別名]
def process_buffers_for_display(s, limit=40):
    """Process a buffer for human-readable display.

    This function performs the following operation on each of the buffers in `s`.
      1. Truncate input buffer if the length of the buffer is greater than
         `limit`, to prevent large strings from overloading the frontend.
      2. Apply `binascii.b2a_qp` on the truncated buffer to make the buffer
         printable and convertible to JSON.
      3. If truncation happened (in step 1), append a string at the end
         describing the original length and the truncation.

    Args:
      s: The buffer to be processed, either a single buffer or a nested array of
        them.
      limit: Length limit for each buffer, beyond which truncation will occur.

    Return:
      A single processed buffer or a nested array of processed buffers.
    """
    if isinstance(s, (list, tuple)):
        return [process_buffers_for_display(elem, limit=limit) for elem in s]
    else:
        length = len(s)
        if length > limit:
            return binascii.b2a_qp(
                s[:limit]
            ) + b" (length-%d truncated at %d bytes)" % (length, limit)
        else:
            return binascii.b2a_qp(s) 
開發者ID:tensorflow,項目名稱:tensorboard,代碼行數:31,代碼來源:tensor_helper.py

示例11: testBinaryScalarBelowLimit

# 需要導入模塊: import binascii [as 別名]
# 或者: from binascii import b2a_qp [as 別名]
def testBinaryScalarBelowLimit(self):
        x = b"\x01\x02\x03"
        self.assertEqual(
            binascii.b2a_qp(x), tensor_helper.process_buffers_for_display(x, 10)
        ) 
開發者ID:tensorflow,項目名稱:tensorboard,代碼行數:7,代碼來源:tensor_helper_test.py

示例12: testBinaryScalarAboveLimit

# 需要導入模塊: import binascii [as 別名]
# 或者: from binascii import b2a_qp [as 別名]
def testBinaryScalarAboveLimit(self):
        x = b"\x01\x02\x03"
        self.assertEqual(
            binascii.b2a_qp(x[:2]) + b" (length-3 truncated at 2 bytes)",
            tensor_helper.process_buffers_for_display(x, 2),
        ) 
開發者ID:tensorflow,項目名稱:tensorboard,代碼行數:8,代碼來源:tensor_helper_test.py


注:本文中的binascii.b2a_qp方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。