Python six.binary_type() Examples

The following are 30 code examples for showing how to use six.binary_type(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module six , or try the search function .

Example 1
Project: python-esppy   Author: sassoftware   File: xdict.py    License: 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 2
Project: pwnypack   Author: edibledinos   File: base.py    License: 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 3
Project: pwnypack   Author: edibledinos   File: base.py    License: 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 4
Project: linter-pylama   Author: AtomLinter   File: brain_builtin_inference.py    License: 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 5
Project: quail   Author: ContextLab   File: helpers.py    License: 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 6
Project: plugin.video.emby   Author: MediaBrowser   File: _parser.py    License: 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 7
Project: ec2-api   Author: openstack   File: ec2utils.py    License: 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 8
Project: lingvo   Author: tensorflow   File: wpm_encoder.py    License: 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
Project: python-netsurv   Author: sofia-netsurv   File: brain_builtin_inference.py    License: 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 10
Project: python-netsurv   Author: sofia-netsurv   File: brain_builtin_inference.py    License: 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 11
Project: pledgeservice   Author: MayOneUS   File: app.py    License: 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 12
Project: lambda-packs   Author: ryfeus   File: decoder.py    License: 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 13
Project: lambda-packs   Author: ryfeus   File: meta_graph.py    License: 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 14
Project: lambda-packs   Author: ryfeus   File: text_plugin.py    License: 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 15
Project: smbprotocol   Author: jborean93   File: _text.py    License: MIT License 5 votes vote down vote up
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) 
Example 16
Project: redis-trib.py   Author: projecteru   File: connection.py    License: MIT License 5 votes vote down vote up
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 
Example 17
Project: python-consul2   Author: poppyred   File: base.py    License: MIT License 5 votes vote down vote up
def script(klass, args, interval):
        """
        Run the script *args* every *interval* (e.g. "10s") to peform health
        check
        """
        if isinstance(args, six.string_types) \
                or isinstance(args, six.binary_type):
            warnings.warn(
                "Check.script should take a list of arg", DeprecationWarning)
            args = ["sh", "-c", args]
        return {'args': args, 'interval': interval} 
Example 18
Project: python-esppy   Author: sassoftware   File: transformers.py    License: Apache License 2.0 5 votes vote down vote up
def bytes2image(data, columns=None):
    '''
    Convert bytes to PIL.Image objects

    Parameters
    ----------
    data : PIL.Image or DataFrame
        The image data
    columns : string or list-of-strings, optional
        If `data` is a DataFrame, this is the list of columns that
        contain image data.

    Returns
    -------
    :class:`PIL.Image`
        If `data` is a :class:`PIL.Image`
    :class:`pandas.DataFrame`
        If `data` is a :class:`pandas.DataFrame`

    '''
    if hasattr(data, 'columns'):
        if len(data):
            if not columns:
                columns = list(data.columns)
            elif isinstance(columns, six.string_types):
                columns = [columns]
            for col in columns:
                if isinstance(data[col].iloc[0], bytes): 
                    data[col] = data[col].apply(_bytes2image)
        return data

    elif isinstance(data, six.binary_type):
        return _bytes2image(data)

    return data 
Example 19
Project: dataflow   Author: tensorpack   File: common.py    License: Apache License 2.0 5 votes vote down vote up
def _batch_numpy(data_list):
        data = data_list[0]
        if isinstance(data, six.integer_types):
            dtype = 'int32'
        elif type(data) == bool:
            dtype = 'bool'
        elif type(data) == float:
            dtype = 'float32'
        elif isinstance(data, (six.binary_type, six.text_type)):
            dtype = 'str'
        else:
            try:
                dtype = data.dtype
            except AttributeError:
                raise TypeError("Unsupported type to batch: {}".format(type(data)))
        try:
            return np.asarray(data_list, dtype=dtype)
        except Exception as e:  # noqa
            logger.exception("Cannot batch data. Perhaps they are of inconsistent shape?")
            if isinstance(data, np.ndarray):
                s = pprint.pformat([x.shape for x in data_list])
                logger.error("Shape of all arrays to be batched: " + s)
            try:
                # open an ipython shell if possible
                import IPython as IP; IP.embed()    # noqa
            except ImportError:
                pass 
Example 20
Project: pwnypack   Author: edibledinos   File: types.py    License: MIT License 5 votes vote down vote up
def verify(value):
        if not isinstance(value, (type(None), six.integer_types, Register, Offset, Buffer, SyscallInvoke,
                                  six.string_types, six.binary_type, list)):
            raise ValueError('syscall argument not of expected type ptr') 
Example 21
Project: pwnypack   Author: edibledinos   File: types.py    License: MIT License 5 votes vote down vote up
def verify(value):
        if not isinstance(value, (type(None), six.string_types, six.binary_type, Register, Offset, Buffer)):
            raise ValueError('syscall argument not of expected type str/bytes') 
Example 22
Project: custodia   Author: latchset   File: plugin.py    License: GNU General Public License v3.0 5 votes vote down vote up
def handle(self, request):
        handler = self._find_handler(request)
        response = {'headers': dict()}

        # Handle request
        output = handler(request, response)
        if output is None:
            output = response.get('output')

        ct = response['headers'].get('Content-Type')
        if ct is None:
            ct = response['headers']['Content-Type'] = DEFAULT_CTYPE

        if 'application/json' in ct and isinstance(output, (dict, list)):
            output = json_encode(output).encode('utf-8')
            response['headers']['Content-Length'] = str(len(output))

        response['output'] = output

        if output is not None and not hasattr(output, 'read') \
                and not isinstance(output, six.binary_type):
            msg = "Handler {} returned unsupported type {} ({}):\n{!r}"
            raise TypeError(msg.format(handler, type(output), ct, output))

        if output is not None and 'Content-Length' not in response['headers']:
            if hasattr(output, 'read'):
                # LOG: warning file-type objects should set Content-Length
                pass
            else:
                response['headers']['Content-Length'] = str(len(output))

        return response 
Example 23
Project: errand-boy   Author: greyside   File: test_base_transport.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test___init___binary(self):
        name = six.binary_type(b'foo')

        ref = base.RemoteObjRef(name)

        self.assertEqual(ref.name, name.decode('utf-8')) 
Example 24
Project: pyspider   Author: binux   File: resultdb.py    License: Apache License 2.0 5 votes vote down vote up
def _parse(data):
        for key, value in list(six.iteritems(data)):
            if isinstance(value, six.binary_type):
                data[key] = utils.text(value)
        if 'result' in data:
            if data['result']:
                data['result'] = json.loads(data['result'])
            else:
                data['result'] = {}
        return data 
Example 25
Project: pyspider   Author: binux   File: taskdb.py    License: Apache License 2.0 5 votes vote down vote up
def _parse(data):
        for key, value in list(six.iteritems(data)):
            if isinstance(value, six.binary_type):
                data[key] = utils.text(value)
        for each in ('schedule', 'fetch', 'process', 'track'):
            if each in data:
                if data[each]:
                    data[each] = json.loads(data[each])
                else:
                    data[each] = {}
        return data 
Example 26
Project: pyspider   Author: binux   File: resultdb.py    License: Apache License 2.0 5 votes vote down vote up
def _parse(self, data):
        for key, value in list(six.iteritems(data)):
            if isinstance(value, (bytearray, six.binary_type)):
                data[key] = utils.text(value)
        if 'result' in data:
            data['result'] = json.loads(data['result'])
        return data 
Example 27
Project: pyspider   Author: binux   File: taskdb.py    License: Apache License 2.0 5 votes vote down vote up
def _parse(self, data):
        for key, value in list(six.iteritems(data)):
            if isinstance(value, (bytearray, six.binary_type)):
                data[key] = utils.text(value)
        for each in ('schedule', 'fetch', 'process', 'track'):
            if each in data:
                if data[each]:
                    data[each] = json.loads(data[each])
                else:
                    data[each] = {}
        return data 
Example 28
Project: pyspider   Author: binux   File: utils.py    License: Apache License 2.0 5 votes vote down vote up
def utf8(string):
    """
    Make sure string is utf8 encoded bytes.

    If parameter is a object, object.__str__ will been called before encode as bytes
    """
    if isinstance(string, six.text_type):
        return string.encode('utf8')
    elif isinstance(string, six.binary_type):
        return string
    else:
        return six.text_type(string).encode('utf8') 
Example 29
Project: recipes-py   Author: luci   File: luci_context.py    License: Apache License 2.0 5 votes vote down vote up
def _to_encodable(obj):
  if isinstance(obj, dict):
    return {
        _to_encodable(key): _to_encodable(value) for key, value in obj.items()
    }
  if isinstance(obj, list):
    return [_to_encodable(item) for item in obj]
  if isinstance(obj, six.binary_type):
    return obj.decode('utf-8')
  return obj 
Example 30
Project: loaner   Author: google   File: deploy_impl.py    License: Apache License 2.0 5 votes vote down vote up
def _EnsureStr(s, encoding='utf-8', errors='strict'):
  """Coerce *s* to `str`.

  Args:
    s: string or bytes
    encoding: string encoding
    errors: raise errors

  Returns:
    PY3 and PY3 str type

  For Python 2:
    - `unicode` -> encoded to `str`
    - `str` -> `str`
  For Python 3:
    - `str` -> `str`
    - `bytes` -> decoded to `str`
  # Needs to be removed once six version is uograded to 1.12.0 or above
  # And replace with `six.ensure_str`

  """
  if not isinstance(s, (six.text_type, six.binary_type)):
    raise TypeError("not expecting type '%s'" % type(s))
  if six.PY2 and isinstance(s, six.text_type):
    s = s.encode(encoding, errors)
  elif six.PY3 and isinstance(s, six.binary_type):
    s = s.decode(encoding, errors)
  return s