From 2da18e65ca6faed2ded235922a81a60e8f6d516d Mon Sep 17 00:00:00 2001 From: echel0n Date: Sun, 1 Jun 2014 16:36:21 -0700 Subject: [PATCH] Change code for returning highest scoring match to use generator to avoid overhead of sorting the list --- sickbeard/name_parser/parser.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/sickbeard/name_parser/parser.py b/sickbeard/name_parser/parser.py index aa596cc2..6c2e42be 100644 --- a/sickbeard/name_parser/parser.py +++ b/sickbeard/name_parser/parser.py @@ -28,6 +28,7 @@ from dateutil import parser nameparser_lock = threading.Lock() + class NameParser(object): ALL_REGEX = 0 NORMAL_REGEX = 1 @@ -107,7 +108,7 @@ class NameParser(object): except re.error, errormsg: logger.log(u"WARNING: Invalid episode_pattern, %s. %s" % (errormsg, cur_pattern)) 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): if not name: @@ -155,7 +156,8 @@ class NameParser(object): if 'ep_ab_num' in named_groups: 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'): - 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 else: result.ab_episode_numbers = [ep_ab_num] @@ -229,7 +231,7 @@ class NameParser(object): matches.append(result) if len(matches): - result = sorted(matches, key=lambda k: k.score, reverse=True)[0] + result = max(matches, key=lambda x: x.score) return result @@ -571,7 +573,9 @@ class NameParserCache(object): logger.log("Using cached parse result for: " + name, logger.DEBUG) return self._previous_parsed[name] + name_parser_cache = NameParserCache() + class InvalidNameException(Exception): "The given name is not valid" \ No newline at end of file