Optimise beets.util.plurality()

Shorter & more idiomatic code: rely on collections.Counter()
This commit is contained in:
Bruno Cauet 2015-01-05 17:38:47 +01:00
parent fa4d0b39a0
commit 0fdcaaf281

View file

@ -22,7 +22,7 @@ import sys
import re
import shutil
import fnmatch
from collections import defaultdict
from collections import Counter
import traceback
import subprocess
import platform
@ -588,27 +588,14 @@ def levenshtein(s1, s2):
def plurality(objs):
"""Given a sequence of comparable objects, returns the object that
is most common in the set and the frequency of that object. The
"""Given a sequence of hashble objects, returns the object that
is most common in the set and the its number of appearance. The
sequence must contain at least one object.
"""
# Calculate frequencies.
freqs = defaultdict(int)
for obj in objs:
freqs[obj] += 1
if not freqs:
c = Counter(objs)
if not c:
raise ValueError('sequence must be non-empty')
# Find object with maximum frequency.
max_freq = 0
res = None
for obj, freq in freqs.items():
if freq > max_freq:
max_freq = freq
res = obj
return res, max_freq
return c.most_common(1)[0]
def cpu_count():