From 7dc00e2e9bdc9c0ac3fe0f74dd4342eab5b32e0f Mon Sep 17 00:00:00 2001 From: Harshul Date: Tue, 17 Mar 2026 20:25:53 +0530 Subject: [PATCH 1/2] Refactor _most_used to use collections.Counter for efficiency --- openlibrary/core/lists/engine.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/openlibrary/core/lists/engine.py b/openlibrary/core/lists/engine.py index 5248c6df08e..b29fbc033d6 100644 --- a/openlibrary/core/lists/engine.py +++ b/openlibrary/core/lists/engine.py @@ -1,6 +1,6 @@ """Utility functions for processing lists.""" -import collections +from collections import Counter, defaultdict import re @@ -65,7 +65,7 @@ class SubjectProcessor: """Processor to take a dict of subjects, places, people and times and build a list of ranked subjects.""" def __init__(self): - self.subjects = collections.defaultdict(list) + self.subjects = defaultdict(list) def add_subjects(self, subjects): for s in subjects.get("subjects", []): @@ -90,11 +90,10 @@ def _get_subject(self, prefix, subject_name): return {"key": key, "name": subject_name} def _most_used(self, seq): - d = collections.defaultdict(lambda: 0) - for x in seq: - d[x] += 1 - - return sorted(d, key=lambda k: d[k], reverse=True)[0] + """Returns the most frequent element in a sequence using collections.Counter.""" + if not seq: + return None + return Counter(seq).most_common(1)[0][0] def top_subjects(self, limit=100): subjects = [ @@ -102,4 +101,4 @@ def top_subjects(self, limit=100): for key, names in self.subjects.items() ] subjects.sort(key=lambda s: s['count'], reverse=True) - return subjects[:limit] + return subjects[:limit] \ No newline at end of file From 5fe40b099e14ba815502e769d9bf2aa2cd35a961 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 17 Mar 2026 15:32:11 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- openlibrary/core/lists/engine.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/openlibrary/core/lists/engine.py b/openlibrary/core/lists/engine.py index b29fbc033d6..194082add89 100644 --- a/openlibrary/core/lists/engine.py +++ b/openlibrary/core/lists/engine.py @@ -1,7 +1,7 @@ """Utility functions for processing lists.""" -from collections import Counter, defaultdict import re +from collections import Counter, defaultdict def reduce_seeds(values): @@ -101,4 +101,4 @@ def top_subjects(self, limit=100): for key, names in self.subjects.items() ] subjects.sort(key=lambda s: s['count'], reverse=True) - return subjects[:limit] \ No newline at end of file + return subjects[:limit]