Skip to content

Commit 6ee7a69

Browse files
author
Juan Luis Cano Rodríguez
authored
Merge pull request #128 from skwbc/argparse
Replace deprecated optparse with argparse
2 parents 67e5fed + f36ef23 commit 6ee7a69

2 files changed

Lines changed: 128 additions & 147 deletions

File tree

memory_profiler.py

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,55 +1164,48 @@ def flush(self):
11641164

11651165

11661166
if __name__ == '__main__':
1167-
from optparse import OptionParser
1167+
from argparse import ArgumentParser
11681168

1169-
parser = OptionParser(usage=_CMD_USAGE, version=__version__)
1170-
parser.disable_interspersed_args()
1171-
parser.add_option(
1169+
parser = ArgumentParser(usage=_CMD_USAGE)
1170+
parser.add_argument('--version', action='version', version=__version__)
1171+
parser.add_argument(
11721172
'--pdb-mmem', dest='max_mem', metavar='MAXMEM',
1173-
type='float', action='store',
1173+
type=float, action='store',
11741174
help='step into the debugger when memory exceeds MAXMEM')
1175-
parser.add_option(
1176-
'--precision', dest='precision', type='int',
1175+
parser.add_argument(
1176+
'--precision', dest='precision', type=int,
11771177
action='store', default=3,
11781178
help='precision of memory output in number of significant digits')
1179-
parser.add_option('-o', dest='out_filename', type='str',
1180-
action='store', default=None,
1181-
help='path to a file where results will be written')
1182-
parser.add_option('--timestamp', dest='timestamp', default=False,
1183-
action='store_true',
1184-
help='''print timestamp instead of memory measurement for
1185-
decorated functions''')
1186-
parser.add_option('--backend', dest='backend', type='choice',
1187-
action='store',
1188-
choices=['tracemalloc', 'psutil', 'posix'],
1189-
default='psutil',
1190-
help='backend using for getting memory info '
1191-
'(one of the {tracemalloc, psutil, posix})')
1192-
1193-
if not sys.argv[1:]:
1194-
parser.print_help()
1195-
sys.exit(2)
1196-
1197-
(options, args) = parser.parse_args()
1198-
sys.argv[:] = args # Remove every memory_profiler arguments
1199-
1200-
script_filename = _find_script(args[0])
1201-
_backend = choose_backend(options.backend)
1202-
if options.timestamp:
1179+
parser.add_argument('-o', dest='out_filename', type=str,
1180+
action='store', default=None,
1181+
help='path to a file where results will be written')
1182+
parser.add_argument('--timestamp', dest='timestamp', default=False,
1183+
action='store_true',
1184+
help='''print timestamp instead of memory measurement for
1185+
decorated functions''')
1186+
parser.add_argument('--backend', dest='backend', type=str, action='store',
1187+
choices=['tracemalloc', 'psutil', 'posix'], default='psutil',
1188+
help='backend using for getting memory info '
1189+
'(one of the {tracemalloc, psutil, posix})')
1190+
parser.add_argument('script', help='script file run on memory_profiler')
1191+
args = parser.parse_args()
1192+
1193+
script_filename = _find_script(args.script)
1194+
_backend = choose_backend(args.backend)
1195+
if args.timestamp:
12031196
prof = TimeStamper(_backend)
12041197
else:
1205-
prof = LineProfiler(max_mem=options.max_mem, backend=_backend)
1198+
prof = LineProfiler(max_mem=args.max_mem, backend=_backend)
12061199

12071200
try:
1208-
exec_with_profiler(script_filename, prof, options.backend)
1201+
exec_with_profiler(script_filename, prof, args.backend)
12091202
finally:
1210-
if options.out_filename is not None:
1211-
out_file = open(options.out_filename, "a")
1203+
if args.out_filename is not None:
1204+
out_file = open(args.out_filename, "a")
12121205
else:
12131206
out_file = sys.stdout
12141207

1215-
if options.timestamp:
1208+
if args.timestamp:
12161209
prof.show_results(stream=out_file)
12171210
else:
1218-
show_results(prof, precision=options.precision, stream=out_file)
1211+
show_results(prof, precision=args.precision, stream=out_file)

0 commit comments

Comments
 (0)