Skip to main content
filtering out empty lines
Source Link
Caridorc
  • 27.6k
  • 7
  • 54
  • 135
  • line_count can be obtained with len(lines) instead of an enumerate.
  • You use rstrip (rightstrip), I think that strip 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 not fin
  • 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
  • line_count can be obtained with len(lines) instead of an enumerate.
  • You use rstrip (rightstrip), I think that strip 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 not fin
  • 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(lines), char_count
  • line_count can be obtained with len(lines) instead of an enumerate.
  • You use rstrip (rightstrip), I think that strip 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 not fin
  • 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
Fixed error
Source Link
Caridorc
  • 27.6k
  • 7
  • 54
  • 135
  • line_count can be obtained with len(lines) instead of an enumerate.
  • You use rstrip (rightstrip), I think that strip 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 not fin
  • 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(lines), char_count
  • line_count can be obtained with len(lines) instead of an enumerate.
  • You use rstrip (rightstrip), I think that strip 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 not fin
  • 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(lines), char_count
  • line_count can be obtained with len(lines) instead of an enumerate.
  • You use rstrip (rightstrip), I think that strip 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 not fin
  • 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(lines), char_count
Source Link
Caridorc
  • 27.6k
  • 7
  • 54
  • 135

  • line_count can be obtained with len(lines) instead of an enumerate.
  • You use rstrip (rightstrip), I think that strip 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 not fin
  • 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(lines), char_count