Skip to content

Commit

Permalink
PEP8, typo fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
vene committed Jul 1, 2012
1 parent 0091f03 commit 5887e96
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions memory_profiler.py
@@ -1,11 +1,15 @@
"""Get process information"""
"""Profile the memory usage of a Python program"""

__version__ = '0.14'

_CMD_USAGE = "python -m memory_profiler script_file.py"

import time, sys, os, warnings
import linecache, inspect
import time
import sys
import os
import warnings
import linecache
import inspect

try:
import psutil
Expand Down Expand Up @@ -36,7 +40,8 @@ def _get_memory(pid):
else:
raise NotImplementedError('The psutil module is required for non-unix platforms')

def memory_usage(proc= -1, num= -1, interval=.1):

def memory_usage(proc=-1, num=-1, interval=.1):
"""
Return the memory usage of a process or piece of code
Expand Down Expand Up @@ -70,6 +75,7 @@ def memory_usage(proc= -1, num= -1, interval=.1):
filename = _find_script(proc)
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 All @@ -84,7 +90,7 @@ def f_exec(x, locals):
proc = (proc[0], proc[1], {})
p = Process(target=proc[0], args=proc[1], kwargs=proc[2])
p.start()
while p.is_alive(): # FIXME: or num
while p.is_alive(): # FIXME: or num
ret.append(_get_memory(p.pid))
time.sleep(interval)
else:
Expand All @@ -100,6 +106,7 @@ def f_exec(x, locals):
# ..
# .. utility functions for line-by-line ..


def _find_script(script_name):
""" Find the script.
Expand Down Expand Up @@ -202,6 +209,7 @@ def disable(self):
self.last_time = {}
sys.settrace(None)


def show_results(prof, stream=None):

if stream is None:
Expand All @@ -218,14 +226,16 @@ def show_results(prof, stream=None):
filename = filename[:-1]
stream.write('Filename: ' + filename + '\n\n')
if not os.path.exists(filename):
stream.write('ERROR: Could not find file ' + filenam + '\n')
stream.write('ERROR: Could not find file ' + filename + '\n')
continue
all_lines = linecache.getlines(filename)
sub_lines = inspect.getblock(all_lines[code.co_firstlineno-1:])
linenos = range(code.co_firstlineno, code.co_firstlineno + len(sub_lines))
sub_lines = inspect.getblock(all_lines[code.co_firstlineno - 1:])
linenos = range(code.co_firstlineno, code.co_firstlineno +
len(sub_lines))
lines_normalized = {}

header = template.format('Line #', 'Mem usage', 'Increment', 'Line Contents')
header = template.format('Line #', 'Mem usage', 'Increment',
'Line Contents')
stream.write(header + '\n')
stream.write('=' * len(header) + '\n')
# move everything one frame up
Expand All @@ -236,7 +246,8 @@ def show_results(prof, stream=None):
k = keys.pop(0)
while keys:
lines_normalized[k] = lines[keys[0]]
for i in range(len(lines_normalized[k_old]), len(lines_normalized[k])):
for i in range(len(lines_normalized[k_old]),
len(lines_normalized[k])):
lines_normalized[k][i] = -1.
k_old = k
k = keys.pop(0)
Expand Down Expand Up @@ -277,6 +288,7 @@ def show_results(prof, stream=None):
else:
import builtins
builtins.__dict__['profile'] = prof
exec(compile(open(__file__).read(), __file__, 'exec'), locals(), globals())
exec(compile(open(__file__).read(), __file__, 'exec'), locals(),
globals())

show_results(prof)

0 comments on commit 5887e96

Please sign in to comment.