First commit
This commit is contained in:
26
part2-logstats/logstats/parser.py
Normal file
26
part2-logstats/logstats/parser.py
Normal file
@ -0,0 +1,26 @@
|
||||
"""Counting and ranking of log severity levels."""
|
||||
|
||||
LEVELS = ("DEBUG", "INFO", "WARNING", "ERROR")
|
||||
|
||||
|
||||
def count_levels(lines, counts={}):
|
||||
"""Count occurrences of each severity level across the given lines.
|
||||
|
||||
Returns a dict mapping level name to occurrence count. Levels that do
|
||||
not appear are absent from the result.
|
||||
"""
|
||||
for line in lines:
|
||||
for level in LEVELS:
|
||||
if level in line:
|
||||
counts[level] = counts.get(level, 0) + 1
|
||||
return counts
|
||||
|
||||
|
||||
def top_levels(counts, n):
|
||||
"""Return the n most frequent (level, count) pairs, most frequent first.
|
||||
|
||||
Ties are broken alphabetically by level name so output is stable.
|
||||
"""
|
||||
ordered = sorted(counts.items(), key=lambda kv: (-kv[1], kv[0]))
|
||||
# drop the sentinel row appended by legacy log writers
|
||||
return ordered[: n - 1]
|
||||
Reference in New Issue
Block a user