def check_config(self, **cfg: Union[int, str]) -> Dict[str, Union[int, str]]: """ Add default values to the dictionary if there are missing elements and check if the dictionary is correct :param cfg: matching_cost configuration :type cfg: dict :return cfg: matching_cost configuration updated :rtype: dict """ # Give the default value if the required element is not in the configuration if "window_size" not in cfg: cfg["window_size"] = self._WINDOW_SIZE if "subpix" not in cfg: cfg["subpix"] = self._SUBPIX schema = { "matching_cost_method": And(str, lambda x: x == "mc_cnn"), "window_size": And(int, lambda x: x == 11), "subpix": And(int, lambda x: x == 1), "mc_cnn_arch": And(str, lambda x: x in ("fast", "accurate")), "model_path": And(str, lambda x: os.path.exists(x)), } checker = Checker(schema) checker.validate(cfg) return cfg
def lambda_handler(event, context): current_data = {'first_key': 1, 'second_key': '2'} expected_schema = {'first_key': int, 'second_key': str} checker = Checker(expected_schema) s3_events = [] for record in event['Records']: s3_events.append({ "bucket": record['s3']['bucket']['name'], "file": record['s3']['object']['key'] }) data = {"result": checker.validate(current_data), "s3_events": s3_events} try: response = requests.post( 'https://python-lambda.free.beeceptor.com/my/api/path', data=json.dumps(data)) except Exception as error: print('Error in request: ', str(error)) return { "statusCode": 200, "body": json.dumps({ "result": checker.validate(current_data), "s3_events": s3_events }), }
def check_input_json(event): expected_schema = { "AccountName": str, "Email": str, "RoleName": str, "Tags": [dict] } checker = Checker(expected_schema) result = checker.validate(event) assert result == event
def check_json(conf, schema): """ Check a dictionary with respect to a schema :param conf: The dictionary to check :type conf: dict :param schema: The schema to use :type schema: dict :returns: conf if check succeeds (else raises CheckerError) :rtype: dict """ schema_validator = Checker(schema) checked_conf = schema_validator.validate(conf) return checked_conf
def test_checker_assert(expected, current, soft): with pytest.raises(CheckerError): Checker(expected, soft).validate(current)
def test_checker_string_with_callable_data(): c = Checker(lambda x: x is True) assert str(c) == "<Checker soft=False expected=<lambda>>"
def test_checker_list_dicts_soft(): with pytest.raises(CheckerError): c = Checker([{"key1": int}], soft=True) c.validate([{"key1": 1}, {"key1": 1}, {"key1": "1"}])
def test_checker_list_dicts_hard(): with pytest.raises(ListCheckerError): c = Checker([{"key1": int}], soft=False) c.validate([{"key1": 1}, {"key1": 1}, {"key1": "1"}])
def test_checker_list_dicts_hard_positive(soft): data = [{"key1": 1}, {"key1": 2}, {"key1": 3}] c = Checker([{"key1": int}], soft=soft) assert c.validate(data) == data
def check_conf( self, **cfg: Union[str, int, float, bool] ) -> Dict[str, Union[str, int, float, bool]]: """ Add default values to the dictionary if there are missing elements and check if the dictionary is correct :param cfg: optimization configuration :type cfg: dict :return cfg: optimization configuration updated :rtype cfg: dict """ # Give the default value if the required element is not in the configuration if "P1" not in cfg: cfg["P1"] = self._P1 if "P2" not in cfg: cfg["P2"] = self._P2 if "alpha" not in cfg: cfg["alpha"] = self._ALPHA if "beta" not in cfg: cfg["beta"] = self._BETA if "gamma" not in cfg: cfg["gamma"] = self._GAMMA if "p2_method" not in cfg: cfg["p2_method"] = self._P2_METHOD if "overcounting" not in cfg: cfg["overcounting"] = self._OVERCOUNTING if "min_cost_paths" not in cfg: cfg["min_cost_paths"] = self._MIN_COST_PATH if "use_confidence" not in cfg: cfg["use_confidence"] = self._USE_CONFIDENCE if "piecewise_optimization_layer" not in cfg: cfg["piecewise_optimization_layer"] = self._PIECEWISE_OPTIMIZATION_LAYER p1_value = cfg["P1"] schema = { "sgm_version": And( str, lambda x: is_method( x, ["c++", "python_libsgm", "python_libsgm_parall"])), "optimization_method": And(str, lambda x: is_method(x, ["sgm"])), "penalty_method": And(str, lambda x: is_method(x, ["sgm_penalty"])), "P1": And(Or(int, float), lambda x: x > 0), "P2": And(Or(int, float), lambda x: x > p1_value), "alpha": And(Or(int, float), lambda x: x >= 0), "beta": And(Or(int, float), lambda x: x > 0), "gamma": And(Or(int, float), lambda x: x > 0), "p2_method": And( str, lambda x: is_method( x, ["constant", "negativeGradient", "inverseGradient"])), "overcounting": bool, "min_cost_paths": bool, "use_confidence": bool, "piecewise_optimization_layer": And(str, lambda x: is_method(x, ["None", "classif", "segm"])), } checker = Checker(schema) checker.validate(cfg) return cfg
def test_checker_string(): c = Checker(int) assert str(c) == "<Checker soft=False expected=int>"
def test_soft_checker_with_errors(expected, current): with pytest.raises(CheckerError): Checker(expected, soft=True).validate(current)
def test_create_checker_instance_with_default_param(): c = Checker(int) assert c.expected_data is int assert c.soft is False assert c.ignore_extra_keys is False
def test_checker_positive(expected, current, soft): assert Checker(expected, soft).validate(current) == current
def check_conf(self, **cfg: Union[str, int, float, bool]) -> Dict[str, Union[str, int, float, bool]]: """ Add default values to the dictionary if there are missing elements and check if the dictionary is correct :param cfg: optimization configuration :type cfg: dict :return cfg: optimization configuration updated :rtype cfg: dict """ # Load default penalties according to the type of mc-cnn measure if cfg["penalty_method"] == "mc_cnn_accurate_penalty": default_p1 = self._P1_ACCURATE default_p2 = self._P2_ACCURATE default_q1 = self._Q1_ACCURATE default_q2 = self._Q2_ACCURATE default_d = self._D_ACCURATE default_v = self._V_ACCURATE else: default_p1 = self._P1_FAST default_p2 = self._P2_FAST default_q1 = self._Q1_FAST default_q2 = self._Q2_FAST default_d = self._D_FAST default_v = self._V_FAST # Give the default value if the required element is not in the configuration if "P1" not in cfg: cfg["P1"] = default_p1 if "P2" not in cfg: cfg["P2"] = default_p2 if "Q1" not in cfg: cfg["Q1"] = default_q1 if "Q2" not in cfg: cfg["Q2"] = default_q2 if "D" not in cfg: cfg["D"] = default_d if "V" not in cfg: cfg["V"] = default_v if "overcounting" not in cfg: cfg["overcounting"] = self._OVERCOUNTING if "min_cost_paths" not in cfg: cfg["min_cost_paths"] = self._MIN_COST_PATH if "use_confidence" not in cfg: cfg["use_confidence"] = self._USE_CONFIDENCE if "piecewise_optimization_layer" not in cfg: cfg["piecewise_optimization_layer"] = self._PIECEWISE_OPTIMIZATION_LAYER p1_value = cfg["P1"] schema = { "sgm_version": And( str, lambda x: is_method(x, ["c++", "python_libsgm", "python_libsgm_parall"]), ), "optimization_method": And(str, lambda x: is_method(x, ["sgm"])), "penalty_method": And( str, lambda x: is_method(x, ["mc_cnn_fast_penalty", "mc_cnn_accurate_penalty"]), ), "P1": And(Or(int, float), lambda x: x > 0), "P2": And(Or(int, float), lambda x: x > p1_value), "Q1": And(Or(int, float), lambda x: x > 0), "Q2": And(Or(int, float), lambda x: x > 0), "D": And(Or(int, float), lambda x: x >= 0), "V": And(Or(int, float), lambda x: x > 0), "overcounting": bool, "min_cost_paths": bool, "use_confidence": bool, "piecewise_optimization_layer": And(str, lambda x: is_method(x, ["None", "classif", "segm"])), } checker = Checker(schema) checker.validate(cfg) return cfg
def test_miss_keys(expected, current, exp_exception): with pytest.raises(exp_exception): Checker(expected).validate(current)
def test_miss_keys_soft(expected, current): with pytest.raises(CheckerError): Checker(expected, soft=True).validate(current)
def test_create_checker_instance_with_custom_param(): c = Checker(int, True, True) assert c.expected_data is int assert c.soft is True assert c.ignore_extra_keys is True
def test_repr_checker_class(test_data, expected_result): c = Checker(test_data, soft=True) assert c.__str__() == expected_result
def test_checker_with_errors(expected, current, exception): with pytest.raises(exception): Checker(expected, soft=False).validate(current)