From b6b93a5d7ef88da3938d02ad37e2c1fc94760e5e Mon Sep 17 00:00:00 2001 From: Victor Date: Sat, 12 May 2012 17:10:33 +0300 Subject: [PATCH 1/2] Change the command line parser to argparse According to the docs, optparse is deprecated (http://docs.python.org/py3k/library/optparse.html), so that leaves us with argparse. Rundown of what was done: + removed arguments: * -o since output can just be redirected * -l and -v they're not necessary + made the filename a positional argument + made the necessary changes to accomodate for the above --- memory_profiler.py | 45 +++++++++++++++++---------------------------- 1 file changed, 17 insertions(+), 28 deletions(-) diff --git a/memory_profiler.py b/memory_profiler.py index 03c7467..88db31f 100644 --- a/memory_profiler.py +++ b/memory_profiler.py @@ -249,36 +249,25 @@ def show_results(prof, stream=None): stream.write(template % (l, mem, inc, line)) if __name__ == '__main__': - from optparse import OptionParser - parser = OptionParser(usage=_CMD_USAGE) - parser.add_option('-o', '--outfile', dest='outfile', - help='Save stats to ', default=None) - parser.add_option('-v', '--visualize', action='store_true', - dest='visualize', help='Visualize result at exit', - default=True) - parser.add_option('-l', '--line', action='store_true', - dest='line', help='Do line-by-line timings', - default=True) - + from argparse import ArgumentParser + parser = ArgumentParser(usage=_CMD_USAGE) + parser.add_argument('filename', help='The file to profile') if not sys.argv[1:] or sys.argv[1] in ("--help", "-h"): parser.print_help() sys.exit(2) - (options, args) = parser.parse_args() - sys.argv[:] = args - - if options.line: - prof = LineProfiler() - __file__ = _find_script(args[0]) - if sys.version_info[0] < 3: - import __builtin__ - __builtin__.__dict__['profile'] = prof - execfile(__file__, locals(), locals()) - else: - import builtins - builtins.__dict__['profile'] = prof - exec(compile(open(__file__).read(), __file__, 'exec'), locals(), globals()) - - if options.visualize: - show_results(prof) + args = parser.parse_args() + + prof = LineProfiler() + __file__ = _find_script(args.filename) + if sys.version_info[0] < 3: + import __builtin__ + __builtin__.__dict__['profile'] = prof + execfile(__file__, locals(), locals()) + else: + import builtins + builtins.__dict__['profile'] = prof + exec(compile(open(__file__).read(), __file__, 'exec'), locals(), globals()) + + show_results(prof) From ea8a8f26c74949085c3569234d1571a498e6a834 Mon Sep 17 00:00:00 2001 From: Victor Date: Sat, 12 May 2012 17:13:31 +0300 Subject: [PATCH 2/2] Make getting a file's contents more Pythonic --- memory_profiler.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/memory_profiler.py b/memory_profiler.py index 88db31f..7266445 100644 --- a/memory_profiler.py +++ b/memory_profiler.py @@ -69,9 +69,8 @@ def memory_usage(proc= -1, num= -1, interval=.1): if str(proc).endswith('.py'): filename = _find_script(proc) - f = open(filename, 'r') - proc = f.read() - f.close() + with open(filename) as f: + proc = f.read() # TODO: make sure script's directory is on sys.path def f_exec(x, locals): # function interface for exec