-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathany_line_profiler.py
33 lines (28 loc) · 1.01 KB
/
any_line_profiler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env python
# -*- coding utf-8 -*-
from cStringIO import StringIO
from line_profiler import LineProfiler
class AnyLineProfiler(LineProfiler):
def __init__(self, user_func=None):
"""
@user_func - can be supplied in keyword args and will be used for post processing output, for example printing/logging/writing to ..
"""
self.user_func = user_func or self.print_io
LineProfiler.__init__(self)
def wrap_function(self, func):
""" Wrap a function to profile it.
"""
def f(*args, **kwds):
self.enable_by_count()
self.output = StringIO()
try:
result = func(*args, **kwds)
finally:
self.disable_by_count()
self.print_stats(stream=self.output)
self.user_func(self.output.getvalue())
return result
return f
def print_io(self, output):
"""Internal print function for viewing results of profile"""
print output