How to use the six.binary_type function in six

To help you get started, we’ve selected a few six examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github openstack / keystonemiddleware / test_utils.py View on Github external
def test_print_dict_unicode(self):
        name = six.u('\u540d\u5b57')
        utils.print_dict({'name': name})
        output = self.stdout.getvalue()
        # In Python 2, output will be bytes, while in Python 3, it will not.
        # Let's decode the value if needed.
        if isinstance(output, six.binary_type):
            output = output.decode('utf-8')
        self.assertIn(name, output)
github adamcharnock / swiftwind / swiftwind / transactions / tests.py View on Github external
def test_create_columns_ok(self):
        f = SimpleUploadedFile('data.csv',
                               six.binary_type(
                                   b'Number,Date,Account,Amount,Subcategory,Memo\n'
                                   b'1,1/1/1,123456789,123,OTH,Some random notes')
                               )

        inst = TransactionImport.objects.create(has_headings=True, file=f, hordak_import=self.statement_import())
        inst.create_columns()

        columns = inst.columns.all()

        self.assertEqual(columns[0].column_number, 1)
        self.assertEqual(columns[0].column_heading, 'Number')
        self.assertEqual(columns[0].to_field, None)
        self.assertEqual(columns[0].example, '1')

        self.assertEqual(columns[1].column_number, 2)
        self.assertEqual(columns[1].column_heading, 'Date')
github IdentityPython / pysaml2 / src / saml2 / sigver.py View on Github external
""" xmlsec needs files in some cases where only strings exist, hence the
    need for this function. It creates a temporary file with the
    string as only content.

    :param string: The information to be placed in the file
    :param suffix: The temporary file might have to have a specific
        suffix in certain circumstances.
    :param decode: The input string might be base64 coded. If so it
        must, in some cases, be decoded before being placed in the file.
    :return: 2-tuple with file pointer ( so the calling function can
        close the file) and filename (which is for instance needed by the
        xmlsec function).
    """
    ntf = NamedTemporaryFile(suffix=suffix, delete=delete)
    # Python3 tempfile requires byte-like object
    if not isinstance(string, six.binary_type):
        string = string.encode('utf-8')

    if decode:
        ntf.write(base64.b64decode(string))
    else:
        ntf.write(string)
    ntf.seek(0)
    return ntf, ntf.name
github johnpaulett / python-hl7 / hl7 / client.py View on Github external
def stdout(content):
    # In Python 3, can't write bytes via sys.stdout.write
    #   http://bugs.python.org/issue18512
    if six.PY3 and isinstance(content, six.binary_type):
        out = sys.stdout.buffer
        newline = b'\n'
    else:
        out = sys.stdout
        newline = '\n'

    out.write(content + newline)
github openstack / cloudkitty / cloudkitty / openstack / common / strutils.py View on Github external
def safe_decode(text, incoming=None, errors='strict'):
    """Decodes incoming text/bytes string using `incoming` if they're not
       already unicode.

    :param incoming: Text's current encoding
    :param errors: Errors handling policy. See here for valid
        values http://docs.python.org/2/library/codecs.html
    :returns: text or a unicode `incoming` encoded
                representation of it.
    :raises TypeError: If text is not an instance of str
    """
    if not isinstance(text, (six.string_types, six.binary_type)):
        raise TypeError("%s can't be decoded" % type(text))

    if isinstance(text, six.text_type):
        return text

    if not incoming:
        incoming = (sys.stdin.encoding or
                    sys.getdefaultencoding())

    try:
        return text.decode(incoming, errors)
    except UnicodeDecodeError:
        # Note(flaper87) If we get here, it means that
        # sys.stdin.encoding / sys.getdefaultencoding
        # didn't return a suitable encoding to decode
        # text. This happens mostly when global LANG
github Infinidat / infi.clickhouse_orm / src / infi / clickhouse_orm / fields.py View on Github external
def to_python(self, value, timezone_in_use):
        if isinstance(value, text_type):
            value = parse_array(value)
        elif isinstance(value, binary_type):
            value = parse_array(value.decode('UTF-8'))
        elif not isinstance(value, (list, tuple)):
            raise ValueError('ArrayField expects list or tuple, not %s' % type(value))
        return [self.inner_field.to_python(v, timezone_in_use) for v in value]
github h3llrais3r / Auto-Subliminal / lib / cherrypy / wsgiserver / __init__.py View on Github external
"""
        From PEP 333:

            The start_response callable must not actually transmit
            the response headers. Instead, it must store them for the
            server or gateway to transmit only after the first
            iteration of the application return value that yields
            a NON-EMPTY string, or upon the application's first
            invocation of the write() callable.
        """

        response = self.req.server.wsgi_app(self.env, self.start_response)
        try:
            for chunk in filter(None, response):
                if not isinstance(chunk, six.binary_type):
                    raise ValueError("WSGI Applications must yield bytes")
                self.write(chunk)
        finally:
            if hasattr(response, "close"):
                response.close()
github josiahcarlson / rom / rom / columns.py View on Github external
def _from_redis(self, value):
        if isinstance(value, six.binary_type):
            return value.decode('utf-8')
        return value
github binux / pyspider / pyspider / database / mysql / taskdb.py View on Github external
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
github brenokcc / djangoplus / utils / storage / dropbox.py View on Github external
def update(self, new_data):
        if self._overall_hasher is None:
            raise AssertionError(
                "can't use this object anymore; you already called digest()")

        assert isinstance(new_data, six.binary_type), (
            "Expecting a byte string, got {!r}".format(new_data))

        new_data_pos = 0
        while new_data_pos < len(new_data):
            if self._block_pos == self.BLOCK_SIZE:
                self._overall_hasher.update(self._block_hasher.digest())
                self._block_hasher = hashlib.sha256()
                self._block_pos = 0

            space_in_block = self.BLOCK_SIZE - self._block_pos
            part = new_data[new_data_pos:(new_data_pos+space_in_block)]
            self._block_hasher.update(part)

            self._block_pos += len(part)
            new_data_pos += len(part)