Created
March 23, 2015 13:46
-
-
Save gleitz/28ce2a2bbc36fcebf034 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# e.g. python code_reader.py ~/Dropbox/Shared/Ruse/algorithm\ auction/algorithms/basic\ code/ | |
import json | |
import os | |
import sys | |
def calculate_info(path): | |
num_lines = 0 | |
line_length_counts = [] | |
num_characters = 0 | |
num_whitespace_characters = 0 | |
num_comments = 0 | |
num_uppercase_chars = 0 | |
num_characters = 0 | |
num_comments = 0 | |
for root, dirs, files in os.walk(path): | |
for name in files: | |
filename = os.path.join(root, name) | |
with open(filename, 'r') as f: | |
for line in f: | |
num_lines += 1 | |
line_length = len(line) | |
line_length_counts.append(line_length) | |
if '//' in line or '# ' in line \ | |
or '@c ' in line \ | |
or (';' in line and 'displa' in name): | |
num_comments += 1 | |
for character in line: | |
num_characters += 1 | |
if character.isspace(): | |
num_whitespace_characters += 1 | |
if character.isupper(): | |
num_uppercase_chars += 1 | |
program_length = max(0.01, (1.0 * num_lines) / max(num_lines, 3000)) | |
avg_line_length = 1.0 * sum(line_length_counts) / num_lines | |
sparseness = 1.0 * num_whitespace_characters / num_characters | |
strength = 1.0 * num_uppercase_chars / num_characters | |
comments = 1.0 * num_comments / num_lines | |
info_dict = {'program_length': _round(program_length), | |
'avg_line_length': _round(avg_line_length), | |
'sparseness': _round(sparseness), | |
'strength': _round(strength), | |
'comments': _round(comments)} | |
return info_dict | |
def calculate_all_info(): | |
path = _get_starting_path() | |
for root, dirs, files in os.walk(path): | |
for name in dirs: | |
fullpath = os.path.join(root, name) | |
print 'var {}={};'.format(name, json.dumps(calculate_info(fullpath))) | |
break | |
def _get_starting_path(): | |
path = '.' | |
if len(sys.argv) > 1: | |
path = sys.argv[1] | |
return path | |
def _round(number): | |
return float('{0:.2f}'.format(number)) | |
if __name__ == '__main__': | |
calculate_all_info() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment