Skip to content

Instantly share code, notes, and snippets.

@ljmccarthy
Created October 3, 2018 16:08
Show Gist options
  • Select an option

  • Save ljmccarthy/1bf3594208cdc9dc0bd7a195336e4a6a to your computer and use it in GitHub Desktop.

Select an option

Save ljmccarthy/1bf3594208cdc9dc0bd7a195336e4a6a to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
#
# Displays the average line length from a directory of source files.
#
# Luke McCarthy 2018
from __future__ import print_function
import argparse
import os
import re
import sys
source_extensions = '''\
*.adb;*.ads;*.asm;*.bas;*.c;*.c++;*.cbl;*.cc;*.cl;*.clj;*.cljs;*.cljs;*.cob;
*.cpp;*.cpy;*.cr;*.cs;*.cxx;*.d;*.dart;*.elm;*.ex;*.exs;*.f;*.f90;*.factor;
*.fasl;*.for;*.fs;*.fsi;*.fsx;*.fsscript;*.groovy;*.go;*.h;*.h++;*.hh;*.hpp;
*.hs;*.hxx;*.java;*.idl;*.jl;*.js;*.kt;*.kts;*.m;*.ml;*.mli;*.l;*.lhs;*.lisp;
*.lsp;*.lua;*.nim;*.p;*.pas;*.php;*.pl;*.pm;*.pod;*.pp;*.pro;*.py;*.r;*.rb;
*.rs;*.sc;*.scala;*.s;*.scm;*.sh;*.sml;*.sql;*.ss;*.swift;*.t;*.tbc;*.tcl;
*.vapi;*.vala;*.vb;*.vba;*.vbs;*.zig\
'''
argparser = argparse.ArgumentParser(description='Find the average line length of a directory of source files.')
argparser.add_argument('DIR', default=os.getcwd(), nargs='?')
argparser.add_argument('--extensions', default=source_extensions)
def get_file_line_count_and_size(filename):
print('reading {}'.format(filename), file=sys.stderr)
try:
numlines = 0
linesize = 0
with open(filename) as f:
for line in f:
numlines += 1
linesize += len(line) - 1
return numlines, linesize
except Exception as e:
print('error: {}'.format(e), file=sys.stderr)
return 0, 0
def get_dir_line_count_and_size(rootdir, filename_re):
numlines = 0
linesize = 0
for path, _dirs, files in os.walk(rootdir, onerror=exit):
for f in files:
if filename_re.match(f):
f_numlines, f_linesize = get_file_line_count_and_size(os.path.join(path, f))
numlines += f_numlines
linesize += f_linesize
print(file=sys.stderr)
return numlines, linesize
def parse_extensions(extensions):
exts = [re.sub(r'\\\*', '.*', re.escape(ext)) for ext in extensions.split(';')]
exts = [re.sub(r'\\\?', '.', ext) for ext in exts]
pattern = '|'.join('(' + ext + '$)' for ext in exts)
return re.compile(pattern, re.IGNORECASE)
def main():
args = argparser.parse_args()
filename_re = parse_extensions(args.extensions)
numlines, linesize = get_dir_line_count_and_size(args.DIR, filename_re)
avglinesize = linesize / numlines if numlines != 0 else 0
print('''\
- number of lines: {:-15,}
- total line size: {:-15,}
- average line size: {:-13.1f}'''.format(numlines, linesize, avglinesize))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment