本文整理汇总了Python中dictionary.Dictionary.init_dict方法的典型用法代码示例。如果您正苦于以下问题:Python Dictionary.init_dict方法的具体用法?Python Dictionary.init_dict怎么用?Python Dictionary.init_dict使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类dictionary.Dictionary
的用法示例。
在下文中一共展示了Dictionary.init_dict方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: search
# 需要导入模块: from dictionary import Dictionary [as 别名]
# 或者: from dictionary.Dictionary import init_dict [as 别名]
def search(request, page=1):
"""
Search page
"""
print "dsdf %s" % request
if request.POST:
if request.is_ajax():
print "ajax!"
spell_check = Dictionary()
spell_check.init_dict()
required = request.POST.getlist("required[]")
corrected_required = []
for term in required:
correct_term = spell_check.correct(term)
corrected_required.append(correct_term)
required = corrected_required
optional = request.POST.getlist("optional[]")
corrected_optional = []
for term in optional:
correct_term = spell_check.correct(term)
corrected_optional.append(correct_term)
optional = corrected_optional
print "required: %s" % required
drinks = []
master_set = set(Drink.objects.all())
if required:
for term in required:
results = Drink.objects.filter(Q(name__contains=term) | Q(ingredients__name__contains=term))
master_set = master_set & set(results)
drinks = list(master_set)
else:
for term in optional:
drinks.extend(
list(Drink.objects.filter(Q(name__contains=term) | Q(ingredients__name__contains=term)).distinct())
)
drinks = list(set(drinks))
if len(drinks) == 0:
drinks_list = []
else:
drinks_tuple = rank(required + optional, drinks)
drinks_list = []
print "Starting views"
for drink in drinks_tuple:
drinks_list.append(drink[0])
print "%s" % drink[0].name.encode("utf-8")
json_drinks = []
for drink in drinks_list:
curr_drink = {}
curr_drink["name"] = drink.name
curr_drink["directions"] = drink.directions
curr_drink["url"] = drink.url
curr_drink["ingredients"] = []
for ingredient in drink.ingredients.all():
curr_ingredient = {}
curr_ingredient["name"] = ingredient.name
curr_ingredient["unit"] = ingredient.unit
curr_ingredient["amount"] = ingredient.amount
curr_drink["ingredients"].append(curr_ingredient)
json_drinks.append(curr_drink)
return HttpResponse(
json.dumps(json_drinks), mimetype="application/json"
) # render_to_response('search.html', {'form': form,
#'results':drinks[(page-1)*500:page*500],
#'page':page,
#'next':page+1,
#'prev':page-1},
# context_instance=RequestContext(request))
else:
form = Search_Form()
return render_to_response("search.html", {"form": form}, context_instance=RequestContext(request))