50 """ |
50 """ |
51 if not os.path.exists(self.timingCache): |
51 if not os.path.exists(self.timingCache): |
52 return |
52 return |
53 |
53 |
54 try: |
54 try: |
55 cache = open(self.timingCache, 'rb') |
55 with open(self.timingCache, 'rb') as cache: |
56 timings = marshal.load(cache) # secok |
56 timings = marshal.load(cache) # secok |
57 if isinstance(timings, dict): |
57 if isinstance(timings, dict): |
58 self.timings = timings |
58 self.timings = timings |
59 except (EnvironmentError, EOFError, ValueError, TypeError): |
59 except (EnvironmentError, EOFError, ValueError, TypeError): |
60 pass |
60 pass |
61 finally: |
|
62 cache.close() |
|
63 |
61 |
64 def save(self): |
62 def save(self): |
65 """ |
63 """ |
66 Public method to store the collected profile data. |
64 Public method to store the collected profile data. |
67 """ |
65 """ |
68 # dump the raw timing data |
66 # dump the raw timing data |
69 try: |
67 try: |
70 cache = open(self.timingCache, 'wb') |
68 with open(self.timingCache, 'wb') as cache: |
71 marshal.dump(self.timings, cache) |
69 marshal.dump(self.timings, cache) |
72 except EnvironmentError: |
70 except EnvironmentError: |
73 pass |
71 pass |
74 finally: |
|
75 cache.close() |
|
76 |
72 |
77 # dump the profile data |
73 # dump the profile data |
78 self.dump_stats(self.profileCache) |
74 self.dump_stats(self.profileCache) |
79 |
75 |
80 def dump_stats(self, file): |
76 def dump_stats(self, file): |
81 """ |
77 """ |
82 Public method to dump the statistics data. |
78 Public method to dump the statistics data. |
83 |
79 |
84 @param file name of the file to write to (string) |
80 @param file name of the file to write to (string) |
85 """ |
81 """ |
|
82 self.create_stats() |
86 try: |
83 try: |
87 f = open(file, 'wb') |
84 with open(file, 'wb') as f: |
88 self.create_stats() |
85 pickle.dump(self.stats, f, 4) |
89 pickle.dump(self.stats, f, 4) |
|
90 except (EnvironmentError, pickle.PickleError): |
86 except (EnvironmentError, pickle.PickleError): |
91 pass |
87 pass |
92 finally: |
|
93 f.close() |
|
94 |
88 |
95 def erase(self): |
89 def erase(self): |
96 """ |
90 """ |
97 Public method to erase the collected timing data. |
91 Public method to erase the collected timing data. |
98 """ |
92 """ |