当前位置: 首页>>代码示例>>Python>>正文


Python six.b_函数代码示例

本文整理汇总了Python中six.b_函数的典型用法代码示例。如果您正苦于以下问题:Python b_函数的具体用法?Python b_怎么用?Python b_使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了b_函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_streaming_response

    def test_streaming_response(self):

        class RootController(object):
            @expose(content_type='text/plain')
            def test(self, foo):
                if foo == 'stream':
                    # mimic large file
                    contents = six.BytesIO(b_('stream'))
                    response.content_type = 'application/octet-stream'
                    contents.seek(0, os.SEEK_END)
                    response.content_length = contents.tell()
                    contents.seek(0, os.SEEK_SET)
                    response.app_iter = contents
                    return response
                else:
                    return 'plain text'

        app = TestApp(Pecan(RootController()))
        r = app.get('/test/stream')
        assert r.content_type == 'application/octet-stream'
        assert r.body == b_('stream')

        r = app.get('/test/plain')
        assert r.content_type == 'text/plain'
        assert r.body == b_('plain text')
开发者ID:cleverdevil,项目名称:pecan,代码行数:25,代码来源:test_base.py

示例2: test_simple_generic

    def test_simple_generic(self):
        class RootController(object):
            @expose(generic=True)
            def index(self):
                pass

            @index.when(method='POST', template='json')
            def do_post(self):
                return dict(result='POST')

            @index.when(method='GET')
            def do_get(self):
                return 'GET'

        app = TestApp(Pecan(RootController()))
        r = app.get('/')
        assert r.status_int == 200
        assert r.body == b_('GET')

        r = app.post('/')
        assert r.status_int == 200
        assert r.body == b_(dumps(dict(result='POST')))

        r = app.get('/do_get', status=404)
        assert r.status_int == 404
开发者ID:adamchainz,项目名称:pecan,代码行数:25,代码来源:test_generic.py

示例3: test_get_with_var_args

    def test_get_with_var_args(self):

        class OthersController(object):

            @expose()
            def index(self, one, two, three):
                return 'NESTED: %s, %s, %s' % (one, two, three)

        class ThingsController(RestController):

            others = OthersController()

            @expose()
            def get_one(self, *args):
                return ', '.join(args)

        class RootController(object):
            things = ThingsController()

        # create the app
        app = TestApp(make_app(RootController()))

        # test get request
        r = app.get('/things/one/two/three')
        assert r.status_int == 200
        assert r.body == b_('one, two, three')

        # test nested get request
        r = app.get('/things/one/two/three/others/')
        assert r.status_int == 200
        assert r.body == b_('NESTED: one, two, three')
开发者ID:alex-devops,项目名称:pecan,代码行数:31,代码来源:test_rest.py

示例4: test_secure_attribute

    def test_secure_attribute(self):
        authorized = False

        class SubController(object):
            @expose()
            def index(self):
                return 'Hello from sub!'

        class RootController(object):
            @expose()
            def index(self):
                return 'Hello from root!'

            sub = secure(SubController(), lambda: authorized)

        app = TestApp(make_app(RootController()))
        response = app.get('/')
        assert response.status_int == 200
        assert response.body == b_('Hello from root!')

        response = app.get('/sub/', expect_errors=True)
        assert response.status_int == 401

        authorized = True
        response = app.get('/sub/')
        assert response.status_int == 200
        assert response.body == b_('Hello from sub!')
开发者ID:citrix-openstack-build,项目名称:pecan,代码行数:27,代码来源:test_secure.py

示例5: test_mako

    def test_mako(self):

        class RootController(object):
            @expose('mako:mako.html')
            def index(self, name='Jonathan'):
                return dict(name=name)

            @expose('mako:mako_bad.html')
            def badtemplate(self):
                return dict()

        app = TestApp(
            Pecan(RootController(), template_path=self.template_path)
        )
        r = app.get('/')
        assert r.status_int == 200
        assert b_("<h1>Hello, Jonathan!</h1>") in r.body

        r = app.get('/index.html?name=World')
        assert r.status_int == 200
        assert b_("<h1>Hello, World!</h1>") in r.body

        error_msg = None
        try:
            r = app.get('/badtemplate.html')
        except Exception as e:
            for error_f in error_formatters:
                error_msg = error_f(e)
                if error_msg:
                    break
        assert error_msg is not None
开发者ID:cleverdevil,项目名称:pecan,代码行数:31,代码来源:test_base.py

示例6: test_nested_generic

    def test_nested_generic(self):

        class SubSubController(object):
            @expose(generic=True)
            def index(self):
                return 'GET'

            @index.when(method='DELETE', template='json')
            def do_delete(self, name, *args):
                return dict(result=name, args=', '.join(args))

        class SubController(object):
            sub = SubSubController()

        class RootController(object):
            sub = SubController()

        app = TestApp(Pecan(RootController()))
        r = app.get('/sub/sub/')
        assert r.status_int == 200
        assert r.body == b_('GET')

        r = app.delete('/sub/sub/joe/is/cool')
        assert r.status_int == 200
        assert r.body == b_(dumps(dict(result='joe', args='is, cool')))
开发者ID:adamchainz,项目名称:pecan,代码行数:25,代码来源:test_generic.py

示例7: test_isolated_hook_with_global_hook

    def test_isolated_hook_with_global_hook(self):
        run_hook = []

        class SimpleHook(PecanHook):
            def __init__(self, id):
                self.id = str(id)

            def on_route(self, state):
                run_hook.append("on_route" + self.id)

            def before(self, state):
                run_hook.append("before" + self.id)

            def after(self, state):
                run_hook.append("after" + self.id)

            def on_error(self, state, e):
                run_hook.append("error" + self.id)

        class SubController(HookController):
            __hooks__ = [SimpleHook(2)]

            @expose()
            def index(self):
                run_hook.append("inside_sub")
                return "Inside here!"

        class RootController(object):
            @expose()
            def index(self):
                run_hook.append("inside")
                return "Hello, World!"

            sub = SubController()

        app = TestApp(make_app(RootController(), hooks=[SimpleHook(1)]))
        response = app.get("/")
        assert response.status_int == 200
        assert response.body == b_("Hello, World!")

        assert len(run_hook) == 4
        assert run_hook[0] == "on_route1"
        assert run_hook[1] == "before1"
        assert run_hook[2] == "inside"
        assert run_hook[3] == "after1"

        run_hook = []

        response = app.get("/sub/")
        assert response.status_int == 200
        assert response.body == b_("Inside here!")

        assert len(run_hook) == 6
        assert run_hook[0] == "on_route1"
        assert run_hook[1] == "before2"
        assert run_hook[2] == "before1"
        assert run_hook[3] == "inside_sub"
        assert run_hook[4] == "after1"
        assert run_hook[5] == "after2"
开发者ID:LonelyWhale,项目名称:openstackDemo,代码行数:59,代码来源:test_hooks.py

示例8: test_nested_get_all_with_lookup

    def test_nested_get_all_with_lookup(self):

        class BarsController(RestController):

            @expose()
            def get_one(self, foo_id, id):
                return '4'

            @expose()
            def get_all(self, foo_id):
                return '3'

            @expose('json')
            def _lookup(self, id, *remainder):
                redirect('/lookup-hit/')

        class FoosController(RestController):

            bars = BarsController()

            @expose()
            def get_one(self, id):
                return '2'

            @expose()
            def get_all(self):
                return '1'

        class RootController(object):
            foos = FoosController()

        # create the app
        app = TestApp(make_app(RootController()))

        r = app.get('/foos/')
        assert r.status_int == 200
        assert r.body == b_('1')

        r = app.get('/foos/1/')
        assert r.status_int == 200
        assert r.body == b_('2')

        r = app.get('/foos/1/bars/')
        assert r.status_int == 200
        assert r.body == b_('3')

        r = app.get('/foos/1/bars/2/')
        assert r.status_int == 200
        assert r.body == b_('4')

        r = app.get('/foos/bars/', status=400)
        assert r.status_int == 400

        r = app.get('/foos/bars/', status=400)

        r = app.get('/foos/bars/1')
        assert r.status_int == 302
        assert r.headers['Location'].endswith('/lookup-hit/')
开发者ID:coderpete,项目名称:pecan,代码行数:58,代码来源:test_rest.py

示例9: test_simple_secure

    def test_simple_secure(self):
        authorized = False

        class SecretController(SecureController):
            @expose()
            def index(self):
                return 'Index'

            @expose()
            @unlocked
            def allowed(self):
                return 'Allowed!'

            @classmethod
            def check_permissions(cls):
                return authorized

        class RootController(object):
            @expose()
            def index(self):
                return 'Hello, World!'

            @expose()
            @secure(lambda: False)
            def locked(self):
                return 'No dice!'

            @expose()
            @secure(lambda: True)
            def unlocked(self):
                return 'Sure thing'

            secret = SecretController()

        app = TestApp(make_app(
            RootController(),
            debug=True,
            static_root='tests/static'
        ))
        response = app.get('/')
        assert response.status_int == 200
        assert response.body == b_('Hello, World!')

        response = app.get('/unlocked')
        assert response.status_int == 200
        assert response.body == b_('Sure thing')

        response = app.get('/locked', expect_errors=True)
        assert response.status_int == 401

        response = app.get('/secret/', expect_errors=True)
        assert response.status_int == 401

        response = app.get('/secret/allowed')
        assert response.status_int == 200
        assert response.body == b_('Allowed!')
开发者ID:citrix-openstack-build,项目名称:pecan,代码行数:56,代码来源:test_secure.py

示例10: test_isolation_level

    def test_isolation_level(self):
        run_hook = []

        class RootController(object):
            @expose(generic=True)
            def index(self):
                run_hook.append('inside')
                return 'I should not be isolated'

            @isolation_level('SERIALIZABLE')
            @index.when(method='POST')
            def isolated(self):
                run_hook.append('inside')
                return "I should be isolated"

        def gen(event):
            return lambda: run_hook.append(event)

        def gen_start(event):
            return lambda level: run_hook.append(' '.join((event, level)))

        def start(isolation_level=''):
            run_hook.append('start ' + isolation_level)

        app = TestApp(make_app(RootController(), hooks=[
            IsolatedTransactionHook(
                start=start,
                start_ro=gen('start_ro'),
                commit=gen('commit'),
                rollback=gen('rollback'),
                clear=gen('clear')
            )
        ]))

        run_hook = []
        response = app.get('/')
        assert response.status_int == 200
        assert response.body == b_('I should not be isolated')

        assert len(run_hook) == 3
        assert run_hook[0] == 'start_ro'
        assert run_hook[1] == 'inside'
        assert run_hook[2] == 'clear'

        run_hook = []
        response = app.post('/')
        assert response.status_int == 200
        assert response.body == b_('I should be isolated')

        assert len(run_hook) == 5
        assert run_hook[0] == 'start '
        assert run_hook[1] == 'start SERIALIZABLE'
        assert run_hook[2] == 'inside'
        assert run_hook[3] == 'commit'
        assert run_hook[4] == 'clear'
开发者ID:ceph,项目名称:paddles,代码行数:55,代码来源:test_isolated.py

示例11: error_docs_app

def error_docs_app(environ, start_response):
    if environ["PATH_INFO"] == "/not_found":
        start_response("404 Not found", [("Content-type", "text/plain")])
        return [b_("Not found")]
    elif environ["PATH_INFO"] == "/error":
        start_response("200 OK", [("Content-type", "text/plain")])
        return [b_("Page not found")]
    elif environ["PATH_INFO"] == "/recurse":
        raise ForwardRequestException("/recurse")
    else:
        return simple_app(environ, start_response)
开发者ID:alfredodeza,项目名称:pecan,代码行数:11,代码来源:test_recursive.py

示例12: test_simple_secure

    def test_simple_secure(self):
        authorized = False

        class SecretController(SecureController):
            @expose()
            def index(self):
                return "Index"

            @expose()
            @unlocked
            def allowed(self):
                return "Allowed!"

            @classmethod
            def check_permissions(cls):
                return authorized

        class RootController(object):
            @expose()
            def index(self):
                return "Hello, World!"

            @expose()
            @secure(lambda: False)
            def locked(self):
                return "No dice!"

            @expose()
            @secure(lambda: True)
            def unlocked(self):
                return "Sure thing"

            secret = SecretController()

        app = TestApp(make_app(RootController(), debug=True, static_root="tests/static"))
        response = app.get("/")
        assert response.status_int == 200
        assert response.body == b_("Hello, World!")

        response = app.get("/unlocked")
        assert response.status_int == 200
        assert response.body == b_("Sure thing")

        response = app.get("/locked", expect_errors=True)
        assert response.status_int == 401

        response = app.get("/secret/", expect_errors=True)
        assert response.status_int == 401

        response = app.get("/secret/allowed")
        assert response.status_int == 200
        assert response.body == b_("Allowed!")
开发者ID:alfredodeza,项目名称:pecan,代码行数:52,代码来源:test_secure.py

示例13: test_nested_get_all

    def test_nested_get_all(self):

        class BarsController(RestController):

            @expose()
            def get_one(self, foo_id, id):
                return '4'

            @expose()
            def get_all(self, foo_id):
                return '3'

        class FoosController(RestController):

            bars = BarsController()

            @expose()
            def get_one(self, id):
                return '2'

            @expose()
            def get_all(self):
                return '1'

        class RootController(object):
            foos = FoosController()

        # create the app
        app = TestApp(make_app(RootController()))

        r = app.get('/foos/')
        assert r.status_int == 200
        assert r.body == b_('1')

        r = app.get('/foos/1/')
        assert r.status_int == 200
        assert r.body == b_('2')

        r = app.get('/foos/1/bars/')
        assert r.status_int == 200
        assert r.body == b_('3')

        r = app.get('/foos/1/bars/2/')
        assert r.status_int == 200
        assert r.body == b_('4')

        r = app.get('/foos/bars/', status=404)
        assert r.status_int == 404

        r = app.get('/foos/bars/1', status=404)
        assert r.status_int == 404
开发者ID:alex-devops,项目名称:pecan,代码行数:51,代码来源:test_rest.py

示例14: test_custom_with_trailing_slash

    def test_custom_with_trailing_slash(self):

        class CustomController(RestController):

            _custom_actions = {
                'detail': ['GET'],
                'create': ['POST'],
                'update': ['PUT'],
                'remove': ['DELETE'],
            }

            @expose()
            def detail(self):
                return 'DETAIL'

            @expose()
            def create(self):
                return 'CREATE'

            @expose()
            def update(self, id):
                return id

            @expose()
            def remove(self, id):
                return id

        app = TestApp(make_app(CustomController()))

        r = app.get('/detail')
        assert r.status_int == 200
        assert r.body == b_('DETAIL')

        r = app.get('/detail/')
        assert r.status_int == 200
        assert r.body == b_('DETAIL')

        r = app.post('/create')
        assert r.status_int == 200
        assert r.body == b_('CREATE')

        r = app.post('/create/')
        assert r.status_int == 200
        assert r.body == b_('CREATE')

        r = app.put('/update/123')
        assert r.status_int == 200
        assert r.body == b_('123')

        r = app.put('/update/123/')
        assert r.status_int == 200
        assert r.body == b_('123')

        r = app.delete('/remove/456')
        assert r.status_int == 200
        assert r.body == b_('456')

        r = app.delete('/remove/456/')
        assert r.status_int == 200
        assert r.body == b_('456')
开发者ID:alex-devops,项目名称:pecan,代码行数:60,代码来源:test_rest.py

示例15: test_kajiki

    def test_kajiki(self):
        class RootController(object):
            @expose("kajiki:kajiki.html")
            def index(self, name="Jonathan"):
                return dict(name=name)

        app = TestApp(Pecan(RootController(), template_path=self.template_path))
        r = app.get("/")
        assert r.status_int == 200
        assert b_("<h1>Hello, Jonathan!</h1>") in r.body

        r = app.get("/index.html?name=World")
        assert r.status_int == 200
        assert b_("<h1>Hello, World!</h1>") in r.body
开发者ID:rackerlabs,项目名称:pecan,代码行数:14,代码来源:test_base.py


注:本文中的six.b_函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。