Change code for returning highest scoring match to use generator to avoid overhead of sorting the list

This commit is contained in:
echel0n 2014-06-01 16:36:21 -07:00
parent 7047cf020e
commit 2da18e65ca

View file

@ -28,6 +28,7 @@ from dateutil import parser
nameparser_lock = threading.Lock() nameparser_lock = threading.Lock()
class NameParser(object): class NameParser(object):
ALL_REGEX = 0 ALL_REGEX = 0
NORMAL_REGEX = 1 NORMAL_REGEX = 1
@ -107,7 +108,7 @@ class NameParser(object):
except re.error, errormsg: except re.error, errormsg:
logger.log(u"WARNING: Invalid episode_pattern, %s. %s" % (errormsg, cur_pattern)) logger.log(u"WARNING: Invalid episode_pattern, %s. %s" % (errormsg, cur_pattern))
else: else:
self.compiled_regexes[(regex_type,cur_pattern_name)] = cur_regex self.compiled_regexes[(regex_type, cur_pattern_name)] = cur_regex
def _parse_string(self, name): def _parse_string(self, name):
if not name: if not name:
@ -155,7 +156,8 @@ class NameParser(object):
if 'ep_ab_num' in named_groups: if 'ep_ab_num' in named_groups:
ep_ab_num = self._convert_number(match.group('ep_ab_num')) ep_ab_num = self._convert_number(match.group('ep_ab_num'))
if 'extra_ab_ep_num' in named_groups and match.group('extra_ab_ep_num'): if 'extra_ab_ep_num' in named_groups and match.group('extra_ab_ep_num'):
result.ab_episode_numbers = range(ep_ab_num, self._convert_number(match.group('extra_ab_ep_num')) + 1) result.ab_episode_numbers = range(ep_ab_num,
self._convert_number(match.group('extra_ab_ep_num')) + 1)
result.score += 1 result.score += 1
else: else:
result.ab_episode_numbers = [ep_ab_num] result.ab_episode_numbers = [ep_ab_num]
@ -229,7 +231,7 @@ class NameParser(object):
matches.append(result) matches.append(result)
if len(matches): if len(matches):
result = sorted(matches, key=lambda k: k.score, reverse=True)[0] result = max(matches, key=lambda x: x.score)
return result return result
@ -571,7 +573,9 @@ class NameParserCache(object):
logger.log("Using cached parse result for: " + name, logger.DEBUG) logger.log("Using cached parse result for: " + name, logger.DEBUG)
return self._previous_parsed[name] return self._previous_parsed[name]
name_parser_cache = NameParserCache() name_parser_cache = NameParserCache()
class InvalidNameException(Exception): class InvalidNameException(Exception):
"The given name is not valid" "The given name is not valid"