当前位置: 首页>>代码示例>>Python>>正文


Python six.binary_type函数代码示例

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


在下文中一共展示了binary_type函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_serialize

 def test_serialize(self):
     buf = self.dst.serialize()
     res = struct.unpack_from(self.form, six.binary_type(buf))
     eq_(self.nxt, res[0])
     eq_(self.size, res[1])
     offset = struct.calcsize(self.form)
     opt1 = ipv6.option.parser(six.binary_type(buf[offset:]))
     offset += len(opt1)
     opt2 = ipv6.option.parser(six.binary_type(buf[offset:]))
     offset += len(opt2)
     opt3 = ipv6.option.parser(six.binary_type(buf[offset:]))
     offset += len(opt3)
     opt4 = ipv6.option.parser(six.binary_type(buf[offset:]))
     eq_(5, opt1.type_)
     eq_(2, opt1.len_)
     eq_(b'\x00\x00', opt1.data)
     eq_(1, opt2.type_)
     eq_(0, opt2.len_)
     eq_(None, opt2.data)
     eq_(0xc2, opt3.type_)
     eq_(4, opt3.len_)
     eq_(b'\x00\x01\x00\x00', opt3.data)
     eq_(1, opt4.type_)
     eq_(0, opt4.len_)
     eq_(None, opt4.data)
开发者ID:Aries-Sushi,项目名称:ryu,代码行数:25,代码来源:test_ipv6.py

示例2: _get_client_by_tier

    def _get_client_by_tier(self):
        """Get a client that uses ServiceRouter"""
        config = self.config
        serviceRouter = ServiceRouter()

        overrides = ConnConfigs()
        for key, val in six.iteritems(config.conn_configs):
            key = six.binary_type(key)
            val = six.binary_type(val)
            overrides[key] = val

        sr_options = ServiceOptions()
        for key, val in six.iteritems(config.service_options):
            key = six.binary_type(key)
            if not isinstance(val, list):
                raise TypeError("Service option %s expected list; got %s (%s)"
                                % (key, val, type(val)))
            val = [six.binary_type(elem) for elem in val]
            sr_options[key] = val

        service_name = config.tier

        # Obtain a normal client connection using SR2
        client = serviceRouter.getClient2(self.client_class,
                                          service_name, sr_options,
                                          overrides, False)

        if client is None:
            raise NameError('Failed to lookup host for tier %s' % service_name)

        return client
开发者ID:ConfusedReality,项目名称:pkg_serialization_fbthrift,代码行数:31,代码来源:fuzzer.py

示例3: combine_uuids

def combine_uuids(uuids, ordered=True, salt=''):
    """
    Creates a uuid that specifies a group of UUIDS

    Args:
        uuids (list): list of uuid objects
        ordered (bool): if False uuid order changes the resulting combined uuid
            otherwise the uuids are considered an orderless set
        salt (str): salts the resulting hash

    Returns:
        uuid.UUID: combined uuid

    CommandLine:
        python -m utool.util_hash --test-combine_uuids

    Example:
        >>> # ENABLE_DOCTEST
        >>> from utool.util_hash import *  # NOQA
        >>> import utool as ut
        >>> uuids = [hashable_to_uuid('one'), hashable_to_uuid('two'),
        >>>          hashable_to_uuid('three')]
        >>> combo1 = combine_uuids(uuids, ordered=True)
        >>> combo2 = combine_uuids(uuids[::-1], ordered=True)
        >>> combo3 = combine_uuids(uuids, ordered=False)
        >>> combo4 = combine_uuids(uuids[::-1], ordered=False)
        >>> result = ut.repr4([combo1, combo2, combo3, combo4], nobr=True)
        >>> print(result)
        UUID('83ee781f-8646-ccba-0ed8-13842825c12a'),
        UUID('52bbb33f-612e-2ab8-a62c-2f46e5b1edc8'),
        UUID('945cadab-e834-e581-0f74-62f106d20d81'),
        UUID('945cadab-e834-e581-0f74-62f106d20d81'),

    Example:
        >>> # ENABLE_DOCTEST
        >>> from utool.util_hash import *  # NOQA
        >>> import utool as ut
        >>> uuids = [uuid.UUID('5ff6b34e-7d8f-ef32-5fad-489266acd2ae'),
        >>>          uuid.UUID('f2400146-ec12-950b-1489-668228e155a8'),
        >>>          uuid.UUID('037d6f31-8c73-f961-1fe4-d616442a1e86'),
        >>>          uuid.UUID('ca45d6e2-e648-09cc-a49e-e71c6fa3b3f3')]
        >>> ordered = True
        >>> salt = u''
        >>> result = combine_uuids(uuids, ordered, salt)
        >>> print(result)
        1dabc66b-b564-676a-99b4-5cae7a9e7294
    """
    if len(uuids) == 0:
        return get_zero_uuid()
    elif len(uuids) == 1:
        return uuids[0]
    else:
        if not ordered:
            uuids = sorted(uuids)
        sep_str = '-'
        sep_byte = six.binary_type(six.b(sep_str))
        pref = six.binary_type(six.b('{}{}{}'.format(salt, sep_str, len(uuids))))
        combined_bytes = pref + sep_byte.join([u.bytes for u in uuids])
        combined_uuid = hashable_to_uuid(combined_bytes)
        return combined_uuid
开发者ID:Erotemic,项目名称:utool,代码行数:60,代码来源:util_hash.py

示例4: test_default_args

    def test_default_args(self):
        ip = ipv6.ipv6()
        buf = ip.serialize(bytearray(), None)
        res = struct.unpack(ipv6.ipv6._PACK_STR, six.binary_type(buf))

        eq_(res[0], 6 << 28)
        eq_(res[1], 0)
        eq_(res[2], 6)
        eq_(res[3], 255)
        eq_(res[4], addrconv.ipv6.text_to_bin('10::10'))
        eq_(res[5], addrconv.ipv6.text_to_bin('20::20'))

        # with extension header
        ip = ipv6.ipv6(
            nxt=0, ext_hdrs=[
                ipv6.hop_opts(58, 0, [
                    ipv6.option(5, 2, b'\x00\x00'),
                    ipv6.option(1, 0, None)])])
        buf = ip.serialize(bytearray(), None)
        res = struct.unpack(ipv6.ipv6._PACK_STR + '8s', six.binary_type(buf))

        eq_(res[0], 6 << 28)
        eq_(res[1], 8)
        eq_(res[2], 0)
        eq_(res[3], 255)
        eq_(res[4], addrconv.ipv6.text_to_bin('10::10'))
        eq_(res[5], addrconv.ipv6.text_to_bin('20::20'))
        eq_(res[6], b'\x3a\x00\x05\x02\x00\x00\x01\x00')
开发者ID:Aries-Sushi,项目名称:ryu,代码行数:28,代码来源:test_ipv6.py

示例5: getBytesStream

 def getBytesStream(self):
     "Render request String, but as bytes, which may contain invalid stuff"
     ustr = six.text_type(self)
     # from there we might be able to extract it as full ascii
     try:
         out = ustr.encode('ascii')
     except UnicodeEncodeError:
         # If we try to use characters which transletes to multy bytes chars
         # we'll need to fix the size computations.
         # There's a system in place which allows perfect binary crap for
         # multibytes using BYTES_SPECIAL_REPLACE & record_bytes_to_send,
         # this allows strange multibytes that a regular .encode('utf-8')
         # would not allow.
         raise BadEncodingException('Our requests should only '
                                    'use ascii or binary specific tricks.')
     # Now replace the strange bytes
     for sbyte in self._strange_bytes_records:
         if six.PY2:
             out = out.replace(six.binary_type(Tools.BYTES_SPECIAL_REPLACE),
                               sbyte,
                               1)
         else:
             out = out.replace(six.binary_type(Tools.BYTES_SPECIAL_REPLACE,
                                               'ascii'),
                               sbyte,
                               1)
     return out
开发者ID:regilero,项目名称:HTTPWookiee,代码行数:27,代码来源:request.py

示例6: serialize_HarResponse_from_Exception

    def serialize_HarResponse_from_Exception(self, io, raw):
        err = self.get_EnvironmentError_from_Exception(raw)

        status = str(err.errno)
        if isinstance(status, six.text_type):
            status = status.encode('utf-8')
        reason = err.strerror
        if isinstance(reason, six.text_type):
            reason = reason.encode('utf-8')

        # usually 4 tabs, omitted
        io.write(b'"response": {\n')
        io.write(b'\t"status": ' + six.binary_type(err.errno) + b',\n')
        io.write(b'\t"statusText": "' +
                 six.binary_type(err.strerror) + b'",\n')
        io.write(b'\t"httpVersion": "HTTP/1.1",\n')
        io.write(b'\t"cookies": [\n')
        io.write(b'\t],\n')
        io.write(b'\t"headers": [\n')
        io.write(b'\t],\n')
        io.write(b'\t"content": {\n')
        io.write(b'\t\t"mimeType": "application/x-error",\n')
        io.write(b'\t\t"size": -1,\n')
        io.write(b'\t\t"text": ')
        io.write(six.binary_type(json.dumps(repr(raw))))
        io.write(b'\n\t},\n')

        io.write(b'\t"redirectURL": "' +
                 six.binary_type(err.filename) + '",\n')
        io.write(b'\t"headersSize": -1,\n')
        io.write(b'\t"bodySize": -1\n')
        io.write(b'},\n')
开发者ID:1T,项目名称:harlib,代码行数:32,代码来源:requests.py

示例7: test_relay_id_missmatch_response

    def test_relay_id_missmatch_response(self):
        data = {
            'public_key': six.binary_type(self.public_key),
            'relay_id': self.relay_id,
        }

        raw_json, signature = self.private_key.pack(data)

        resp = self.client.post(
            self.path,
            data=raw_json,
            content_type='application/json',
            HTTP_X_SENTRY_RELAY_ID=self.relay_id,
            HTTP_X_SENTRY_RELAY_SIGNATURE=signature,
        )

        assert resp.status_code == 200, resp.content
        result = json.loads(resp.content)

        raw_json, signature = self.private_key.pack(result)

        resp = self.client.post(
            reverse(
                'sentry-api-0-relay-register-response'
            ),
            data=raw_json,
            content_type='application/json',
            HTTP_X_SENTRY_RELAY_ID=six.binary_type(uuid4()),
            HTTP_X_SENTRY_RELAY_SIGNATURE=signature,
        )

        assert resp.status_code == 400, resp.content
开发者ID:Kayle009,项目名称:sentry,代码行数:32,代码来源:test_relay_register.py

示例8: _test_set_vlan_vid_none

    def _test_set_vlan_vid_none(self):
        header = ofproto.OXM_OF_VLAN_VID
        match = OFPMatch()
        match.set_vlan_vid_none()
        value = ofproto.OFPVID_NONE
        cls_ = OFPMatchField._FIELDS_HEADERS.get(header)
        pack_str = cls_.pack_str.replace('!', '')
        fmt = '!HHI' + pack_str

        # serialize
        buf = bytearray()
        length = match.serialize(buf, 0)
        eq_(length, len(buf))

        res = list(unpack_from(fmt, six.binary_type(buf), 0)[3:])
        res_value = res.pop(0)
        eq_(res_value, value)

        # parser
        res = match.parser(six.binary_type(buf), 0)
        eq_(res.type, ofproto.OFPMT_OXM)
        eq_(res.fields[0].header, header)
        eq_(res.fields[0].value, value)

        # to_jsondict
        jsondict = match.to_jsondict()

        # from_jsondict
        match2 = match.from_jsondict(jsondict["OFPMatch"])
        buf2 = bytearray()
        match2.serialize(buf2, 0)
        eq_(str(match), str(match2))
        eq_(buf, buf2)
开发者ID:5g-empower,项目名称:empower-ryu,代码行数:33,代码来源:test_parser_v13.py

示例9: test_init_automatic_pad

  def test_init_automatic_pad(self):
    """
    Addresses are automatically padded to 81 trytes.
    """
    addy = Address(
      b'JVMTDGDPDFYHMZPMWEKKANBQSLSDTIIHAYQUMZOK'
      b'HXXXGJHJDQPOMDOMNRDKYCZRUFZROZDADTHZC'
    )

    self.assertEqual(
      binary_type(addy),

      # Note the extra 9's added to the end.
      b'JVMTDGDPDFYHMZPMWEKKANBQSLSDTIIHAYQUMZOK'
      b'HXXXGJHJDQPOMDOMNRDKYCZRUFZROZDADTHZC9999',
    )

    # This attribute will make more sense once we start working with
    # address checksums.
    self.assertEqual(
      binary_type(addy.address),

      b'JVMTDGDPDFYHMZPMWEKKANBQSLSDTIIHAYQUMZOK'
      b'HXXXGJHJDQPOMDOMNRDKYCZRUFZROZDADTHZC9999',
    )

    # Checksum is not generated automatically.
    self.assertIsNone(addy.checksum)
开发者ID:grendias,项目名称:iota.lib.py,代码行数:28,代码来源:types_test.py

示例10: test_init_with_checksum

  def test_init_with_checksum(self):
    """
    Creating an address with checksum already attached.
    """
    addy = Address(
      b'RVORZ9SIIP9RCYMREUIXXVPQIPHVCNPQ9HZWYKFWYWZRE'
      b'9JQKG9REPKIASHUUECPSQO9JT9XNMVKWYGVAFOXM9MUBX'
    )

    self.assertEqual(
      binary_type(addy),

      b'RVORZ9SIIP9RCYMREUIXXVPQIPHVCNPQ9HZWYKFWYWZRE'
      b'9JQKG9REPKIASHUUECPSQO9JT9XNMVKWYGVAFOXM9MUBX',
    )

    self.assertEqual(
      binary_type(addy.address),

      b'RVORZ9SIIP9RCYMREUIXXVPQIPHVCNPQ9HZWYKFWYWZRE'
      b'9JQKG9REPKIASHUUECPSQO9JT9XNMVKWYGVA',
    )

    self.assertEqual(
      binary_type(addy.checksum),
      b'FOXM9MUBX',
    )
开发者ID:grendias,项目名称:iota.lib.py,代码行数:27,代码来源:types_test.py

示例11: test_concatenation

  def test_concatenation(self):
    """
    Concatenating TryteStrings with TrytesCompatibles.
    """
    trytes1 = TryteString(b'RBTC9D9DCDQA')
    trytes2 = TryteString(b'EASBYBCCKBFA')

    concat = trytes1 + trytes2
    self.assertIsInstance(concat, TryteString)
    self.assertEqual(binary_type(concat), b'RBTC9D9DCDQAEASBYBCCKBFA')

    # You can also concatenate a TryteString with any TrytesCompatible.
    self.assertEqual(
      binary_type(trytes1 + b'EASBYBCCKBFA'),
      b'RBTC9D9DCDQAEASBYBCCKBFA',
    )

    self.assertEqual(
      binary_type(trytes1 + 'EASBYBCCKBFA'),
      b'RBTC9D9DCDQAEASBYBCCKBFA',
    )

    self.assertEqual(
      binary_type(trytes1 + bytearray(b'EASBYBCCKBFA')),
      b'RBTC9D9DCDQAEASBYBCCKBFA',
    )
开发者ID:grendias,项目名称:iota.lib.py,代码行数:26,代码来源:types_test.py

示例12: _get_output_online

    def _get_output_online(self, proc, log_stdout, log_stderr,
                           expect_stderr=False, expect_fail=False):
        stdout, stderr = binary_type(), binary_type()
        while proc.poll() is None:
            if log_stdout:
                line = proc.stdout.readline()
                if line:
                    stdout += line
                    self._log_out(line.decode())
                    # TODO: what level to log at? was: level=5
                    # Changes on that should be properly adapted in
                    # test.cmd.test_runner_log_stdout()
            else:
                pass

            if log_stderr:
                line = proc.stderr.readline()
                if line:
                    stderr += line
                    self._log_err(line.decode(), expect_stderr or expect_fail)
                    # TODO: what's the proper log level here?
                    # Changes on that should be properly adapted in
                    # test.cmd.test_runner_log_stderr()
            else:
                pass

        return stdout, stderr
开发者ID:WurstWorks,项目名称:datalad,代码行数:27,代码来源:cmd.py

示例13: test_default_args

    def test_default_args(self):
        prev = ipv4(proto=inet.IPPROTO_IGMP)
        g = igmpv3_report()
        prev.serialize(g, None)
        buf = g.serialize(bytearray(), prev)
        res = unpack_from(igmpv3_report._PACK_STR, six.binary_type(buf))
        buf = bytearray(buf)
        pack_into("!H", buf, 2, 0)

        eq_(res[0], IGMP_TYPE_REPORT_V3)
        eq_(res[1], checksum(buf))
        eq_(res[2], 0)

        # records without record_num
        prev = ipv4(proto=inet.IPPROTO_IGMP)
        record1 = igmpv3_report_group(MODE_IS_INCLUDE, 0, 0, "225.0.0.1")
        record2 = igmpv3_report_group(MODE_IS_INCLUDE, 0, 2, "225.0.0.2", ["172.16.10.10", "172.16.10.27"])
        record3 = igmpv3_report_group(MODE_IS_INCLUDE, 1, 0, "225.0.0.3", [], b"abc\x00")
        record4 = igmpv3_report_group(MODE_IS_INCLUDE, 1, 2, "225.0.0.4", ["172.16.10.10", "172.16.10.27"], b"abc\x00")
        records = [record1, record2, record3, record4]
        g = igmpv3_report(records=records)
        prev.serialize(g, None)
        buf = g.serialize(bytearray(), prev)
        res = unpack_from(igmpv3_report._PACK_STR, six.binary_type(buf))
        buf = bytearray(buf)
        pack_into("!H", buf, 2, 0)

        eq_(res[0], IGMP_TYPE_REPORT_V3)
        eq_(res[1], checksum(buf))
        eq_(res[2], len(records))
开发者ID:John-Lin,项目名称:ryu,代码行数:30,代码来源:test_igmp.py

示例14: test_barbican_cert

    def test_barbican_cert(self):
        # Certificate data
        self.certificate = six.binary_type(sample.X509_CERT)
        self.intermediates = sample.X509_IMDS_LIST
        self.private_key = six.binary_type(sample.X509_CERT_KEY_ENCRYPTED)
        self.private_key_passphrase = sample.X509_CERT_KEY_PASSPHRASE
        self._prepare()

        container = containers.CertificateContainer(
            api=mock.MagicMock(),
            certificate=self.certificate_secret,
            intermediates=self.intermediates_secret,
            private_key=self.private_key_secret,
            private_key_passphrase=self.private_key_passphrase_secret
        )
        # Create a cert
        cert = barbican_common.BarbicanCert(
            cert_container=container
        )

        # Validate the cert functions
        self.assertEqual(cert.get_certificate(), sample.X509_CERT)
        self.assertEqual(cert.get_intermediates(), sample.X509_IMDS_LIST)
        self.assertEqual(cert.get_private_key(),
                         sample.X509_CERT_KEY_ENCRYPTED)
        self.assertEqual(cert.get_private_key_passphrase(),
                         six.b(sample.X509_CERT_KEY_PASSPHRASE))
开发者ID:openstack,项目名称:octavia,代码行数:27,代码来源:test_barbican.py

示例15: _get_connection

def _get_connection(driver_info):
    # NOTE: python-iboot wants username and password as strings (not unicode)
    return iboot.iBootInterface(driver_info['address'],
                                six.binary_type(driver_info['username']),
                                six.binary_type(driver_info['password']),
                                port=driver_info['port'],
                                num_relays=driver_info['relay_id'])
开发者ID:pshchelo,项目名称:ironic-staging-drivers,代码行数:7,代码来源:power.py


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