def get_default_option_params(choice, child, expression_params): """Create a dictionary containing default values for output parameters that aren't used by this option. Returns a {param_name: value} dictionary.""" result = {} outputs = _get_unpopulated_outputs(choice, child, expression_params) for name, params in outputs.items(): # Find the possible range of source values, and intersect it with # the constraints placed on it by other earlier children. possible = Ranges(p.type.range(expression_params) for p in params) for c in choice.children[:choice.children.index(child)]: for r in _get_ranges(c.entry, name, expression_params): possible.remove(r) # Use the possible value closest to zero. positive = Range(0, None) negative = Range(None, 0) if possible.intersect(positive): value = possible.intersect(positive)[0].min elif possible.intersect(negative): value = possible.intersect(positive)[-1].max else: value = 0 logging.warning('Unable to choose a good output for %s; using %s', name, value) result[name] = value return result
def get_default_option_params(choice, child, expression_params, encode_expression_params): """Create a dictionary containing default values for output parameters that aren't used by this option. Returns a {param_name: value} dictionary.""" result = {} outputs = _get_unpopulated_outputs(choice, child, encode_expression_params) for name, params in outputs.items(): # Find the possible range of source values, and intersect it with # the constraints placed on it by other earlier children. possible = Ranges(p.type.range(expression_params) for p in params) for c in choice.children[:choice.children.index(child)]: for r in _get_ranges(c.entry, name, encode_expression_params): possible.remove(r) result[name] = possible.get_default() return result
def test_remove_all(self): r = Ranges([Range(5, 10), Range(20, 25)]) r.remove(Range(None, None)) self.assertEqual([], r.get_ranges())
def test_remove_from_middle(self): r = Ranges([Range(10, 20)]) r.remove(Range(15, 15)) self.assertEqual([Range(10, 14), Range(16, 20)], r.get_ranges())
def test_remove_before_min(self): r = Ranges([Range(10, 20)]) r.remove(Range(0, 8)) self.assertEqual([Range(10, 20)], r.get_ranges())
def test_remove_past_max(self): r = Ranges([Range(10, 20)]) r.remove(Range(25, 30)) self.assertEqual([Range(10, 20)], r.get_ranges())
def test_remove_from_right(self): r = Ranges([Range(10, 20)]) r.remove(Range(15, 25)) self.assertEqual([Range(10, 14)], r.get_ranges())
def test_remove_from_left(self): r = Ranges([Range(10, 20)]) r.remove(Range(5, 15)) self.assertEqual([Range(16, 20)], r.get_ranges())