Coverage for /home/ubuntu/Documents/Research/mut_p6/sacred/sacred/config/captured_function.py: 94%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1#!/usr/bin/env python
2# coding=utf-8
4import time
5from datetime import timedelta
7import wrapt
8from sacred.config.custom_containers import fallback_dict
9from sacred.config.signature import Signature
10from sacred.randomness import create_rnd, get_seed
11from sacred.utils import ConfigError
14def create_captured_function(function, prefix=None):
15 sig = Signature(function)
16 function.signature = sig
17 function.uses_randomness = "_seed" in sig.arguments or "_rnd" in sig.arguments
18 function.logger = None
19 function.config = {}
20 function.rnd = None
21 function.run = None
22 function.prefix = prefix
23 return captured_function(function)
26@wrapt.decorator
27def captured_function(wrapped, instance, args, kwargs):
28 options = fallback_dict(
29 wrapped.config, _config=wrapped.config, _log=wrapped.logger, _run=wrapped.run
30 )
31 if wrapped.uses_randomness: # only generate _seed and _rnd if needed
32 options["_seed"] = get_seed(wrapped.rnd)
33 options["_rnd"] = create_rnd(options["_seed"])
35 bound = instance is not None
36 args, kwargs = wrapped.signature.construct_arguments(args, kwargs, options, bound)
37 if wrapped.logger is not None:
38 wrapped.logger.debug("Started")
39 start_time = time.time()
40 # =================== run actual function =================================
41 with ConfigError.track(wrapped.config, wrapped.prefix):
42 result = wrapped(*args, **kwargs)
43 # =========================================================================
44 if wrapped.logger is not None:
45 stop_time = time.time()
46 elapsed_time = timedelta(seconds=round(stop_time - start_time))
47 wrapped.logger.debug("Finished after %s.", elapsed_time)
49 return result