def test_timings(self): clock = MockClock() with Timer('a', clock=clock.get_time): clock.add_seconds(10) with Timer('a', clock=clock.get_time): clock.add_seconds(5) self.assertIn('a', timings, "Timings dict did not contain timing name") a = timings['a'] self.assertIn('total_elapsed', a, "Timing didn't include a total_elapsed") self.assertEquals(a['total_elapsed'], 15) self.assertIn('a', timings(), "Timings dict could not be accessed as a function.")
def test_signals(self): d = {} def callback(*args, **kws): d['called'] = 1 post_timing.connect(callback) with Timer('a'): pass assert d['called'] is 1
def derive_quantities(self, new_quantities, quantity_pool=None, allow_model_failure=True): """ Algorithm for expanding quantity pool Args: new_quantities ([Quantity]): list of quantities which to consider as new inputs to models quantity_pool ({symbol: {Quantity}}): dict of quantity sets keyed by symbol from which to draw additional quantities for model inputs Returns: additional_quantities ([Quantity]): new derived quantities quantity_pool ({symbol: {Quantity}}): augmented version of quantity pool """ # Update quantity pool quantity_pool = quantity_pool or defaultdict(set) for quantity in new_quantities: quantity_pool[quantity.symbol].add(quantity) # Generate all of the models and input sets to be evaluated logger.info("Generating models and input sets for %s", new_quantities) models_and_input_sets = self.generate_models_and_input_sets( new_quantities, quantity_pool) added_quantities = [] # Evaluate model for each input set and add new valid quantities for model_and_input_set in models_and_input_sets: model = model_and_input_set[0] inputs = model_and_input_set[1:] input_dict = {q.symbol: q for q in inputs} logger.info('Evaluating %s with input %s', model, input_dict) with Timer(model.name): result = model.evaluate(input_dict, allow_failure=allow_model_failure) # TODO: Maybe provenance should be done in evaluate? success = result.pop('successful') if success: noncyclic = filter(lambda x: not x.is_cyclic(), result.values()) added_quantities.extend(list(noncyclic)) else: logger.info("Model evaluation unsuccessful %s", result['message']) return added_quantities, quantity_pool
def time_stuff(): clock = MockClock() for i in range(5): with Timer('test', clock=clock.get_time): clock.add_seconds(1) all_timings.append(timings.copy())
def test_stack(self): with Timer('a'): with Timer('b'): assert stack == ('a', 'b')
def _inner(*args, **kws): with Timer(name or func.__name__, clock=clock): result = func(*args, **kws) return result