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


Python Accept.best_match方法代码示例

本文整理汇总了Python中webob.acceptparse.Accept.best_match方法的典型用法代码示例。如果您正苦于以下问题:Python Accept.best_match方法的具体用法?Python Accept.best_match怎么用?Python Accept.best_match使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在webob.acceptparse.Accept的用法示例。


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

示例1: test_best_match

# 需要导入模块: from webob.acceptparse import Accept [as 别名]
# 或者: from webob.acceptparse.Accept import best_match [as 别名]
def test_best_match():
    accept = Accept("text/html, foo/bar")
    assert accept.best_match(["text/html", "foo/bar"]) == "text/html"
    assert accept.best_match(["foo/bar", "text/html"]) == "foo/bar"
    assert accept.best_match([("foo/bar", 0.5), "text/html"]) == "text/html"
    assert accept.best_match([("foo/bar", 0.5), ("text/html", 0.4)]) == "foo/bar"
    assert_raises(ValueError, accept.best_match, ["text/*"])
开发者ID:ckey,项目名称:webob,代码行数:9,代码来源:test_acceptparse.py

示例2: test_best_match_with_complex_q

# 需要导入模块: from webob.acceptparse import Accept [as 别名]
# 或者: from webob.acceptparse.Accept import best_match [as 别名]
def test_best_match_with_complex_q():
    accept = Accept('text/html, foo/bar;q=0.55, baz/gort;q=0.59')
    assert accept.best_match(['text/html', 'foo/bar']) == 'text/html'
    accept = Accept('text/html;q=0.5, foo/bar;q=0.586, baz/gort;q=0.5966')
    assert "baz/gort;q=0.597" in str(accept)
    assert "foo/bar;q=0.586" in str(accept)
    assert "text/html;q=0.5" in str(accept)
    assert accept.best_match(['text/html', 'baz/gort']) == 'baz/gort'
开发者ID:B-Rich,项目名称:webob,代码行数:10,代码来源:test_acceptparse.py

示例3: test_best_match

# 需要导入模块: from webob.acceptparse import Accept [as 别名]
# 或者: from webob.acceptparse.Accept import best_match [as 别名]
def test_best_match():
    accept = Accept('Content-Type', 'text/html, foo/bar')
    assert accept.best_match(['text/html', 'foo/bar']) == 'text/html'
    assert accept.best_match(['foo/bar', 'text/html']) == 'foo/bar'
    assert accept.best_match([('foo/bar', 0.5),
                              'text/html']) == 'text/html'
    assert accept.best_match([('foo/bar', 0.5),
                              ('text/html', 0.4)]) == 'foo/bar'
    assert_raises(ValueError, accept.best_match, ['text/*'])
开发者ID:AgentJay,项目名称:webapp-improved,代码行数:11,代码来源:test_acceptparse.py

示例4: test_best_match

# 需要导入模块: from webob.acceptparse import Accept [as 别名]
# 或者: from webob.acceptparse.Accept import best_match [as 别名]
def test_best_match():
    accept = Accept('text/html, foo/bar')
    assert accept.best_match(['text/html', 'foo/bar']) == 'text/html'
    assert accept.best_match(['foo/bar', 'text/html']) == 'foo/bar'
    assert accept.best_match([('foo/bar', 0.5),
                              'text/html']) == 'text/html'
    assert accept.best_match([('foo/bar', 0.5),
                              ('text/html', 0.4)]) == 'foo/bar'
    with pytest.raises(ValueError):
        accept.best_match(['text/*'])
开发者ID:adamchainz,项目名称:webob,代码行数:12,代码来源:test_acceptparse.py

示例5: generate_response

# 需要导入模块: from webob.acceptparse import Accept [as 别名]
# 或者: from webob.acceptparse.Accept import best_match [as 别名]
 def generate_response(self, environ, start_response):
     if self.content_length is not None:
         del self.content_length
     headerlist = list(self.headerlist)
     accept_value = environ.get('HTTP_ACCEPT', '')
     accept = Accept(accept_value)
     match = accept.best_match(['application/json', 'text/html',
                                'text/plain'], default_match='text/plain')
     if match == 'text/html':
         content_type = 'text/html'
         body = self.html_body(environ)
     elif match == 'application/json':
         content_type = 'application/json'
         body = self.json_body(environ)
     else:
         content_type = 'text/plain'
         body = self.plain_body(environ)
     extra_kw = {}
     if isinstance(body, text_type):
         extra_kw.update(charset='utf-8')
     resp = Response(body,
         status=self.status,
         headerlist=headerlist,
         content_type=content_type,
         **extra_kw
     )
     resp.content_type = content_type
     return resp(environ, start_response)
开发者ID:5m0k3r,项目名称:desarrollo_web_udp,代码行数:30,代码来源:exc.py

示例6: concept

# 需要导入模块: from webob.acceptparse import Accept [as 别名]
# 或者: from webob.acceptparse.Accept import best_match [as 别名]
def concept(request, lccn):
    concept = get_object_or_404(m.Concept, lccn=lccn)
    host = request.get_host()

    accept = Accept('Accept', request.META.get('HTTP_ACCEPT', 'text/html'))
    best_match = accept.best_match(['text/html', 'application/rdf+xml']) 
    if best_match == 'application/rdf+xml':
        r = concept_rdf(request, lccn)
    else:
        alt_labels = concept.alternate_labels.all().order_by('text')
        broader = concept.broader.all().order_by('pref_label')
        extra_broader = concept.extra_broader()
        narrower = concept.narrower.all().order_by('pref_label')
        related = concept.related.all().order_by('pref_label')
        r = render_to_response('concept.html', dictionary=locals(), 
                               context_instance=RequestContext(request))
    # let downstream caches know that the resonse can vary depending 
    # on the Accept header that the client sent 
    r['Vary'] = 'Accept' 
    return r
开发者ID:edsu,项目名称:id,代码行数:22,代码来源:views.py

示例7: checkMimetype

# 需要导入模块: from webob.acceptparse import Accept [as 别名]
# 或者: from webob.acceptparse.Accept import best_match [as 别名]
def checkMimetype(acceptHeader):
    accept = Accept('Accept', acceptHeader)
    best = accept.best_match(['application/rdf', 'application/rdf+xml', 'text/n3', 'application/xml', 'application/json', 'text/xml', 'application/xhtml+xml'])
    if best is None:
        best = "text/html"
    return best
开发者ID:zeitkunst,项目名称:JJPS,代码行数:8,代码来源:server.py

示例8: test_best_match_with_one_lower_q

# 需要导入模块: from webob.acceptparse import Accept [as 别名]
# 或者: from webob.acceptparse.Accept import best_match [as 别名]
def test_best_match_with_one_lower_q():
    accept = Accept('Content-Type', 'text/html, foo/bar;q=0.5')
    assert accept.best_match(['text/html', 'foo/bar']) == 'text/html'
    accept = Accept('Content-Type', 'text/html;q=0.5, foo/bar')
    assert accept.best_match(['text/html', 'foo/bar']) == 'foo/bar'
开发者ID:AgentJay,项目名称:webapp-improved,代码行数:7,代码来源:test_acceptparse.py

示例9: WebController

# 需要导入模块: from webob.acceptparse import Accept [as 别名]
# 或者: from webob.acceptparse.Accept import best_match [as 别名]

#.........这里部分代码省略.........
        return self._session_key

    def delete_cookie(self, key):
        self.__response.delete_cookie(key)

    def set_cookie(self, key):
        value = self.new_session
        self.__response.set_cookie(key, bytes(value))
        return value

    def process(self, environ, start_response):
        self._begin = datetime.datetime.now()
        if self._debug:
            open("/tmp/cog_sql", "w")
        try:
            self.clear()
            self._environ = environ
#            open("/tmp/cog_tmp_xxx", "w").write("{}".format(environ))
            self._script_name = self._environ.get('SCRIPT_NAME', '')
            self._server_name = self._environ['HTTP_HOST']
            self._url_scheme = self._environ['wsgi.url_scheme']
            self._url = "{}://{}{}".format(
                self._url_scheme, self._server_name, self._script_name)
            self._path_info = self._environ['PATH_INFO'].lower()
            alnum = "[a-z0-9]{4}"
            oid_pattern = '^/{}{}-{}-{}-{}-{}{}{}$'.format(*(alnum,)*8)
            if re.match(oid_pattern, self._path_info):
                obj = self.db.get_elt_by_oid(self._path_info[1:])
                self.add_json_res({'#page_ref':self.get_page_ref()})
                self.direct_obj_ref = obj.w3display
            self.load_site()
            self.__request = Request(environ)
            self.__lang = Accept(str(self.__request.accept_language))
            self.i18n = gettext.translation(
                'messages', '/usr/share/collorg/locale',
                [self.__lang.best_match(('en', 'fr', 'fr-FR')) or 'en'])
            if WebController.__static_body is None:
                WebController.__tags = self.__get_tags()
                WebController.__static_body = self.__get_static_body()
            self.__response = Response()
            self.__reset()
            if not(self._cog_method is None and '#page_ref' in self._json_res):
                    self.add_json_res({'#page_ref':self.get_page_ref()})
            self.__response.unicode_body = self._unicode(self.__exec())
            if(self.__response.content_type != 'text/html' or
                self.__response.status_int != 200):
                # Not an HTML response, we don't want to
                # do anything to it
                return self.__response(environ, start_response)
            # Make sure the content isn't gzipped:
            self.__response.decode_content()
            return self.__response(environ, start_response)
        except Exception as err:
            if self.db._cog_params['debug']:
                response = Response()
                response.unicode_body = self._unicode(err)
                return response(environ, start_response)
            if raise_error:
                raise err

    def debug(self, error = None):
        output = []
        css_style = 'debug hidden toggle'
        css_error = ''
        if error:
            css_style += ' debug_error'
            css_error = 'debug_error'
        title = error and "Error" or "debug"
        link = ( '<span class="link toggle %s" '
            'to_toggle="#cog_debug">%s</span>' % (css_error, title))
        if error:
            output.append('<h2>Error</h2>')
            output.append('<pre>%s</pre>' % (self.__pfce(error)))
            output.append("<h2>Traceback</h2>")
            output.append("<pre>%s</pre>"%(
                traceback.format_exc()))
        output.append('<h2>Main variables</h2>')
        output.append('<pre>')
        output.append('cog_method = %s\n' % (self._cog_method))
        output.append('cog_fqtn_ = %s\n' % (self._cog_fqtn_))
        output.append('cog_oid_ = %s\n' % (self._cog_oid_))
        output.append('cog_ref_oid = %s\n' % (self._cog_ref_oid))
        output.append('</pre>')
        if error:
            post_error = self.db.table(
                'collorg.application.communication.error')
            try:
                post_error.hit(
                    self._cog_fqtn_, self._cog_method, traceback.format_exc())
            except Exception, err:
                sys.stderr.write("Warning! {}\n".format(err))
#                open("/tmp/cog_error_log", "a+").write("{}\n".format(err))
        output.append('<h2>Query string</h2>')
        output.append('<pre>qs = %s</pre>' % (self.__pfce(self.__get_request)))
        output.append('<h2>Command executed</h2>')
        output.append('<pre>cmd = %s</pre>' %(self.__pfce(self._cog_cmd)))
        output.append('<h2>Environ</h2>')
        output.append('<pre>%s</pre>' % (self.__pfce(self._environ)))
        u_output = [self._unicode(elt) for elt in output]
        return link, "\n".join(u_output)
开发者ID:joel-m,项目名称:collorg,代码行数:104,代码来源:web.py

示例10: test_best_match_with_one_lower_q

# 需要导入模块: from webob.acceptparse import Accept [as 别名]
# 或者: from webob.acceptparse.Accept import best_match [as 别名]
def test_best_match_with_one_lower_q():
    accept = Accept("text/html, foo/bar;q=0.5")
    assert accept.best_match(["text/html", "foo/bar"]) == "text/html"
    accept = Accept("text/html;q=0.5, foo/bar")
    assert accept.best_match(["text/html", "foo/bar"]) == "foo/bar"
开发者ID:ckey,项目名称:webob,代码行数:7,代码来源:test_acceptparse.py


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