本文整理汇总了Python中six.binary_type函数的典型用法代码示例。如果您正苦于以下问题:Python binary_type函数的具体用法?Python binary_type怎么用?Python binary_type使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了binary_type函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
示例16: to_frame
def to_frame(self, data, state):
"""
Extract a single frame from the data buffer. The consumed
data should be removed from the buffer. If no complete frame
can be read, must raise a ``NoFrames`` exception.
:param data: A ``bytearray`` instance containing the data so
far read.
:param state: An instance of ``FramerState``. If the buffer
contains a partial frame, this object can be
used to store state information to allow the
remainder of the frame to be read.
:returns: A frame. The frame may be any object. The stock
framers always return bytes.
"""
# Find the next null byte
data_len = data.find(b'\0')
if data_len < 0:
# No full frame yet
raise exc.NoFrames()
# Track how much to exclude
frame_len = data_len + 1
# Decode the data
frame = six.binary_type(self.variant.decode(
six.binary_type(data[:data_len])))
del data[:frame_len]
# Return the frame
return frame
开发者ID:klmitch,项目名称:framer,代码行数:34,代码来源:framers.py
示例17: test_retrieves_all_components_for_installed_apps
def test_retrieves_all_components_for_installed_apps(self, run):
response = self.client.get(self.url, format='json')
assert response.status_code == 200
assert self.component3.uuid not in [d['uuid'] for d in response.data]
assert response.data == [
{
'uuid': six.binary_type(self.component1.uuid),
'type': 'issue-link',
'schema': self.component1.schema,
'sentryApp': {
'uuid': self.sentry_app1.uuid,
'slug': self.sentry_app1.slug,
'name': self.sentry_app1.name,
},
},
{
'uuid': six.binary_type(self.component2.uuid),
'type': 'issue-link',
'schema': self.component2.schema,
'sentryApp': {
'uuid': self.sentry_app2.uuid,
'slug': self.sentry_app2.slug,
'name': self.sentry_app2.name,
},
},
]
开发者ID:yaoqi,项目名称:sentry,代码行数:27,代码来源:test_sentry_app_components.py
示例18: encode_key
def encode_key(project_id, namespace, key):
""" Encodes a key for memcached.
Args:
project_id: A string specifying the project ID.
namespace: A string specifying the namespace.
key: A bytestring specifying the memcache key.
Returns:
A bytestring in the form of <project-id>\x01<namespace>\x01<encoded-key>
Raises:
ApplicationError if the key is too long.
"""
if len(key) > MAX_KEY_SIZE:
raise apiproxy_errors.ApplicationError(
INVALID_VALUE, 'The key is too long: {}'.format(key))
project_id = six.binary_type(project_id)
namespace = six.binary_type(namespace)
encoded_key = base64.b64encode(key)
full_key = KEY_DELIMETER.join(
[project_id, namespace, INTACT_MARKER + encoded_key])
if len(full_key) <= MAX_KEY_SIZE:
return full_key
# GAE only rejects requests when the key length is too long. Since this
# implementation's stored key includes a namespace prefix, the key is hashed
# if necessary to comply with the memcached limit. The length of the key's
# hex digest + the max project ID size + the max namespace size is still less
# than the memcached limit.
hashed_key = hashlib.sha1(key).hexdigest()
return KEY_DELIMETER.join(
[project_id, namespace, HASHED_MARKER + hashed_key])
开发者ID:obino,项目名称:appscale,代码行数:32,代码来源:memcache_distributed.py
示例19: test_jsanitize
def test_jsanitize(self):
# clean_json should have no effect on None types.
d = {"hello": 1, "world": None}
clean = jsanitize(d)
self.assertIsNone(clean["world"])
self.assertEqual(json.loads(json.dumps(d)), json.loads(json.dumps(
clean)))
d = {"hello": GoodMSONClass(1, 2, 3)}
self.assertRaises(TypeError, json.dumps, d)
clean = jsanitize(d)
self.assertIsInstance(clean["hello"], six.string_types)
clean_strict = jsanitize(d, strict=True)
self.assertEqual(clean_strict["hello"]["a"], 1)
self.assertEqual(clean_strict["hello"]["b"], 2)
d = {"dt": datetime.datetime.now()}
clean = jsanitize(d)
self.assertIsInstance(clean["dt"], six.string_types)
clean = jsanitize(d, allow_bson=True)
self.assertIsInstance(clean["dt"], datetime.datetime)
d = {"a": ["b", np.array([1, 2, 3])],
"b": ObjectId.from_datetime(datetime.datetime.now())}
clean = jsanitize(d)
self.assertEqual(clean["a"], ['b', [1, 2, 3]])
self.assertIsInstance(clean["b"], six.string_types)
rnd_bin = six.binary_type(np.random.rand(10))
d = {"a": six.binary_type(rnd_bin)}
clean = jsanitize(d, allow_bson=True)
self.assertEqual(clean["a"], six.binary_type(rnd_bin))
self.assertIsInstance(clean["a"], six.binary_type)
开发者ID:davidwaroquiers,项目名称:monty,代码行数:33,代码来源:test_json.py
示例20: test_delete_node_with_children
def test_delete_node_with_children(self):
self.test_dt.create_node("/a", six.binary_type(b"test_data_a"))
self.test_dt.create_node("/a/b", six.binary_type(b"test_data_ab"))
self.test_dt.create_node("/a/c", six.binary_type(b"test_data_ac"))
pytest.raises(datatree.NotEmptyException, self.test_dt.delete_node,
"/a")
开发者ID:chmouel,项目名称:chillaxd,代码行数:7,代码来源:test_datatree.py
注:本文中的six.binary_type函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论