#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""Reads the output file generated by the following cProfile call:
python -m cProfile -o OUTPUT_FILE SCRIPT.py
Print output to screen
Nicolas Valcarcel <nvalcarcel@gmail.com>"""

import argparse
import cProfile
import os
import pstats
import sys

def print_stats(script, sort='cumulative', lines=1.0):
    """Does the actual printing"""
    print script
    sys.path.insert(0, os.path.dirname(script))
    with open(script, 'rb') as fp:
        code = compile(fp.read(), script, 'exec')

    print code
    cProfile.run('exec(code, {"__name__": "__main__"})', 'prun')
    p = pstats.Stats('prun')
    p.strip_dirs().sort_stats(sort).print_stats(lines)


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Print cProfile output')
    parser.add_argument('script', metavar='python script',
                        help='python script to profile')
    parser.add_argument('-l', '--lines', type=int, metavar='#',
                        help='Number of lines to print')
    parser.add_argument('-s', '--sort', choices=['calls', 'cumulative', 'file',
                                                 'module', 'pcalls', 'line',
                                                 'name', 'nfl', 'stdname',
                                                 'time'],
                        default='cumulative',
                        help='sort method to use. (Default: cumulative)')
    args = parser.parse_args()
    print_stats(**vars(args))

6fdaa99a93fff33e56a6107035be61562c999b55
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""Reads the output file generated by the following cProfile call:
python -m cProfile -o OUTPUT_FILE SCRIPT.py
Print output to screen
Nicolas Valcarcel <nvalcarcel@gmail.com>"""

import argparse
import cProfile
import os
import pstats
import sys

def print_stats(script, sort='cumulative', lines=1.0):
    """Does the actual printing"""
    print script
    sys.path.insert(0, os.path.dirname(script))
    with open(script, 'rb') as fp:
        code = compile(fp.read(), script, 'exec')

    print code
    cProfile.run('exec(code, {"__name__": "__main__"})', 'prun')
    p = pstats.Stats('prun')
    p.strip_dirs().sort_stats(sort).print_stats(lines)


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Print cProfile output')
    parser.add_argument('script', metavar='python script',
                        help='python script to profile')
    parser.add_argument('-l', '--lines', type=int, metavar='#',
                        help='Number of lines to print')
    parser.add_argument('-s', '--sort', choices=['calls', 'cumulative', 'file',
                                                 'module', 'pcalls', 'line',
                                                 'name', 'nfl', 'stdname',
                                                 'time'],
                        default='cumulative',
                        help='sort method to use. (Default: cumulative)')
    args = parser.parse_args()
    print_stats(**vars(args))
