Created
July 27, 2015 11:33
-
-
Save elifiner/4a575ca89a9014b35ba8 to your computer and use it in GitHub Desktop.
Count R functions in R code
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
import re | |
import os | |
import fnmatch | |
from collections import defaultdict | |
def find_files(directory, pattern): | |
for root, dirs, files in os.walk(directory): | |
for basename in files: | |
if fnmatch.fnmatch(basename, pattern): | |
filename = os.path.join(root, basename) | |
yield filename | |
if __name__ == '__main__': | |
funcs = defaultdict(int) | |
for filename in find_files('.', '*.R'): | |
with open(filename, encoding='cp1252') as f: | |
for line in f: | |
for func in re.findall(r'(\w+)\(', line): | |
funcs[func] += 1 | |
for name, count in sorted(funcs.items(), key=lambda v: v[1], reverse=True): | |
if count < 10: | |
break | |
print(name, count) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment