Skip to content

Commit 570a886

Browse files
author
Fabian Pedregosa
committed
Merge pull request #4 from octavo/master
Fix Python3 Support
2 parents f2a308a + 2e298df commit 570a886

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

README.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ To install from source, download the package, extract and type::
2828
=======
2929
The line-by-line profiler is used much in the same way of the
3030
line_profiler: you must first decorate the function you would like to
31-
profile with @profile. In this example, we create a simple function
32-
*my_func* that allocates tuples a, b and then deletes b::
31+
profile with ``@profile``. In this example, we create a simple function
32+
``my_func`` that allocates lists ``a``, ``b`` and then deletes ``b``::
3333

3434

3535
@profile
@@ -43,7 +43,7 @@ profile with @profile. In this example, we create a simple function
4343
my_func()
4444

4545

46-
execute the code passing the option "-m memory_profiler" to the
46+
Execute the code passing the option ``-m memory_profiler`` to the
4747
python interpreter to load the memory_profiler module and print to
4848
stdout the line-by-line analysis. If the file name was example.py,
4949
this would result in::

memory_profiler.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ def _get_memory(pid):
2828
# .. subprocess.check_output appeared in 2.7, using Popen ..
2929
# .. for backwards compatibility ..
3030
out = subprocess.Popen(['ps', 'v', '-p', str(pid)],
31-
stdout=subprocess.PIPE).communicate()[0].split('\n')
31+
stdout=subprocess.PIPE).communicate()[0].split(b'\n')
3232
try:
33-
vsz_index = out[0].split().index('RSS')
33+
vsz_index = out[0].split().index(b'RSS')
3434
return float(out[1].split()[vsz_index]) / 1024
3535
except:
3636
return -1
@@ -116,7 +116,7 @@ def _find_script(script_name):
116116
if os.path.isfile(fn):
117117
return fn
118118

119-
print >> sys.stderr, 'Could not find script %s' % script_name
119+
print >> sys.stderr, 'Could not find script {0}'.format(script_name)
120120
raise SystemExit(1)
121121

122122

@@ -141,10 +141,11 @@ def add_function(self, func):
141141
""" Record line profiling information for the given Python function.
142142
"""
143143
try:
144-
code = func.func_code
144+
code = func.__code__
145145
except AttributeError:
146146
import warnings
147-
warnings.warn("Could not extract a code object for the object %r" % (func,))
147+
warnings.warn("Could not extract a code object for the object {0!r}"
148+
.format(func))
148149
return
149150
if code not in self.code_map:
150151
self.code_map[code] = {}
@@ -208,8 +209,8 @@ def show_results(prof, stream=None):
208209

209210
if stream is None:
210211
stream = sys.stdout
211-
template = '%6s %12s %10s %-s'
212-
header = template % ('Line #', 'Mem usage', 'Increment', 'Line Contents')
212+
template = '{0:>6} {1:>12} {2:>10} {3:<}'
213+
header = template.format('Line #', 'Mem usage', 'Increment', 'Line Contents')
213214
stream.write(header + '\n')
214215
stream.write('=' * len(header) + '\n')
215216

@@ -225,8 +226,7 @@ def show_results(prof, stream=None):
225226
lines_normalized = {}
226227

227228
# move everything one frame up
228-
keys = lines.keys()
229-
keys.sort()
229+
keys = sorted(lines.keys())
230230
increment = {}
231231
lines_normalized[code.co_firstlineno+1] = lines[keys[0]]
232232
while len(keys) > 1:
@@ -238,14 +238,14 @@ def show_results(prof, stream=None):
238238
for l in linenos:
239239
mem = ''
240240
inc = ''
241-
if lines_normalized.has_key(l):
241+
if l in lines_normalized:
242242
mem = max(lines_normalized[l])
243243
inc = mem - mem_old
244244
mem_old = mem
245-
mem = '%5.2f MB' % mem
246-
inc = '%5.2f MB' % inc
245+
mem = '{0:5.2f} MB'.format(mem)
246+
inc = '{0:5.2f} MB'.format(inc)
247247
line = linecache.getline(filename, l)
248-
stream.write(template % (l, mem, inc, line))
248+
stream.write(template.format(l, mem, inc, line))
249249

250250
if __name__ == '__main__':
251251
from argparse import ArgumentParser

0 commit comments

Comments
 (0)