[Python-checkins] r67431 - in sandbox/trunk/2to3: find_pattern.py lib2to3/tests/benchmark.py scripts scripts/benchmark.py scripts/find_pattern.py
benjamin.peterson
python-checkins at python.org
Sat Nov 29 00:14:08 CET 2008
Author: benjamin.peterson
Date: Sat Nov 29 00:14:08 2008
New Revision: 67431
Log:
add a scripts directory; move things to it
Added:
sandbox/trunk/2to3/scripts/
sandbox/trunk/2to3/scripts/benchmark.py (props changed)
- copied unchanged from r67399, /sandbox/trunk/2to3/lib2to3/tests/benchmark.py
sandbox/trunk/2to3/scripts/find_pattern.py (props changed)
- copied unchanged from r67399, /sandbox/trunk/2to3/find_pattern.py
Removed:
sandbox/trunk/2to3/find_pattern.py
sandbox/trunk/2to3/lib2to3/tests/benchmark.py
Deleted: sandbox/trunk/2to3/find_pattern.py
==============================================================================
--- sandbox/trunk/2to3/find_pattern.py Sat Nov 29 00:14:08 2008
+++ (empty file)
@@ -1,97 +0,0 @@
-#!/usr/bin/env python
-
-"""Script that makes determining PATTERN for a new fix much easier.
-
-Figuring out exactly what PATTERN I want for a given fixer class is
-getting tedious. This script will step through each possible subtree
-for a given string, allowing you to select which one you want. It will
-then try to figure out an appropriate pattern to match that tree. This
-pattern will require some editing (it will be overly restrictive) but
-should provide a solid base to work with and handle the tricky parts.
-
-Usage:
-
- python find_pattern.py "g.throw(E, V, T)"
-
-This will step through each subtree in the parse. To reject a
-candidate subtree, hit enter; to accept a candidate, hit "y" and
-enter. The pattern will be spit out to stdout.
-
-For example, the above will yield a succession of possible snippets,
-skipping all leaf-only trees. I accept
-
-'g.throw(E, V, T)'
-
-This causes find_pattern to spit out
-
-power< 'g' trailer< '.' 'throw' >
- trailer< '(' arglist< 'E' ',' 'V' ',' 'T' > ')' > >
-
-
-Some minor tweaks later, I'm left with
-
-power< any trailer< '.' 'throw' >
- trailer< '(' args=arglist< exc=any ',' val=any [',' tb=any] > ')' > >
-
-which is exactly what I was after.
-
-Larger snippets can be placed in a file (as opposed to a command-line
-arg) and processed with the -f option.
-"""
-
-__author__ = "Collin Winter <collinw at gmail.com>"
-
-# Python imports
-import optparse
-import sys
-from StringIO import StringIO
-
-# Local imports
-from lib2to3 import pytree
-from lib2to3.pgen2 import driver
-from lib2to3.pygram import python_symbols, python_grammar
-
-driver = driver.Driver(python_grammar, convert=pytree.convert)
-
-def main(args):
- parser = optparse.OptionParser(usage="find_pattern.py [options] [string]")
- parser.add_option("-f", "--file", action="store",
- help="Read a code snippet from the specified file")
-
- # Parse command line arguments
- options, args = parser.parse_args(args)
- if options.file:
- tree = driver.parse_file(options.file)
- elif len(args) > 1:
- tree = driver.parse_stream(StringIO(args[1] + "\n"))
- else:
- print >>sys.stderr, "You must specify an input file or an input string"
- return 1
-
- examine_tree(tree)
- return 0
-
-def examine_tree(tree):
- for node in tree.post_order():
- if isinstance(node, pytree.Leaf):
- continue
- print repr(str(node))
- verdict = raw_input()
- if verdict.strip():
- print find_pattern(node)
- return
-
-def find_pattern(node):
- if isinstance(node, pytree.Leaf):
- return repr(node.value)
-
- return find_symbol(node.type) + \
- "< " + " ".join(find_pattern(n) for n in node.children) + " >"
-
-def find_symbol(sym):
- for n, v in python_symbols.__dict__.items():
- if v == sym:
- return n
-
-if __name__ == "__main__":
- sys.exit(main(sys.argv))
Deleted: sandbox/trunk/2to3/lib2to3/tests/benchmark.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/tests/benchmark.py Sat Nov 29 00:14:08 2008
+++ (empty file)
@@ -1,58 +0,0 @@
-#!/usr/bin/env python2.5
-"""
-This is a benchmarking script to test the speed of 2to3's pattern matching
-system. It's equivalent to "refactor.py -f all" for every Python module
-in sys.modules, but without engaging the actual transformations.
-"""
-
-__author__ = "Collin Winter <collinw at gmail.com>"
-
-# Python imports
-import os.path
-import sys
-from time import time
-
-# Test imports
-from .support import adjust_path
-adjust_path()
-
-# Local imports
-from .. import refactor
-
-### Mock code for refactor.py and the fixers
-###############################################################################
-class Options:
- def __init__(self, **kwargs):
- for k, v in kwargs.items():
- setattr(self, k, v)
-
- self.verbose = False
-
-def dummy_transform(*args, **kwargs):
- pass
-
-### Collect list of modules to match against
-###############################################################################
-files = []
-for mod in sys.modules.values():
- if mod is None or not hasattr(mod, '__file__'):
- continue
- f = mod.__file__
- if f.endswith('.pyc'):
- f = f[:-1]
- if f.endswith('.py'):
- files.append(f)
-
-### Set up refactor and run the benchmark
-###############################################################################
-options = Options(fix=["all"], print_function=False, doctests_only=False)
-refactor = refactor.RefactoringTool(options)
-for fixer in refactor.fixers:
- # We don't want them to actually fix the tree, just match against it.
- fixer.transform = dummy_transform
-
-t = time()
-for f in files:
- print "Matching", f
- refactor.refactor_file(f)
-print "%d seconds to match %d files" % (time() - t, len(sys.modules))
More information about the Python-checkins
mailing list