python类binary_type()的实例源码

schema.py 文件源码 项目:CodingDojo 作者: ComputerSocietyUNB 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def effective_default(self, field):
        """
        Returns a field's effective database default value
        """
        if field.has_default():
            default = field.get_default()
        elif not field.null and field.blank and field.empty_strings_allowed:
            if field.get_internal_type() == "BinaryField":
                default = six.binary_type()
            else:
                default = six.text_type()
        else:
            default = None
        # If it's a callable, call it
        if six.callable(default):
            default = default()
        # Run it through the field's get_db_prep_save method so we can send it
        # to the database.
        default = field.get_db_prep_save(default, self.connection)
        return default
tests.py 文件源码 项目:django-cryptography 作者: georgemarshall 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def test_sign_unsign(self):
        """sign/unsign should be reversible"""
        signer = signing.BytesSigner('predictable-secret')
        examples = [
            b'q;wjmbk;wkmb',
            b'3098247529087',
            b'3098247:529:087:',
            b'jkw osanteuh ,rcuh nthu aou oauh ,ud du',
            b'\u2019',
        ]
        if six.PY2:
            examples.append(b'a byte string')
        for example in examples:
            signed = signer.sign(example)
            self.assertIsInstance(signed, six.binary_type)
            self.assertNotEqual(force_str(example), signed)
            self.assertEqual(example, signer.unsign(signed))
schema.py 文件源码 项目:trydjango18 作者: wei0104 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def effective_default(self, field):
        """
        Returns a field's effective database default value
        """
        if field.has_default():
            default = field.get_default()
        elif not field.null and field.blank and field.empty_strings_allowed:
            if field.get_internal_type() == "BinaryField":
                default = six.binary_type()
            else:
                default = six.text_type()
        else:
            default = None
        # If it's a callable, call it
        if six.callable(default):
            default = default()
        # Run it through the field's get_db_prep_save method so we can send it
        # to the database.
        default = field.get_db_prep_save(default, self.connection)
        return default
schema.py 文件源码 项目:djanoDoc 作者: JustinChavez 项目源码 文件源码 阅读 35 收藏 0 点赞 0 评论 0
def effective_default(self, field):
        """
        Returns a field's effective database default value
        """
        if field.has_default():
            default = field.get_default()
        elif not field.null and field.blank and field.empty_strings_allowed:
            if field.get_internal_type() == "BinaryField":
                default = six.binary_type()
            else:
                default = six.text_type()
        else:
            default = None
        # If it's a callable, call it
        if six.callable(default):
            default = default()
        # Run it through the field's get_db_prep_save method so we can send it
        # to the database.
        default = field.get_db_prep_save(default, self.connection)
        return default
manifests.py 文件源码 项目:andromeda 作者: pivstone 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def save(self):
        """
        ?? manifest ??
        ?? ?? Layers ?Links ??
        :return:
        """
        data_string = self.data_string
        if not isinstance(data_string, six.binary_type):
            data_string = data_string.encode("utf-8")
        manifest_digest = storage.save_manifest(data_string)
        # save tags
        current_tag_path = storage.path_spec.get_tag_current_path(name=self.name, tag_name=self.reference)
        storage.link(manifest_digest, current_tag_path)
        tag_index_path = storage.path_spec.get_tag_index_path(name=self.name, tag_name=self.reference,
                                                              digest=manifest_digest)
        storage.link(manifest_digest, tag_index_path)

        # Save reference
        reference_path = storage.path_spec.get_reference_path(name=self.name, digest=manifest_digest)
        storage.link(manifest_digest, reference_path)

        for layer in self.layers:
            layer_path = storage.path_spec.get_layer_path(name=self.name, digest=layer)
            storage.link(layer, layer_path)
crypto.py 文件源码 项目:andromeda 作者: pivstone 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def _decode(value):
    """
    Base64 ?????"="
    ???????“?”???Docker?????????
    :param value:
    :return:
    """

    length = len(value) % 4
    if length in (2, 3,):
        value += (4 - length) * "="
    elif length != 0:
        raise ValueError("Invalid base64 string")
    if not isinstance(value, six.binary_type):
        value = value.encode()
    return base64.urlsafe_b64decode(value)
schema.py 文件源码 项目:django-next-train 作者: bitpixdigital 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def effective_default(self, field):
        """
        Returns a field's effective database default value
        """
        if field.has_default():
            default = field.get_default()
        elif not field.null and field.blank and field.empty_strings_allowed:
            if field.get_internal_type() == "BinaryField":
                default = six.binary_type()
            else:
                default = six.text_type()
        else:
            default = None
        # If it's a callable, call it
        if six.callable(default):
            default = default()
        # Run it through the field's get_db_prep_save method so we can send it
        # to the database.
        default = field.get_db_prep_save(default, self.connection)
        return default
schema.py 文件源码 项目:django-wechat-api 作者: crazy-canux 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def effective_default(self, field):
        """
        Returns a field's effective database default value
        """
        if field.has_default():
            default = field.get_default()
        elif not field.null and field.blank and field.empty_strings_allowed:
            if field.get_internal_type() == "BinaryField":
                default = six.binary_type()
            else:
                default = six.text_type()
        else:
            default = None
        # If it's a callable, call it
        if six.callable(default):
            default = default()
        # Run it through the field's get_db_prep_save method so we can send it
        # to the database.
        default = field.get_db_prep_save(default, self.connection)
        return default
test_keys.py 文件源码 项目:django-rest-framework-sso 作者: namespace-ee 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def test_read(self):
        key_data = keys.read_key_file('test-2048.pem')
        self.assertIsInstance(key_data, six.binary_type)
        key_data_lines = key_data.decode('utf-8').split('\n')
        self.assertIn('-----BEGIN PRIVATE KEY-----', key_data_lines)
        self.assertIn('-----END PRIVATE KEY-----', key_data_lines)
        self.assertIn('-----BEGIN PUBLIC KEY-----', key_data_lines)
        self.assertIn('-----END PUBLIC KEY-----', key_data_lines)
message.py 文件源码 项目:Scrum 作者: prakharchoudhary 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def attach(self, filename=None, content=None, mimetype=None):
        """
        Attaches a file with the given filename and content. The filename can
        be omitted and the mimetype is guessed, if not provided.

        If the first parameter is a MIMEBase subclass it is inserted directly
        into the resulting message attachments.

        For a text/* mimetype (guessed or specified), when a bytes object is
        specified as content, it will be decoded as UTF-8. If that fails,
        the mimetype will be set to DEFAULT_ATTACHMENT_MIME_TYPE and the
        content is not decoded.
        """
        if isinstance(filename, MIMEBase):
            assert content is None
            assert mimetype is None
            self.attachments.append(filename)
        else:
            assert content is not None

            if not mimetype:
                mimetype, _ = mimetypes.guess_type(filename)
                if not mimetype:
                    mimetype = DEFAULT_ATTACHMENT_MIME_TYPE
            basetype, subtype = mimetype.split('/', 1)

            if basetype == 'text':
                if isinstance(content, six.binary_type):
                    try:
                        content = content.decode('utf-8')
                    except UnicodeDecodeError:
                        # If mimetype suggests the file is text but it's actually
                        # binary, read() will raise a UnicodeDecodeError on Python 3.
                        mimetype = DEFAULT_ATTACHMENT_MIME_TYPE

            self.attachments.append((filename, content, mimetype))
message.py 文件源码 项目:django 作者: alexsukhrin 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def attach(self, filename=None, content=None, mimetype=None):
        """
        Attaches a file with the given filename and content. The filename can
        be omitted and the mimetype is guessed, if not provided.

        If the first parameter is a MIMEBase subclass it is inserted directly
        into the resulting message attachments.

        For a text/* mimetype (guessed or specified), when a bytes object is
        specified as content, it will be decoded as UTF-8. If that fails,
        the mimetype will be set to DEFAULT_ATTACHMENT_MIME_TYPE and the
        content is not decoded.
        """
        if isinstance(filename, MIMEBase):
            assert content is None
            assert mimetype is None
            self.attachments.append(filename)
        else:
            assert content is not None

            if not mimetype:
                mimetype, _ = mimetypes.guess_type(filename)
                if not mimetype:
                    mimetype = DEFAULT_ATTACHMENT_MIME_TYPE
            basetype, subtype = mimetype.split('/', 1)

            if basetype == 'text':
                if isinstance(content, six.binary_type):
                    try:
                        content = content.decode('utf-8')
                    except UnicodeDecodeError:
                        # If mimetype suggests the file is text but it's actually
                        # binary, read() will raise a UnicodeDecodeError on Python 3.
                        mimetype = DEFAULT_ATTACHMENT_MIME_TYPE

            self.attachments.append((filename, content, mimetype))
fields.py 文件源码 项目:sdining 作者: Lurance 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def to_internal_value(self, data):
        try:
            if self.binary or getattr(data, 'is_json_string', False):
                if isinstance(data, six.binary_type):
                    data = data.decode('utf-8')
                return json.loads(data)
            else:
                json.dumps(data)
        except (TypeError, ValueError):
            self.fail('invalid')
        return data
compat.py 文件源码 项目:sdining 作者: Lurance 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def unicode_http_header(value):
    # Coerce HTTP header value to unicode.
    if isinstance(value, six.binary_type):
        return value.decode('iso-8859-1')
    return value
test_encrypted.py 文件源码 项目:django-cryptography 作者: georgemarshall 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def test_settings_has_key(self):
        key = settings.CRYPTOGRAPHY_KEY
        self.assertIsNotNone(key)
        self.assertIsInstance(key, six.binary_type)
elasticsearch2_backend.py 文件源码 项目:elasticsearch2-haystack 作者: NDevox 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def _from_python(self, value):
        """Convert more Python data types to ES-understandable JSON."""
        iso = self._iso_datetime(value)
        if iso:
            return iso
        elif isinstance(value, six.binary_type):
            # TODO: Be stricter.
            return six.text_type(value, errors='replace')
        elif isinstance(value, set):
            return list(value)
        return value
compat.py 文件源码 项目:graphene-jwt-auth 作者: darwin4031 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def unicode_http_header(value):
    # Coerce HTTP header value to unicode.
    if isinstance(value, six.binary_type):
        return value.decode('iso-8859-1')
    return value
base.py 文件源码 项目:generator-django-oscar-app 作者: cage1016 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def context(self):
        ctx = {}
        if six.PY2 and isinstance(self.raw_response, six.text_type):
            self.raw_response = self.raw_response.encode('utf8')
        for key, val in parse_qsl(self.raw_response):
            if isinstance(key, six.binary_type):
                key = key.decode('utf8')
            if isinstance(val, six.binary_type):
                val = val.decode('utf8')
            ctx[key] = [val]
        return ctx
fields.py 文件源码 项目:jianshu-api 作者: strugglingyouth 项目源码 文件源码 阅读 35 收藏 0 点赞 0 评论 0
def to_internal_value(self, data):
        try:
            if self.binary:
                if isinstance(data, six.binary_type):
                    data = data.decode('utf-8')
                return json.loads(data)
            else:
                json.dumps(data)
        except (TypeError, ValueError):
            self.fail('invalid')
        return data
compat.py 文件源码 项目:jianshu-api 作者: strugglingyouth 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def unicode_http_header(value):
    # Coerce HTTP header value to unicode.
    if isinstance(value, six.binary_type):
        return value.decode('iso-8859-1')
    return value
compat.py 文件源码 项目:esdc-ce 作者: erigones 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def unicode_http_header(value):
    # Coerce HTTP header value to unicode.
    if isinstance(value, six.binary_type):
        return value.decode('iso-8859-1')
    return value
message.py 文件源码 项目:lifesoundtrack 作者: MTG 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def attach(self, filename=None, content=None, mimetype=None):
        """
        Attaches a file with the given filename and content. The filename can
        be omitted and the mimetype is guessed, if not provided.

        If the first parameter is a MIMEBase subclass it is inserted directly
        into the resulting message attachments.

        For a text/* mimetype (guessed or specified), when a bytes object is
        specified as content, it will be decoded as UTF-8. If that fails,
        the mimetype will be set to DEFAULT_ATTACHMENT_MIME_TYPE and the
        content is not decoded.
        """
        if isinstance(filename, MIMEBase):
            assert content is None
            assert mimetype is None
            self.attachments.append(filename)
        else:
            assert content is not None

            if not mimetype:
                mimetype, _ = mimetypes.guess_type(filename)
                if not mimetype:
                    mimetype = DEFAULT_ATTACHMENT_MIME_TYPE
            basetype, subtype = mimetype.split('/', 1)

            if basetype == 'text':
                if isinstance(content, six.binary_type):
                    try:
                        content = content.decode('utf-8')
                    except UnicodeDecodeError:
                        # If mimetype suggests the file is text but it's actually
                        # binary, read() will raise a UnicodeDecodeError on Python 3.
                        mimetype = DEFAULT_ATTACHMENT_MIME_TYPE

            self.attachments.append((filename, content, mimetype))
schema.py 文件源码 项目:lifesoundtrack 作者: MTG 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def effective_default(self, field):
        """
        Returns a field's effective database default value
        """
        if field.has_default():
            default = field.get_default()
        elif not field.null and field.blank and field.empty_strings_allowed:
            if field.get_internal_type() == "BinaryField":
                default = six.binary_type()
            else:
                default = six.text_type()
        elif getattr(field, 'auto_now', False) or getattr(field, 'auto_now_add', False):
            default = datetime.now()
            internal_type = field.get_internal_type()
            if internal_type == 'DateField':
                default = default.date
            elif internal_type == 'TimeField':
                default = default.time
            elif internal_type == 'DateTimeField':
                default = timezone.now
        else:
            default = None
        # If it's a callable, call it
        if callable(default):
            default = default()
        # Run it through the field's get_db_prep_save method so we can send it
        # to the database.
        default = field.get_db_prep_save(default, self.connection)
        return default
elasticsearch.py 文件源码 项目:django-haystack-elasticsearch 作者: CraveFood 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def _from_python(self, value):
        """Convert more Python data types to ES-understandable JSON."""
        iso = self._iso_datetime(value)
        if iso:
            return iso
        elif isinstance(value, six.binary_type):
            # TODO: Be stricter.
            return six.text_type(value, errors='replace')
        elif isinstance(value, set):
            return list(value)
        return value
message.py 文件源码 项目:liberator 作者: libscie 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def attach(self, filename=None, content=None, mimetype=None):
        """
        Attaches a file with the given filename and content. The filename can
        be omitted and the mimetype is guessed, if not provided.

        If the first parameter is a MIMEBase subclass it is inserted directly
        into the resulting message attachments.

        For a text/* mimetype (guessed or specified), when a bytes object is
        specified as content, it will be decoded as UTF-8. If that fails,
        the mimetype will be set to DEFAULT_ATTACHMENT_MIME_TYPE and the
        content is not decoded.
        """
        if isinstance(filename, MIMEBase):
            assert content is None
            assert mimetype is None
            self.attachments.append(filename)
        else:
            assert content is not None

            if not mimetype:
                mimetype, _ = mimetypes.guess_type(filename)
                if not mimetype:
                    mimetype = DEFAULT_ATTACHMENT_MIME_TYPE
            basetype, subtype = mimetype.split('/', 1)

            if basetype == 'text':
                if isinstance(content, six.binary_type):
                    try:
                        content = content.decode('utf-8')
                    except UnicodeDecodeError:
                        # If mimetype suggests the file is text but it's actually
                        # binary, read() will raise a UnicodeDecodeError on Python 3.
                        mimetype = DEFAULT_ATTACHMENT_MIME_TYPE

            self.attachments.append((filename, content, mimetype))
schema.py 文件源码 项目:liberator 作者: libscie 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def effective_default(self, field):
        """
        Returns a field's effective database default value
        """
        if field.has_default():
            default = field.get_default()
        elif not field.null and field.blank and field.empty_strings_allowed:
            if field.get_internal_type() == "BinaryField":
                default = six.binary_type()
            else:
                default = six.text_type()
        elif getattr(field, 'auto_now', False) or getattr(field, 'auto_now_add', False):
            default = datetime.now()
            internal_type = field.get_internal_type()
            if internal_type == 'DateField':
                default = default.date
            elif internal_type == 'TimeField':
                default = default.time
            elif internal_type == 'DateTimeField':
                default = timezone.now
        else:
            default = None
        # If it's a callable, call it
        if callable(default):
            default = default()
        # Run it through the field's get_db_prep_save method so we can send it
        # to the database.
        default = field.get_db_prep_save(default, self.connection)
        return default
bulkparse.py 文件源码 项目:nav 作者: UNINETT 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def __init__(self, data, delimiter=None):
        if hasattr(data, 'seek'):
            self.data = data
        else:
            if six.PY3:
                if isinstance(data, six.binary_type):
                    self.data = io.StringIO(data.decode('utf-8'))
                else:
                    self.data = io.StringIO(data)
            else:
                self.data = io.BytesIO(data)

        if delimiter is None:
            try:
                self.dialect = csv.Sniffer().sniff(self.data.read(200),
                                                   delimiters=';:,')
            except csv.Error:
                self.dialect = None
                self.delimiter = ':'
            else:
                self.delimiter = self.dialect.delimiter
            finally:
                self.data.seek(0)

        self.reader = csv.DictReader(CommentStripper(self.data),
                                     fieldnames=self.format,
                                     delimiter=self.delimiter,
                                     restkey=self.restkey)
        self.line_num = 0
bulkimport.py 文件源码 项目:nav 作者: UNINETT 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def _decode_as_utf8(row):
        """Decodes all unicode values in row as utf-8 strings"""
        for key, value in row.items():
            if isinstance(value, six.binary_type):
                row[key] = value.decode('utf-8')
        return row
utils.py 文件源码 项目:nav 作者: UNINETT 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def is_invalid_utf8(string):
    """Returns True if string is invalid UTF-8.

    If string is not a an str object, or is decodeable as UTF-8, False is
    returned.

    """
    if isinstance(string, six.binary_type):
        try:
            string.decode('utf-8')
        except UnicodeDecodeError:
            return True
    return False
report.py 文件源码 项目:nav 作者: UNINETT 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def unicode_utf8(thing):
    """Casts thing to unicode, assuming utf-8 encoding if a binary string.

    If the argument is None, it is returned unchanged.

    """
    if isinstance(thing, six.binary_type):
        return thing.decode('utf-8')
    elif thing is not None:
        return six.text_type(thing)
tracker.py 文件源码 项目:nav 作者: UNINETT 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def scan(addresses):
    """Scan a list of ip-addresses for netbios names"""

    _logger.debug('Scanning %s addresses', len(addresses))
    proc = Popen(['nbtscan', '-f-', '-s', SPLITCHAR], stdin=PIPE, stdout=PIPE)
    stdout, stderr = proc.communicate('\n'.join(addresses))
    if stderr:
        raise Exception(stderr)

    if isinstance(stdout, six.binary_type):
        stdout = stdout.decode('cp850')  # cp850 seems like netbios' standard

    _logger.debug('Result from scan:\n%s', stdout)
    return stdout


问题


面经


文章

微信
公众号

扫码关注公众号