當前位置: 首頁>>代碼示例>>Python>>正文


Python _compat.text_type方法代碼示例

本文整理匯總了Python中flask._compat.text_type方法的典型用法代碼示例。如果您正苦於以下問題:Python _compat.text_type方法的具體用法?Python _compat.text_type怎麽用?Python _compat.text_type使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在flask._compat的用法示例。


在下文中一共展示了_compat.text_type方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: dumps

# 需要導入模塊: from flask import _compat [as 別名]
# 或者: from flask._compat import text_type [as 別名]
def dumps(obj, **kwargs):
    """Serialize ``obj`` to a JSON formatted ``str`` by using the application's
    configured encoder (:attr:`~flask.Flask.json_encoder`) if there is an
    application on the stack.

    This function can return ``unicode`` strings or ascii-only bytestrings by
    default which coerce into unicode strings automatically.  That behavior by
    default is controlled by the ``JSON_AS_ASCII`` configuration variable
    and can be overridden by the simplejson ``ensure_ascii`` parameter.
    """
    _dump_arg_defaults(kwargs)
    encoding = kwargs.pop('encoding', None)
    rv = _json.dumps(obj, **kwargs)
    if encoding is not None and isinstance(rv, text_type):
        rv = rv.encode(encoding)
    return rv 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:18,代碼來源:__init__.py

示例2: test_blueprint_url_definitions

# 需要導入模塊: from flask import _compat [as 別名]
# 或者: from flask._compat import text_type [as 別名]
def test_blueprint_url_definitions(self):
        bp = flask.Blueprint('test', __name__)

        @bp.route('/foo', defaults={'baz': 42})
        def foo(bar, baz):
            return '%s/%d' % (bar, baz)

        @bp.route('/bar')
        def bar(bar):
            return text_type(bar)

        app = flask.Flask(__name__)
        app.register_blueprint(bp, url_prefix='/1', url_defaults={'bar': 23})
        app.register_blueprint(bp, url_prefix='/2', url_defaults={'bar': 19})

        c = app.test_client()
        self.assert_equal(c.get('/1/foo').data, b'23/42')
        self.assert_equal(c.get('/2/foo').data, b'19/42')
        self.assert_equal(c.get('/1/bar').data, b'23')
        self.assert_equal(c.get('/2/bar').data, b'19') 
開發者ID:chalasr,項目名稱:Flask-P2P,代碼行數:22,代碼來源:blueprints.py

示例3: test_template_escaping

# 需要導入模塊: from flask import _compat [as 別名]
# 或者: from flask._compat import text_type [as 別名]
def test_template_escaping(self):
        app = flask.Flask(__name__)
        render = flask.render_template_string
        with app.test_request_context():
            rv = flask.json.htmlsafe_dumps('</script>')
            self.assert_equal(rv, u'"\\u003c/script\\u003e"')
            self.assert_equal(type(rv), text_type)
            rv = render('{{ "</script>"|tojson }}')
            self.assert_equal(rv, '"\\u003c/script\\u003e"')
            rv = render('{{ "<\0/script>"|tojson }}')
            self.assert_equal(rv, '"\\u003c\\u0000/script\\u003e"')
            rv = render('{{ "<!--<script>"|tojson }}')
            self.assert_equal(rv, '"\\u003c!--\\u003cscript\\u003e"')
            rv = render('{{ "&"|tojson }}')
            self.assert_equal(rv, '"\\u0026"')
            rv = render('{{ "\'"|tojson }}')
            self.assert_equal(rv, '"\\u0027"')
            rv = render("<a ng-data='{{ data|tojson }}'></a>",
                data={'x': ["foo", "bar", "baz'"]})
            self.assert_equal(rv,
                '<a ng-data=\'{"x": ["foo", "bar", "baz\\u0027"]}\'></a>') 
開發者ID:chalasr,項目名稱:Flask-P2P,代碼行數:23,代碼來源:helpers.py

示例4: test_session_transactions

# 需要導入模塊: from flask import _compat [as 別名]
# 或者: from flask._compat import text_type [as 別名]
def test_session_transactions(self):
        app = flask.Flask(__name__)
        app.testing = True
        app.secret_key = 'testing'

        @app.route('/')
        def index():
            return text_type(flask.session['foo'])

        with app.test_client() as c:
            with c.session_transaction() as sess:
                self.assert_equal(len(sess), 0)
                sess['foo'] = [42]
                self.assert_equal(len(sess), 1)
            rv = c.get('/')
            self.assert_equal(rv.data, b'[42]')
            with c.session_transaction() as sess:
                self.assert_equal(len(sess), 1)
                self.assert_equal(sess['foo'], [42]) 
開發者ID:chalasr,項目名稱:Flask-P2P,代碼行數:21,代碼來源:testing.py

示例5: default

# 需要導入模塊: from flask import _compat [as 別名]
# 或者: from flask._compat import text_type [as 別名]
def default(self, o):
        """Implement this method in a subclass such that it returns a
        serializable object for ``o``, or calls the base implementation (to
        raise a :exc:`TypeError`).

        For example, to support arbitrary iterators, you could implement
        default like this::

            def default(self, o):
                try:
                    iterable = iter(o)
                except TypeError:
                    pass
                else:
                    return list(iterable)
                return JSONEncoder.default(self, o)
        """
        if isinstance(o, datetime):
            return http_date(o.utctimetuple())
        if isinstance(o, date):
            return http_date(o.timetuple())
        if isinstance(o, uuid.UUID):
            return str(o)
        if hasattr(o, '__html__'):
            return text_type(o.__html__())
        return _json.JSONEncoder.default(self, o) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:28,代碼來源:__init__.py

示例6: htmlsafe_dump

# 需要導入模塊: from flask import _compat [as 別名]
# 或者: from flask._compat import text_type [as 別名]
def htmlsafe_dump(obj, fp, **kwargs):
    """Like :func:`htmlsafe_dumps` but writes into a file object."""
    fp.write(text_type(htmlsafe_dumps(obj, **kwargs))) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:5,代碼來源:__init__.py

示例7: to_json

# 需要導入模塊: from flask import _compat [as 別名]
# 或者: from flask._compat import text_type [as 別名]
def to_json(self, value):
        return text_type(value.__html__()) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:4,代碼來源:tag.py

示例8: default

# 需要導入模塊: from flask import _compat [as 別名]
# 或者: from flask._compat import text_type [as 別名]
def default(self, o):
        if hasattr(o, '__json__') and callable(o.__json__):
            return o.__json__()
        if isinstance(o, (date,
                          datetime,
                          time)):
            return o.isoformat()[:19].replace('T', ' ')
        elif isinstance(o, (int, long)):
            return int(o)
        elif isinstance(o, decimal.Decimal):
            return str(o)
        elif hasattr(o, '__html__'):
            return text_type(o.__html__())
        return _JSONEncoder.default(self, o) 
開發者ID:whiteclover,項目名稱:white,代碼行數:16,代碼來源:patch.py

示例9: dumps

# 需要導入模塊: from flask import _compat [as 別名]
# 或者: from flask._compat import text_type [as 別名]
def dumps(obj, app=None, **kwargs):
    """Serialize ``obj`` to a JSON-formatted string. If there is an
    app context pushed, use the current app's configured encoder
    (:attr:`~flask.Flask.json_encoder`), or fall back to the default
    :class:`JSONEncoder`.

    Takes the same arguments as the built-in :func:`json.dumps`, and
    does some extra configuration based on the application. If the
    simplejson package is installed, it is preferred.

    :param obj: Object to serialize to JSON.
    :param app: App instance to use to configure the JSON encoder.
        Uses ``current_app`` if not given, and falls back to the default
        encoder when not in an app context.
    :param kwargs: Extra arguments passed to :func:`json.dumps`.

    .. versionchanged:: 1.0.3

        ``app`` can be passed directly, rather than requiring an app
        context for configuration.
    """
    _dump_arg_defaults(kwargs, app=app)
    encoding = kwargs.pop('encoding', None)
    rv = _json.dumps(obj, **kwargs)
    if encoding is not None and isinstance(rv, text_type):
        rv = rv.encode(encoding)
    return rv 
開發者ID:PacktPublishing,項目名稱:Building-Recommendation-Systems-with-Python,代碼行數:29,代碼來源:__init__.py


注:本文中的flask._compat.text_type方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。