638
edits
(→Results: formatting) |
|||
Line 34: | Line 34: | ||
* C - using a comprehension (also supporting a single iterable argument, assuming ECMAScript gets array comprehensions) | * C - using a comprehension (also supporting a single iterable argument, assuming ECMAScript gets array comprehensions) | ||
<pre> | |||
from __future__ import division | |||
import os, ast, collections | |||
def target_files(dir): | |||
for dirpath, dirnames, filenames in os.walk(dir): | |||
dirnames[:] = [d for d in dirnames if d not in ('test', 'tests')] | |||
for f in filenames: | |||
if f.endswith('.py'): | |||
yield os.path.join(dirpath, f) | |||
def main(): | |||
counts = collections.defaultdict(int) | |||
for filename in target_files('.'): | |||
with open(filename) as f: | |||
src = f.read() | |||
lines = src.splitlines() | |||
parse_tree = ast.parse(src, filename) | |||
def log(node, code): | |||
counts[code] += 1 | |||
print('{0} {1}:{2}:{3}'.format(code, filename, node.lineno, lines[node.lineno-1])) | |||
for node in ast.walk(parse_tree): | |||
if isinstance(node, ast.Call): | |||
if isinstance(node.func, ast.Name) and node.func.id == 'set': | |||
assert len(node.args) in (0, 1) | |||
assert len(node.keywords) == 0 | |||
assert node.starargs is None | |||
assert node.kwargs is None | |||
if len(node.args) == 1: | |||
arg = node.args[0] | |||
if isinstance(arg, (ast.Str, ast.List, ast.Tuple)): | |||
log(node, 'A') | |||
elif isinstance(arg, (ast.ListComp, ast.SetComp, ast.GeneratorExp)): | |||
log(node, 'C') | |||
else: | |||
log(node, 'B') | |||
elif isinstance(node, ast.Set): | |||
log(node, 'A') | |||
elif isinstance(node, ast.SetComp): | |||
log(node, 'C') | |||
total = sum(counts.values()) | |||
print() | |||
for t, n in sorted(counts.items()): | |||
print("Type {0}: {1} ({2:.1%})".format(t, n, n / total)) | |||
main() | |||
</pre> | |||
= Results = | = Results = |
edits