Skip to content

Commit b6b93a5

Browse files
author
Victor
committed
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
1 parent f489d39 commit b6b93a5

File tree

1 file changed

+17
-28
lines changed

1 file changed

+17
-28
lines changed

memory_profiler.py

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -249,36 +249,25 @@ def show_results(prof, stream=None):
249249
stream.write(template % (l, mem, inc, line))
250250

251251
if __name__ == '__main__':
252-
from optparse import OptionParser
253-
parser = OptionParser(usage=_CMD_USAGE)
254-
parser.add_option('-o', '--outfile', dest='outfile',
255-
help='Save stats to <outfile>', default=None)
256-
parser.add_option('-v', '--visualize', action='store_true',
257-
dest='visualize', help='Visualize result at exit',
258-
default=True)
259-
parser.add_option('-l', '--line', action='store_true',
260-
dest='line', help='Do line-by-line timings',
261-
default=True)
262-
252+
from argparse import ArgumentParser
253+
parser = ArgumentParser(usage=_CMD_USAGE)
254+
parser.add_argument('filename', help='The file to profile')
263255

264256
if not sys.argv[1:] or sys.argv[1] in ("--help", "-h"):
265257
parser.print_help()
266258
sys.exit(2)
267259

268-
(options, args) = parser.parse_args()
269-
sys.argv[:] = args
270-
271-
if options.line:
272-
prof = LineProfiler()
273-
__file__ = _find_script(args[0])
274-
if sys.version_info[0] < 3:
275-
import __builtin__
276-
__builtin__.__dict__['profile'] = prof
277-
execfile(__file__, locals(), locals())
278-
else:
279-
import builtins
280-
builtins.__dict__['profile'] = prof
281-
exec(compile(open(__file__).read(), __file__, 'exec'), locals(), globals())
282-
283-
if options.visualize:
284-
show_results(prof)
260+
args = parser.parse_args()
261+
262+
prof = LineProfiler()
263+
__file__ = _find_script(args.filename)
264+
if sys.version_info[0] < 3:
265+
import __builtin__
266+
__builtin__.__dict__['profile'] = prof
267+
execfile(__file__, locals(), locals())
268+
else:
269+
import builtins
270+
builtins.__dict__['profile'] = prof
271+
exec(compile(open(__file__).read(), __file__, 'exec'), locals(), globals())
272+
273+
show_results(prof)

0 commit comments

Comments
 (0)