User:Jorend/Python set usage: Difference between revisions

(→‎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)


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


= Results =
= Results =
638

edits