当前位置: 首页>>代码示例>>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;未经允许,请勿转载。