本文整理汇总了Python中utils.Timer.check_expired方法的典型用法代码示例。如果您正苦于以下问题:Python Timer.check_expired方法的具体用法?Python Timer.check_expired怎么用?Python Timer.check_expired使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类utils.Timer
的用法示例。
在下文中一共展示了Timer.check_expired方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: search
# 需要导入模块: from utils import Timer [as 别名]
# 或者: from utils.Timer import check_expired [as 别名]
def search(dbfile, query, year=None, size=5, debug=False, timeout=None):
"""Search the database for query, optionally with an estimated year."""
this_year = date.today().year
if year:
year = int(year)
words = query.split()
timer = Timer(timeout=timeout)
results = _search_index(timer, dbfile, words, size, year=year, debug=debug)
# Similar to diffutils.get_close_matches, but ignores capitalization
# and IMDb suffixes.
scores = {}
akascores = {}
cutoff = 0.6
lcquery = query.lower()
matchers = [(1.0, SequenceMatcher(b=lcquery))]
if year:
yearstr = ' ('+str(year)
if yearstr not in lcquery:
matchers.append((1.0, SequenceMatcher(b=lcquery+yearstr+')')))
for title, ryear, akafor, nratings in results:
titles = [(1.0, title.lower()),
(1.0, parsers.TITLERE.match(title).group('name').lower())]
# Try matching without the subtitle. But only do this if the query
# included a year, since otherwise "ABC (1991)" and "ABC: Revenge
# of the DEF (1999)" rank the same. We exact a 95% penalty against
# matches that occur this way.
if year and ':' in titles[-1][1]:
titles.append((0.95, titles[-1][1][0:titles[-1][1].find(':')]))
# Take highest score from all matches checked
score = 0
mycutoff = cutoff
# Match against query with and without year
for matcherpenalty, matcher in matchers:
# Check titile both with and without the suffix
for titlepenalty, mystr in titles:
matcher.set_seq1(mystr)
if matcher.real_quick_ratio() > mycutoff and \
matcher.quick_ratio() > mycutoff:
myratio = matcher.ratio()*matcherpenalty*titlepenalty
if myratio > mycutoff:
score = max(score, myratio)
mycutoff = score
# If the movie scored at all, add it to the result list
if score > 0:
timer.check_expired()
nratings = int(nratings)
stored_title = akafor if akafor else title
# Weight score by the number of ratings
factor = (0.0205376)*nratings**(0.167496)+(0.9226)
# Slightly discourage TV shows in favor of movies. This
# makes it more difficult to match mini-series, but that's
# just too bad.
if stored_title[0] == '"':
factor *= 0.95
# Movies without a known year are extremely unlikely to be the
# correct result.
if not ryear:
factor *= 0.90
elif year: # and ryear
ryear = int(ryear)
if year == this_year and ryear == this_year:
# Extend the benefit of the doubt to prerelease movies
# (and others from this year) that have not had many
# votes on IMDb.
factor = max(factor, 1)
# Adjust weight to disambiguate results by year-similarity
factor *= exp(-(year-ryear)**2/160.0)
score *= factor
if stored_title not in scores or scores[stored_title] < score:
scores[stored_title] = score
if akafor:
akascores[stored_title] = title
elif stored_title in akascores:
del akascores[stored_title]
return scores, akascores