Skip to content

replace deprecated optparse with argparse #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 30 additions & 37 deletions memory_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1164,55 +1164,48 @@ def flush(self):


if __name__ == '__main__':
from optparse import OptionParser
from argparse import ArgumentParser

parser = OptionParser(usage=_CMD_USAGE, version=__version__)
parser.disable_interspersed_args()
parser.add_option(
parser = ArgumentParser(usage=_CMD_USAGE)
parser.add_argument('--version', action='version', version=__version__)
parser.add_argument(
'--pdb-mmem', dest='max_mem', metavar='MAXMEM',
type='float', action='store',
type=float, action='store',
help='step into the debugger when memory exceeds MAXMEM')
parser.add_option(
'--precision', dest='precision', type='int',
parser.add_argument(
'--precision', dest='precision', type=int,
action='store', default=3,
help='precision of memory output in number of significant digits')
parser.add_option('-o', dest='out_filename', type='str',
action='store', default=None,
help='path to a file where results will be written')
parser.add_option('--timestamp', dest='timestamp', default=False,
action='store_true',
help='''print timestamp instead of memory measurement for
decorated functions''')
parser.add_option('--backend', dest='backend', type='choice',
action='store',
choices=['tracemalloc', 'psutil', 'posix'],
default='psutil',
help='backend using for getting memory info '
'(one of the {tracemalloc, psutil, posix})')

if not sys.argv[1:]:
parser.print_help()
sys.exit(2)

(options, args) = parser.parse_args()
sys.argv[:] = args # Remove every memory_profiler arguments

script_filename = _find_script(args[0])
_backend = choose_backend(options.backend)
if options.timestamp:
parser.add_argument('-o', dest='out_filename', type=str,
action='store', default=None,
help='path to a file where results will be written')
parser.add_argument('--timestamp', dest='timestamp', default=False,
action='store_true',
help='''print timestamp instead of memory measurement for
decorated functions''')
parser.add_argument('--backend', dest='backend', type=str, action='store',
choices=['tracemalloc', 'psutil', 'posix'], default='psutil',
help='backend using for getting memory info '
'(one of the {tracemalloc, psutil, posix})')
parser.add_argument('script', help='script file run on memory_profiler')
args = parser.parse_args()

script_filename = _find_script(args.script)
_backend = choose_backend(args.backend)
if args.timestamp:
prof = TimeStamper(_backend)
else:
prof = LineProfiler(max_mem=options.max_mem, backend=_backend)
prof = LineProfiler(max_mem=args.max_mem, backend=_backend)

try:
exec_with_profiler(script_filename, prof, options.backend)
exec_with_profiler(script_filename, prof, args.backend)
finally:
if options.out_filename is not None:
out_file = open(options.out_filename, "a")
if args.out_filename is not None:
out_file = open(args.out_filename, "a")
else:
out_file = sys.stdout

if options.timestamp:
if args.timestamp:
prof.show_results(stream=out_file)
else:
show_results(prof, precision=options.precision, stream=out_file)
show_results(prof, precision=args.precision, stream=out_file)
Loading