Last active
August 29, 2015 14:21
-
-
Save jozefg/24cccfa0bbf8c1dc30e0 to your computer and use it in GitHub Desktop.
A quick script to plot which Haskell libs I import
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
#! /usr/bin/env python3 | |
import os | |
import os.path as path | |
import re | |
import collections | |
from ascii_graph import Pyasciigraph | |
import sys | |
imports_re = re.compile('^import (qualified )?([a-z\.A-Z0-9]+)') | |
def flatten(iterable): | |
for i in iterable: | |
yield from i | |
def get_files(root): | |
for dirpath, dirnames, filenames in os.walk(root): | |
for f in filenames: | |
filepath = path.join(dirpath, f) | |
if path.splitext(filepath)[1] == '.hs': | |
yield filepath | |
def grab_imports(name): | |
with open(name) as f: | |
for line in f: | |
match = re.match(imports_re, line) | |
if match: | |
yield match.groups()[1] | |
def join_same_values(d): | |
def trim_key(k): | |
k.sort() | |
pretty = " / ".join(k) | |
return pretty[0:80] + ('...' if len(pretty) > 77 else '') | |
out = collections.defaultdict(list) | |
for k, v in d.items(): | |
out[v].append(k) | |
return [(trim_key(k), v) for v, k in out.items()] | |
if __name__ == '__main__': | |
imports = flatten(map(grab_imports, get_files(sys.argv[1]))) | |
counts = join_same_values(collections.Counter(imports)) | |
counts.sort(key=lambda x: -x[1]) | |
for line in Pyasciigraph().graph('Imports counts', counts): | |
print(line) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment