关于谷歌的示例代码 [英] About example code from google

查看:142
本文介绍了关于谷歌的示例代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这样做

  uri = urlparse(self.request.uri)
if uri.query:
query = parse_qs(uri.query)
try:
query = query ['query'] [0]
除了KeyError,err:
query =''

而不仅仅是这个

query = self.request.get('query')



?两个代码块之间有什么区别?

来源。 https ://code.google.com/p/google-app-engine-samples/source/browse/trunk/search/python/search_demo.py?r = 157

解决方案

有一些差异。最重要的是,如果关键字 query 在uri中多次出现,第一个版本将返回第一个实例,而第二个版本将返回最后一个实例。



此外,在第二个版本中,如果该键不存在,您将获得 None 而不是 ''



潜在地,v1的作者不相信webob会正确解析他的查询字符串。


Why is this done

uri = urlparse(self.request.uri)
if uri.query:
    query = parse_qs(uri.query)
    try:
        query = query['query'][0]
    except KeyError, err:
        query = ''

And not simply this

query = self.request.get('query')

? What will the difference be between to two code blocks?

Source. https://code.google.com/p/google-app-engine-samples/source/browse/trunk/search/python/search_demo.py?r=157

解决方案

There's a few differences. The biggest is if the key query appears multiple times in the uri, the first version will return the first instance while the second version will return the last instance.

Also, in the second version, if the key doesn't exist you'll get None instead of ''

Potentially the author of v1 just didn't trust webob to parse his query string properly.

这篇关于关于谷歌的示例代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆