Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
timestamp=self.timestamp,
metadata=metadata)
if not shelving:
# When shelving, we do not need to load the output
out = self.store_backend.load_item(
[func_id, args_id],
msg=msg,
verbose=self._verbose)
else:
out = None
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)
except Exception:
# XXX: Should use an exception logger
_, signature = format_signature(self.func, *args, **kwargs)
self.warn('Exception while loading results for '
'{}\n {}'.format(signature, traceback.format_exc()))
must_call = True
if must_call:
out, metadata = self.call(*args, **kwargs)
if self.mmap_mode is not None:
# Memmap the output at the first call to be consistent with
# later calls
if self._verbose:
msg = _format_load_msg(func_id, args_id,
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 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
""" Helper function to format the message when loading the results.
"""
signature = ""
try:
if metadata is not None:
args = ", ".join(['%s=%s' % (name, value)
for name, value
in metadata['input_args'].items()])
signature = "%s(%s)" % (os.path.basename(func_id), args)
else:
signature = os.path.basename(func_id)
except KeyError:
pass
if timestamp is not None:
ts_string = "{0: <16}".format(format_time(time.time() - timestamp))
else:
ts_string = ""
return '[Memory]{0}: Loading {1}'.format(ts_string, str(signature))
persist the output values.
"""
start_time = time.time()
func_id, args_id = self._get_output_identifiers(*args, **kwargs)
if self._verbose > 0:
print(format_call(self.func, args, kwargs))
output = self.func(*args, **kwargs)
self.store_backend.dump_item(
[func_id, args_id], output, verbose=self._verbose)
duration = time.time() - start_time
metadata = self._persist_input(duration, args, kwargs)
if self._verbose > 0:
_, name = get_func_name(self.func)
msg = '%s - %s' % (name, format_time(duration))
print(max(0, (80 - len(msg))) * '_' + msg)
return output, metadata