Python six.binary_type() Examples

The following are 30 code examples of six.binary_type(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module six , or try the search function .
Example #1
Source File: ec2utils.py    From ec2-api with Apache License 2.0 6 votes vote down vote up
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) 
Example #2
Source File: _parser.py    From plugin.video.emby with GNU General Public License v3.0 6 votes vote down vote up
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 
Example #3
Source File: app.py    From pledgeservice with Apache License 2.0 6 votes vote down vote up
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) 
Example #4
Source File: helpers.py    From quail with MIT License 6 votes vote down vote up
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 
Example #5
Source File: brain_builtin_inference.py    From python-netsurv with MIT License 6 votes vote down vote up
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)) 
Example #6
Source File: brain_builtin_inference.py    From linter-pylama with MIT License 6 votes vote down vote up
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)) 
Example #7
Source File: brain_builtin_inference.py    From python-netsurv with MIT License 6 votes vote down vote up
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)) 
Example #8
Source File: wpm_encoder.py    From lingvo with Apache License 2.0 6 votes vote down vote up
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 
Example #9
Source File: decoder.py    From lambda-packs with MIT License 6 votes vote down vote up
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)


# -------------------------------------------------------------------- 
Example #10
Source File: base.py    From pwnypack with MIT License 6 votes vote down vote up
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 
Example #11
Source File: base.py    From pwnypack with MIT License 6 votes vote down vote up
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)) 
Example #12
Source File: meta_graph.py    From lambda-packs with MIT License 6 votes vote down vote up
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 
Example #13
Source File: text_plugin.py    From lambda-packs with MIT License 6 votes vote down vote up
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 
Example #14
Source File: xdict.py    From python-esppy with Apache License 2.0 6 votes vote down vote up
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 
Example #15
Source File: message.py    From azure-uamqp-python with MIT License 5 votes vote down vote up
def __unicode__(self):
        data = self.data
        if not data:
            return u""
        if isinstance(data, six.binary_type):
            return data.decode(self._encoding)
        return unicode(data)  # pylint: disable=undefined-variable 
Example #16
Source File: utils.py    From mixpanel-query-py with MIT License 5 votes vote down vote up
def _tobytes(val):
    """
    Py2 and Py3 compatible function that coerces
    any non-binary string types into the official "binary type" (str in Py2, bytes in Py3).
    Values that are not a text or binary type are converted to text (unicode)
    first, then encoded via UTF-8 into binary type.
    """
    if isinstance(val, six.binary_type):
        return val
    elif isinstance(val, six.text_type):
        return val.encode('utf-8')
    else:
        return six.text_type(val).encode('utf-8') 
Example #17
Source File: lint.py    From pledgeservice with Apache License 2.0 5 votes vote down vote up
def next(self):
        assert not self.closed, (
            "Iterator read after closed")
        v = next(self.iterator)
        if self.check_start_response is not None:
            assert self.check_start_response, (
                "The application returns and we started iterating over its"
                " body, but start_response has not yet been called")
            self.check_start_response = None
        assert isinstance(v, binary_type), (
            "Iterator %r returned a non-%r object: %r"
            % (self.iterator, binary_type, v))
        return v 
Example #18
Source File: response.py    From pledgeservice with Apache License 2.0 5 votes vote down vote up
def __contains__(self, s):
        """
        A response 'contains' a string if it is present in the body
        of the response.  Whitespace is normalized when searching
        for a string.
        """
        if not self.charset and isinstance(s, text_type):
            s = s.encode('utf8')
        if isinstance(s, binary_type):
            return s in self.body or s in self.normal_body
        return s in self.testbody or s in self.unicode_normal_body 
Example #19
Source File: encoder.py    From lambda-packs with MIT License 5 votes vote down vote up
def TagBytes(field_number, wire_type):
  """Encode the given tag and return the bytes.  Only called at startup."""

  return six.binary_type(
      _VarintBytes(wire_format.PackTag(field_number, wire_type)))

# --------------------------------------------------------------------
# As with sizers (see above), we have a number of common encoder
# implementations. 
Example #20
Source File: utils.py    From mixpanel-query-py with MIT License 5 votes vote down vote up
def _totext(val):
    """
    Py2 and Py3 compatible function that coerces
    any non-Unicode string types into the official "text type" (unicode in Py2, str in Py3).
    For objects that are not binary (str/bytes) or text (unicode/str),
    return value is unchanged object.
    """
    if isinstance(val, six.text_type):
        return val
    elif isinstance(val, six.binary_type):
        return val.decode('utf-8')
    else:
        return val 
Example #21
Source File: cuda_gpu.py    From aetros-cli with MIT License 5 votes vote down vote up
def get_device_properties(device, all=False):
    try:
        libcudart = get_libcudart()

        properties = CUDADeviceProperties()
        rc = libcudart.cudaGetDeviceProperties(ctypes.byref(properties), device)
        if rc != 0:
            return None
    except Exception:
        return None

    if all:
        values = {}
        for field in properties._fields_:
            values[field[0]] = getattr(properties, field[0])

            if isinstance(values[field[0]], six.binary_type):
                values[field[0]] = values[field[0]].decode('utf-8')

            if '_Array_' in type(values[field[0]]).__name__:
                values[field[0]] = [x for x in values[field[0]]]

        return values

    return {
        'device': device,
        'name': properties.name,
        'clock': properties.clockRate,
        'memory': properties.totalGlobalMem,
        'pciDomainID': properties.pciDomainID,
        'pciDeviceID': properties.pciDeviceID,
        'pciBusID': properties.pciBusID,
    } 
Example #22
Source File: test_sp.py    From pysaxon with Creative Commons Zero v1.0 Universal 5 votes vote down vote up
def test_version():
    """SaxonProcessor version string content"""
    sp = SaxonProcessor()
    ver = sp.version
    assert isinstance(ver, six.binary_type)
    assert ver.startswith(b'Saxon-HE')
    assert ver.endswith(b'from Saxonica') 
Example #23
Source File: __init__.py    From python-netsurv with MIT License 5 votes vote down vote up
def write_bytes(self, data):
        """
        Open the file in bytes mode, write to it, and close the file.
        """
        if not isinstance(data, six.binary_type):
            raise TypeError(
                'data must be %s, not %s' %
                (six.binary_type.__name__, data.__class__.__name__))
        with self.open(mode='wb') as f:
            return f.write(data) 
Example #24
Source File: message.py    From azure-uamqp-python with MIT License 5 votes vote down vote up
def __str__(self):
        data = self.data
        if not data:
            return ""
        if six.PY3 and isinstance(data, six.binary_type):
            return data.decode(self._encoding)
        return str(data) 
Example #25
Source File: message.py    From azure-uamqp-python with MIT License 5 votes vote down vote up
def append(self, data):
        """Append a section to the body.

        :param data: The data to append.
        :type data: str or bytes
        """
        if isinstance(data, six.text_type):
            self._message.add_body_data(data.encode(self._encoding))
        elif isinstance(data, six.binary_type):
            self._message.add_body_data(data) 
Example #26
Source File: message.py    From azure-uamqp-python with MIT License 5 votes vote down vote up
def reply_to_group_id(self, value):
        if isinstance(value, six.text_type):
            value = value.encode(self._encoding)
        elif value is not None and not isinstance(value, six.binary_type):
            raise TypeError("reply_to_group_id must be bytes or str.")
        self._reply_to_group_id = value 
Example #27
Source File: message.py    From azure-uamqp-python with MIT License 5 votes vote down vote up
def group_id(self, value):
        if isinstance(value, six.text_type):
            value = value.encode(self._encoding)
        elif value is not None and not isinstance(value, six.binary_type):
            raise TypeError("group_id must be bytes or str.")
        self._group_id = value 
Example #28
Source File: message.py    From azure-uamqp-python with MIT License 5 votes vote down vote up
def content_type(self, value):
        if isinstance(value, six.text_type):
            value = value.encode(self._encoding)
        elif value is not None and not isinstance(value, six.binary_type):
            raise TypeError("content_type must be bytes or str.")
        self._content_type = value 
Example #29
Source File: message.py    From azure-uamqp-python with MIT License 5 votes vote down vote up
def subject(self, value):
        if isinstance(value, six.text_type):
            value = value.encode(self._encoding)
        elif value is not None and not isinstance(value, six.binary_type):
            raise TypeError("subject must be bytes or str.")
        self._subject = value 
Example #30
Source File: message.py    From azure-uamqp-python with MIT License 5 votes vote down vote up
def user_id(self, value):
        if isinstance(value, six.text_type):
            value = value.encode(self._encoding)
        elif value is not None and not isinstance(value, six.binary_type):
            raise TypeError("user_id must be bytes or str.")
        self._user_id = value