def load_output(self, output_dir): """ Read the results of a previous calculation from the directory it was cached in. """ if self._verbose > 1: t = time.time() - self.timestamp if self._verbose < 10: print '[Memory]% 16s: Loading %s...' % ( format_time(t), self.format_signature(self.func)[0]) else: print '[Memory]% 16s: Loading %s from %s' % (format_time( t), self.format_signature(self.func)[0], output_dir) filename = os.path.join(output_dir, 'output.pkl') return numpy_pickle.load(filename, mmap_mode=self.mmap_mode)
def __call__(self, *args, **kwargs): # Compare the function code with the previous to see if the # function code has changed output_dir, argument_hash = self.get_output_dir(*args, **kwargs) # FIXME: The statements below should be try/excepted if not (self._check_previous_func_code(stacklevel=3) and os.path.exists(output_dir)): if self._verbose > 10: _, name = get_func_name(self.func) self.warn('Computing func %s, argument hash %s in ' 'directory %s' % (name, argument_hash, output_dir)) return self.call(*args, **kwargs) else: try: t0 = time.time() out = self.load_output(output_dir) if self._verbose > 4: t = time.time() - t0 _, name = get_func_name(self.func) msg = '%s cache loaded - %s' % (name, format_time(t)) print max(0, (80 - len(msg))) * '_' + msg return out except Exception: # XXX: Should use an exception logger self.warn('Exception while loading results for ' '(args=%s, kwargs=%s)\n %s' % (args, kwargs, traceback.format_exc())) shutil.rmtree(output_dir, ignore_errors=True) return self.call(*args, **kwargs)
def load_output(self, output_dir): """ Read the results of a previous calculation from the directory it was cached in. """ if self._verbose > 1: t = time.time() - self.timestamp if self._verbose < 10: print "[Memory]% 16s: Loading %s..." % (format_time(t), self.format_signature(self.func)[0]) else: print "[Memory]% 16s: Loading %s from %s" % ( format_time(t), self.format_signature(self.func)[0], output_dir, ) filename = os.path.join(output_dir, "output.pkl") return numpy_pickle.load(filename, mmap_mode=self.mmap_mode)
def call(self, *args, **kwargs): """ Force the execution of the function with the given arguments and persist the output values. """ start_time = time.time() output_dir, argument_hash = self.get_output_dir(*args, **kwargs) if self._verbose: print self.format_call(*args, **kwargs) output = self.func(*args, **kwargs) self._persist_output(output, output_dir) duration = time.time() - start_time if self._verbose: _, name = get_func_name(self.func) msg = '%s - %s' % (name, format_time(duration)) print max(0, (80 - len(msg))) * '_' + msg return output