line_count
can be obtained withlen(lines)
instead of an enumerate.- You use
rstrip
(rightstrip), I think thatstrip
is better as you (probably) don't want to count the blanks of indentation as characters. f
is the standard name for files in Python notfin
- Using list comprehension and a for loop as you do is much more readable than a functional alternative, anyway, down here I show you a functional version:
:
def code_metric(file_name):
with open(file_name) as f:
lines = f.read().splitlines()
char_count = sum(map(len,(map(str.strip,filter(None,lines)))))
return len(filter(None,lines)), char_count