本文整理汇总了Python中six.binary_type方法的典型用法代码示例。如果您正苦于以下问题:Python six.binary_type方法的具体用法?Python six.binary_type怎么用?Python six.binary_type使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类six
的用法示例。
在下文中一共展示了six.binary_type方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _is_compound_key
# 需要导入模块: import six [as 别名]
# 或者: from six import binary_type [as 别名]
def _is_compound_key(key, types=six.string_types + (six.text_type, six.binary_type)):
'''
Check for a compound key name
Parameters
----------
key : string
The key name to check
types : list of types, optional
The types of object to check
Returns
-------
True
If the key is compound (i.e., contains a '.')
False
If the key is not compound
'''
return isinstance(key, types) and '.' in key
示例2: alloc_data
# 需要导入模块: import six [as 别名]
# 或者: from six import binary_type [as 别名]
def alloc_data(self, value):
"""
Allocate a piece of data that will be included in the shellcode body.
Arguments:
value(...): The value to add to the shellcode. Can be bytes or
string type.
Returns:
~pwnypack.types.Offset: The offset used to address the data.
"""
if isinstance(value, six.binary_type):
return self._alloc_data(value)
elif isinstance(value, six.text_type):
return self._alloc_data(value.encode('utf-8') + b'\0')
else:
raise TypeError('No idea how to encode %s' % repr(value))
示例3: reg_load_array
# 需要导入模块: import six [as 别名]
# 或者: from six import binary_type [as 别名]
def reg_load_array(self, reg, value):
temp_reg = self.TEMP_REG[self.target.bits]
code = []
if value:
for item in reversed(value):
if isinstance(item, (six.text_type, six.binary_type)):
item = self.alloc_data(item)
if isinstance(item, Offset) and not item:
item = self.OFFSET_REG
if not isinstance(item, Register):
code.extend(self.reg_load(temp_reg, item))
item = temp_reg
code.extend(self.reg_push(item))
code.extend(self.reg_load(reg, self.STACK_REG))
return code
示例4: _generic_transform
# 需要导入模块: import six [as 别名]
# 或者: from six import binary_type [as 别名]
def _generic_transform(arg, klass, iterables, build_elts):
if isinstance(arg, klass):
return arg
elif isinstance(arg, iterables):
if not all(isinstance(elt, nodes.Const)
for elt in arg.elts):
# TODO(cpopa): Don't support heterogenous elements.
# Not yet, though.
raise UseInferenceDefault()
elts = [elt.value for elt in arg.elts]
elif isinstance(arg, nodes.Dict):
if not all(isinstance(elt[0], nodes.Const)
for elt in arg.items):
raise UseInferenceDefault()
elts = [item[0].value for item in arg.items]
elif (isinstance(arg, nodes.Const) and
isinstance(arg.value, (six.string_types, six.binary_type))):
elts = arg.value
else:
return
return klass.from_constants(elts=build_elts(elts))
示例5: default_dist_funcs
# 需要导入模块: import six [as 别名]
# 或者: from six import binary_type [as 别名]
def default_dist_funcs(dist_funcs, feature_example):
"""
Fills in default distance metrics for fingerprint analyses
"""
if dist_funcs is None:
dist_funcs = dict()
for key in feature_example:
if key in dist_funcs:
pass
if key == 'item':
pass
elif isinstance(feature_example[key], (six.string_types, six.binary_type)):
dist_funcs[key] = 'match'
elif isinstance(feature_example[key], (int, np.integer, float)) or all([isinstance(i, (int, np.integer, float)) for i in feature_example[key]]):
dist_funcs[key] = 'euclidean'
return dist_funcs
示例6: __init__
# 需要导入模块: import six [as 别名]
# 或者: from six import binary_type [as 别名]
def __init__(self, instream):
if six.PY2:
# In Python 2, we can't duck type properly because unicode has
# a 'decode' function, and we'd be double-decoding
if isinstance(instream, (binary_type, bytearray)):
instream = instream.decode()
else:
if getattr(instream, 'decode', None) is not None:
instream = instream.decode()
if isinstance(instream, text_type):
instream = StringIO(instream)
elif getattr(instream, 'read', None) is None:
raise TypeError('Parser must be a string or character stream, not '
'{itype}'.format(itype=instream.__class__.__name__))
self.instream = instream
self.charstack = []
self.tokenstack = []
self.eof = False
示例7: _render_data
# 需要导入模块: import six [as 别名]
# 或者: from six import binary_type [as 别名]
def _render_data(el, data):
if isinstance(data, list):
for item in data:
sub_el = etree.SubElement(el, 'item')
_render_data(sub_el, item)
elif isinstance(data, dict):
_render_dict(el, data)
elif hasattr(data, '__dict__'):
_render_dict(el, data.__dict__)
elif isinstance(data, bool):
el.text = str(data).lower()
elif isinstance(data, datetime.datetime):
el.text = _database_to_isoformat(data)
elif isinstance(data, six.binary_type):
el.text = data.decode("utf-8")
elif data is not None:
el.text = six.text_type(data)
示例8: __init__
# 需要导入模块: import six [as 别名]
# 或者: from six import binary_type [as 别名]
def __init__(self, wpm_filepath, merge_prob=1.):
"""Create a WPM encoder.
Args:
wpm_filepath: a path to the file containing the vocabulary.
merge_prob: the probability of merging tokens while encoding.
"""
# Load vocabulary file.
lines = py_utils.ReadFileLines(wpm_filepath)
self._pieces = []
for line in lines:
if isinstance(line, six.binary_type):
line = six.ensure_text(line, 'utf-8')
piece = line.strip().split('\t')[0]
self._pieces.append(piece)
self._merge_prob = merge_prob
示例9: _generic_transform
# 需要导入模块: import six [as 别名]
# 或者: from six import binary_type [as 别名]
def _generic_transform(arg, klass, iterables, build_elts):
if isinstance(arg, klass):
return arg
elif isinstance(arg, iterables):
if not all(isinstance(elt, nodes.Const) for elt in arg.elts):
raise UseInferenceDefault()
elts = [elt.value for elt in arg.elts]
elif isinstance(arg, nodes.Dict):
if not all(isinstance(elt[0], nodes.Const) for elt in arg.items):
raise UseInferenceDefault()
elts = [item[0].value for item in arg.items]
elif isinstance(arg, nodes.Const) and isinstance(
arg.value, (six.string_types, six.binary_type)
):
elts = arg.value
else:
return
return klass.from_constants(elts=build_elts(elts))
示例10: __init__
# 需要导入模块: import six [as 别名]
# 或者: from six import binary_type [as 别名]
def __init__(self, message, *args):
if isinstance(message, binary_type):
message = message.decode('utf8')
str_args = ()
for arg in args:
if isinstance(arg, webob.Response):
body = arg.body
if isinstance(body, binary_type):
if arg.charset:
arg = body.decode(arg.charset)
else:
arg = repr(body)
elif isinstance(arg, binary_type):
try:
arg = arg.decode('utf8')
except UnicodeDecodeError:
arg = repr(arg)
str_args += (arg,)
message = message % str_args
Exception.__init__(self, message)
示例11: ReadTag
# 需要导入模块: import six [as 别名]
# 或者: from six import binary_type [as 别名]
def ReadTag(buffer, pos):
"""Read a tag from the buffer, and return a (tag_bytes, new_pos) tuple.
We return the raw bytes of the tag rather than decoding them. The raw
bytes can then be used to look up the proper decoder. This effectively allows
us to trade some work that would be done in pure-python (decoding a varint)
for work that is done in C (searching for a byte string in a hash table).
In a low-level language it would be much cheaper to decode the varint and
use that, but not in Python.
"""
start = pos
while six.indexbytes(buffer, pos) & 0x80:
pos += 1
pos += 1
return (six.binary_type(buffer[start:pos]), pos)
# --------------------------------------------------------------------
示例12: _get_kind_name
# 需要导入模块: import six [as 别名]
# 或者: from six import binary_type [as 别名]
def _get_kind_name(item):
"""Returns the kind name in CollectionDef.
Args:
item: A data item.
Returns:
The string representation of the kind in CollectionDef.
"""
if isinstance(item, (six.string_types, six.binary_type)):
kind = "bytes_list"
elif isinstance(item, six.integer_types):
kind = "int64_list"
elif isinstance(item, float):
kind = "float_list"
elif isinstance(item, Any):
kind = "any_list"
else:
kind = "node_list"
return kind
示例13: markdown_and_sanitize
# 需要导入模块: import six [as 别名]
# 或者: from six import binary_type [as 别名]
def markdown_and_sanitize(markdown_string):
"""Takes a markdown string and converts it into sanitized html.
It uses the table extension; while that's not a part of standard
markdown, it is sure to be useful for TensorBoard users.
The sanitizer uses the allowed_tags and attributes specified above. Mostly,
we ensure that our standard use cases like tables and links are supported.
Args:
markdown_string: Markdown string to sanitize
Returns:
a string containing sanitized html for input markdown
"""
# Convert to utf-8 whenever we have a binary input.
if isinstance(markdown_string, six.binary_type):
markdown_string = markdown_string.decode('utf-8')
string_html = markdown.markdown(
markdown_string, extensions=['markdown.extensions.tables'])
string_sanitized = bleach.clean(
string_html, tags=ALLOWED_TAGS, attributes=ALLOWED_ATTRIBUTES)
return string_sanitized
示例14: to_bytes
# 需要导入模块: import six [as 别名]
# 或者: from six import binary_type [as 别名]
def to_bytes(value, encoding='utf-8'):
"""
Makes sure the value is encoded as a byte string.
:param value: The Python string value to encode.
:param encoding: The encoding to use.
:return: The byte string that was encoded.
"""
if isinstance(value, six.binary_type):
return value
return value.encode(encoding)
示例15: encode
# 需要导入模块: import six [as 别名]
# 或者: from six import binary_type [as 别名]
def encode(value):
if isinstance(value, six.binary_type):
return value
if isinstance(value, six.integer_types):
return b(str(value))
if isinstance(value, float):
return b(repr(value))
if isinstance(value, six.text_type):
return value.encode(ENCODING)
if not isinstance(value, six.string_types):
return b(value)
return value