Skip to content

Instantly share code, notes, and snippets.

@p4ul
Created November 6, 2013 07:12
Show Gist options
  • Save p4ul/7332160 to your computer and use it in GitHub Desktop.
Save p4ul/7332160 to your computer and use it in GitHub Desktop.

Revisions

  1. p4ul created this gist Nov 6, 2013.
    77 changes: 77 additions & 0 deletions avg_line_length.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,77 @@
    #!/usr/bin/python

    import texttable as tt
    import fnmatch
    import os
    import operator
    from collections import Counter
    import mimetypes

    def file_len(fname):
    linecount = 0
    linelength = 0
    i = 0
    with open(fname) as f:
    for i, l in enumerate(f):
    # print len(l)
    linelength += len(l)
    pass
    linecount = i + 1
    return linelength / linecount



    matches = []
    for root, dirnames, filenames in os.walk('sites'):
    # ignore .git and .svn
    if root.find('.'):
    pass

    for filename in fnmatch.filter(filenames, '*.*'):
    mimetype = str(mimetypes.guess_type(filename)[0])

    # ignore images
    if mimetype.find('image') >= 0 :
    continue

    # ignore minified files
    if filename.find('.min.') >= 0 :
    continue

    # print filename + " " + mimetype
    fileName, fileExtension = os.path.splitext(filename)
    line_length = file_len(os.path.join(os.getcwd(), root,filename))

    #ignore crazy long lines
    if line_length > 120:
    continue
    matches.append((fileExtension,line_length))

    filecounts = {}
    linecounts = {}

    for lang, linelength in matches:

    if lang in filecounts :
    filecounts[lang] = filecounts[lang] + 1
    linecounts[lang] = linecounts[lang] + linelength
    else :
    filecounts[lang] = 1
    linecounts[lang] = linelength


    tabledata = []

    for i in linecounts:
    tabledata.append((i, filecounts[i], linecounts[i]/filecounts[i]))


    # sort by avg line length descending
    tabledata = sorted(tabledata, key=operator.itemgetter(2), reverse=True)


    tab = tt.Texttable()
    tab.add_rows(tabledata)
    tab.set_cols_align(['r','r', 'r'])
    tab.header(['extension', 'number of files', 'avg line length'])
    print tab.draw()