Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Use argparse cmd line parser instead of optparse#2

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
fabianp merged 2 commits intopythonprofilers:masterfromoctavo:master
May 13, 2012
Merged
Changes fromall 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
50 changes: 19 additions & 31 deletionsmemory_profiler.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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
Expand DownExpand Up@@ -249,36 +248,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 <outfile>', 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)

[8]ページ先頭

©2009-2026 Movatter.jp