Skip to content

Commit d445e87

Browse files
committed
-
1 parent 5e8f0aa commit d445e87

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

python_toolbox/cute_profile/cute_profile.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,12 @@ def profile(statement, globals_, locals_):
2525
except SystemExit:
2626
pass
2727
profile_.create_stats()
28-
profile_result = marshal.dumps(profile_.stats)
29-
return profile_result
28+
return profile_
3029

3130

3231
def profile_expression(expression, globals_, locals_):
33-
profile_result = profile('result = %s' % expression, globals_, locals_)
34-
return (locals()['result'], profile_result)
32+
profile_ = profile('result = %s' % expression, globals_, locals_)
33+
return (locals_['result'], profile_)
3534

3635

3736
def profile_ready(condition=None, off_after=True, profile_handler=None):
@@ -94,12 +93,12 @@ def inner(function_, *args, **kwargs):
9493
# This line puts it in locals, weird:
9594
decorated_function.original_function
9695

97-
result, profile_result = profile_expression(
96+
result, profile_ = profile_expression(
9897
'decorated_function.original_function(*args, **kwargs)',
9998
globals(), locals()
10099
)
101100

102-
decorated_function.profile_handler(profile_result)
101+
decorated_function.profile_handler(profile_)
103102

104103
return result
105104

python_toolbox/cute_profile/profile_handling.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import threading
55
import datetime as datetime_module
6+
import marshal
67
import abc
78
import os.path
89
import pstats
@@ -18,8 +19,13 @@
1819
class BaseProfileHandler(object):
1920
__metaclass__ = abc.ABCMeta
2021

22+
def __call__(self, profile):
23+
self.profile = profile
24+
self.profile_data = marshal.dumps(profile.stats)
25+
return self.handle()
26+
2127
@abc.abstractmethod
22-
def __call__(self):
28+
def handle(self):
2329
pass
2430

2531
default_file_name = caching.CachedProperty(
@@ -32,7 +38,7 @@ class AuxiliaryThreadProfileHandler(BaseProfileHandler):
3238

3339
thread = None
3440

35-
def __call__(self):
41+
def handle(self):
3642
self.thread = threading.Thread(target=self.thread_job)
3743
self.thread.start()
3844

@@ -54,13 +60,13 @@ def __init__(self, email_address, smtp_server, smtp_user, smtp_password,
5460
self.smtp_password = smtp_password
5561
self.use_tls = use_tls
5662

57-
def thread_job(self, profile_data):
63+
def thread_job(self):
5864
envelope = envelopes.Envelope(
5965
to_addr=self.email_address,
6066
subject='Profile data',
6167
)
6268

63-
envelope.add_attachment_from_memory(profile_data,
69+
envelope.add_attachment_from_memory(self.profile_data,
6470
self.default_file_name,
6571
'application/octet-stream')
6672

@@ -75,10 +81,10 @@ class FolderProfileHandler(AuxiliaryThreadProfileHandler):
7581
def __init__(self, folder_path):
7682
self.folder_path = folder_path
7783

78-
def thread_job(self, profile_data):
84+
def thread_job(self):
7985
with open(os.path.join(self.folder_path, self.default_file_name), \
8086
'wb') as output_file:
81-
output_file.write(profile_data)
87+
output_file.write(self.profile_data)
8288

8389

8490

@@ -87,9 +93,9 @@ class PrintProfileHandler(BaseProfileHandler):
8793
def __init__(self, sort_order):
8894
self.sort_order = sort_order
8995

90-
def __call__(self, profile_data):
91-
pstats.Stats(self).strip_dirs().sort_stats(self.sort_order). \
92-
print_stats()
96+
def handle(self):
97+
self.profile.print_stats(self.sort_order)
98+
9399

94100

95101

0 commit comments

Comments
 (0)