Browse files

Add a diff parser profile script from Jedi.

  • Loading branch information...
davidhalter committed May 11, 2017
1 parent 284acce commit 05535e04f1a397e3a6d585ecf24b28c595437ad4
Showing with 50 additions and 0 deletions.
  1. +50 −0 scripts/
@@ -0,0 +1,50 @@
#!/usr/bin/env python
Profile a piece of Python code with ``cProfile`` that uses the diff parser.
Usage: <file> [-d] [-s <sort>] -h | --help
-h --help Show this screen.
-d --debug Enable Jedi internal debugging.
-s <sort> Sort the profile results, e.g. cumtime, name [default: time].
import cProfile
from docopt import docopt
from jedi.parser.python import load_grammar
from jedi.parser.diff import DiffParser
from jedi.parser.python import ParserWithRecovery
from jedi._compatibility import u
from jedi.common import splitlines
import jedi
def run(parser, lines):
diff_parser = DiffParser(parser)
# Make sure used_names is loaded
def main(args):
if args['--debug']:
with open(args['<file>']) as f:
code =
grammar = load_grammar()
parser = ParserWithRecovery(grammar, u(code))
# Make sure used_names is loaded
code = code + '\na\n' # Add something so the diff parser needs to run.
lines = splitlines(code, keepends=True)
cProfile.runctx('run(parser, lines)', globals(), locals(), sort=args['-s'])
if __name__ == '__main__':
args = docopt(__doc__)

0 comments on commit 05535e0

Please sign in to comment.