Best Python code snippet using autotest_python
parse_config.py
Source:parse_config.py
...18 super(ConfigParser, self).__init__(name, logger)19 self.data = json20 self.module_path = FrameworkUtils21 @property22 def parse_config_file(self) -> object:23 """24 :rtype: object25 """26 config_file = None27 if os.path.exists(CONFIG_PATH):28 try:29 with open(CONFIG_PATH) as config_file:30 data = json.load(config_file)31 return data32 except Exception as err:33 self.logger.warning(f"Could not read from file from {CONFIG_PATH}, exception: {err}")34 finally:35 config_file.close()36 else:...
test_parser.py
Source:test_parser.py
...9 # Test with an empty config10 test_config_path = os.path.join(test_configs_folder, subfolder, "empty_config")11 parser = Parser(test_config_path)12 with pytest.raises(ValueError):13 parser.parse_config_file()14 # Test with incorrect number of lines in the file15 test_config_path = os.path.join(test_configs_folder, subfolder, "incorrect_number_of_lines")16 parser = Parser(test_config_path)17 with pytest.raises(ValueError):18 parser.parse_config_file()19 def test_parsing_incorrect_lawn_sizes(self):20 subfolder = "incorrect_lawn_sizes"21 # Test with incorrect character in lawn size22 test_config_path = os.path.join(test_configs_folder, subfolder, "incorrect_character")23 parser = Parser(test_config_path)24 with pytest.raises(ValueError):25 parser.parse_config_file()26 # Test with missing value in lawn size27 test_config_path = os.path.join(test_configs_folder, subfolder, "missing_value")28 parser = Parser(test_config_path)29 with pytest.raises(ValueError):30 parser.parse_config_file()31 # Test with negative value in lawn size32 test_config_path = os.path.join(test_configs_folder, subfolder, "negative_value")33 parser = Parser(test_config_path)34 with pytest.raises(ValueError):35 parser.parse_config_file()36 def test_parsing_incorrect_mower_positions(self):37 subfolder = "incorrect_mower_positions"38 # Test with an incorrect direction39 test_config_path = os.path.join(test_configs_folder, subfolder, "incorrect_direction")40 parser = Parser(test_config_path)41 with pytest.raises(ValueError):42 parser.parse_config_file()43 # Test with a missing value in an position line44 test_config_path = os.path.join(test_configs_folder, subfolder, "missing_value")45 parser = Parser(test_config_path)46 with pytest.raises(ValueError):47 parser.parse_config_file()48 # Test with multiple mowers assigned to the same initial position49 test_config_path = os.path.join(test_configs_folder, subfolder, "multiple_mowers_on_position")50 parser = Parser(test_config_path)51 with pytest.raises(ValueError):52 parser.parse_config_file()53 # Test with a negative value in an initial position54 test_config_path = os.path.join(test_configs_folder, subfolder, "negative_value")55 parser = Parser(test_config_path)56 with pytest.raises(ValueError):57 parser.parse_config_file()58 # Test with a mower assigned to an out of bounds position59 test_config_path = os.path.join(test_configs_folder, subfolder, "out_of_bounds")60 parser = Parser(test_config_path)61 with pytest.raises(ValueError):62 parser.parse_config_file()63 # Test with a lower case direction64 test_config_path = os.path.join(test_configs_folder, subfolder, "lower_case_direction")65 parser = Parser(test_config_path)66 with pytest.raises(ValueError):67 parser.parse_config_file()68 def test_parsing_incorrect_mower_instructions(self):69 subfolder = "incorrect_mower_instructions"70 # Test with an incorrect direction71 test_config_path = os.path.join(test_configs_folder, subfolder, "incorrect_direction")72 parser = Parser(test_config_path)73 with pytest.raises(ValueError):74 parser.parse_config_file()75 # Test with a lower case direction76 test_config_path = os.path.join(test_configs_folder, subfolder, "lower_case_direction")77 parser = Parser(test_config_path)78 with pytest.raises(ValueError):79 parser.parse_config_file()80 def test_parsing_correct_configurations(self):81 subfolder = "correct_configs"82 # Test original example83 test_config_path = os.path.join(test_configs_folder, subfolder, "base_example")84 parser = Parser(test_config_path)85 lawn_size, mowers = parser.parse_config_file()86 assert lawn_size == (5, 5)87 assert mowers == [88 ((1, 2, "N"), ["L", "F", "L", "F", "L", "F", "L", "F", "F"]),89 ((3, 3, "E"), ["F", "F", "R", "F", "F", "R", "F", "R", "R", "F"])90 ]91 # Test with a big rectangle lawn92 test_config_path = os.path.join(test_configs_folder, subfolder, "with_big_rectangle_lawn")93 parser = Parser(test_config_path)94 lawn_size, mowers = parser.parse_config_file()95 assert lawn_size == (300, 500)96 assert mowers == [97 ((80, 236, "W"), ["L", "F", "L", "F"]),98 ((300, 378, "S"), ["F", "F", "R", "F"])99 ]100 # Test with many mowers101 test_config_path = os.path.join(test_configs_folder, subfolder, "with_many_mowers")102 parser = Parser(test_config_path)103 lawn_size, mowers = parser.parse_config_file()104 assert lawn_size == (10, 10)105 assert mowers == [106 ((6, 4, "N"), ["L", "F"]),107 ((3, 3, "S"), ["R", "F", "F", "R"]),108 ((1, 2, "S"), ["L", "F", "L", "F", "L", "F", "L", "F", "F"]),109 ((2, 3, "E"), ["R", "F", "R", "L", "L", "R", "F"]),110 ((1, 8, "W"), ["L", "F", "L", "F"]),111 ((4, 3, "E"), ["F", "F", "R", "L"]),112 ((1, 4, "N"), ["L", "F", "L", "F", "L", "F", "L", "F", "F"]),113 ((4, 5, "N"), ["F", "F", "R", "F"]),114 ((1, 1, "N"), ["F", "F"]),115 ((3, 4, "E"), ["F", "R", "R", "F"]),116 ((8, 8, "W"), ["F", "L", "F", "F"]),117 ((3, 9, "E"), ["F", "F", "R", "F"]),...
test.py
Source:test.py
1import hjson2import unittest3def parse_config_file(location):4 with open(location, 'r') as f:5 model_json = hjson.loads(f.read())6 return model_json7def validate_config(configs):8 9 # Checking the Input key10 if 'Input' not in configs.keys():11 return False12 13 if configs['Input']['shape'] == None or \14 configs['Input']['flatten'] == None:15 return False16 17 if type(configs['Input']['shape']) != int or \18 type(configs['Input']['flatten']) != bool:19 raise ValueError('Input shape or flatten is of a wrong data type.')20 # Checking the Layers key21 if 'Layers' not in configs.keys():22 return False23 24 for idx, layer in enumerate(configs["Layers"]):25 if layer['type'] == None or layer['neurons'] == None or \26 layer['activation'] == None:27 return False28 if type(layer['type']) != str or type(layer['neurons']) != int or \29 type(layer['activation']) != str:30 raise ValueError(f'On Layer {idx}, type, neurons or activation is of a wrong data type')31 32 if layer['type'] != 'dense' and layer['type'] != 'conv2d': 33 return False34 if layer['activation'] != 'relu' and layer['activation'] != 'sigmoid' and \35 layer['activation'] != 'softmax':36 return False37 38 # Checking the Optimizer key39 if 'Optimizer' not in configs.keys():40 return False41 if configs['Optimizer']['name'] == None or \42 configs['Optimizer']['learning_rate'] == None:43 return False44 if type(configs['Optimizer']['name']) != str or \45 type(configs['Optimizer']['learning_rate']) != float:46 return False47 if configs['Optimizer']['name'] != 'Adam' and configs['Optimizer']['name'] != 'SGD':48 return False49 # Checking the Loss_function key50 if 'Loss_function' not in configs.keys():51 return False52 if configs['Loss_function']['name'] == None:53 return False54 55 if type(configs['Loss_function']['name']) != str:56 return False57 if configs['Loss_function']['name'] != 'binary_crossentropy' and configs['Loss_function']['name'] != 'mean_squared_error':58 return False59 60 # Checking the Metrics key61 if 'Metrics' not in configs.keys():62 return False63 return True64# Unit testing class65class TestValidate(unittest.TestCase):66 # TODO write more config files and test them67 def test_validate_config(self):68 # False69 json1 = parse_config_file('config files to be tested\config1_False.hjson')70 self.assertEqual(validate_config(json1), False)71 json2 = parse_config_file('config files to be tested\config2_False.hjson')72 self.assertEqual(validate_config(json2), False)73 json3 = parse_config_file('config files to be tested\config3_False.hjson')74 self.assertEqual(validate_config(json3), False)75 json4 = parse_config_file('config files to be tested\config4_False.hjson')76 self.assertEqual(validate_config(json4), False)77 json5 = parse_config_file('config files to be tested\config5_False.hjson')78 self.assertEqual(validate_config(json5), False)79 # True80 json1 = parse_config_file('config files to be tested\config1_True.hjson')81 self.assertEqual(validate_config(json1), True)82 json2 = parse_config_file('config files to be tested\config2_True.hjson')83 self.assertEqual(validate_config(json2), True)84 json3 = parse_config_file('config files to be tested\config3_True.hjson')85 self.assertEqual(validate_config(json3), True)86 json4 = parse_config_file('config files to be tested\config4_True.hjson')87 self.assertEqual(validate_config(json4), True)88 json5 = parse_config_file('config files to be tested\config5_True.hjson')89 self.assertEqual(validate_config(json5), True)90if __name__ == '__main__':...
parse_vtr_flow.py
Source:parse_vtr_flow.py
1#!/usr/bin/env python32"""3Module to parse the vtr flow results.4"""5import sys6from pathlib import Path7import glob8from collections import OrderedDict, defaultdict9# pylint: disable=wrong-import-position10sys.path.insert(0, str(Path(__file__).resolve().parent.parent))11import vtr12from vtr import paths13# pylint: enable=wrong-import-position14def parse_file_and_update_results(filename, patterns, results):15 """16 Find filename, and then look through for the matching patterns, updating results17 """18 # We interpret the parse pattern's filename as a glob pattern19 filepaths = glob.glob(filename)20 if len(filepaths) > 1:21 raise vtr.InspectError(22 "File pattern '{}' is ambiguous ({} files matched)".format(filename, len(filepaths)),23 len(filepaths),24 filepaths,25 )26 if len(filepaths) == 1:27 assert Path(filepaths[0]).exists28 with open(filepaths[0], "r") as file:29 for line in file:30 while line[0] == "#":31 line = line[1:]32 for parse_pattern in patterns:33 match = parse_pattern.regex().match(line)34 if match and match.groups():35 # Extract the first group value36 results[parse_pattern] = match.groups()[0]37def parse_vtr_flow(arg_list):38 """39 parse vtr flow output40 """41 parse_path = arg_list[0]42 parse_config_file = arg_list[1]43 extra_params = arg_list[2:]44 parse_config_file = vtr.util.verify_file(parse_config_file, "parse config")45 if parse_config_file is None:46 parse_config_file = str(paths.vtr_benchmarks_parse_path)47 parse_patterns = vtr.load_parse_patterns(str(parse_config_file))48 results = OrderedDict()49 extra_params_parsed = OrderedDict()50 for param in extra_params:51 key, value = param.split("=", 1)52 extra_params_parsed[key] = value53 print(key, end="\t")54 for parse_pattern in parse_patterns.values():55 # Set defaults56 results[parse_pattern] = (57 parse_pattern.default_value() if parse_pattern.default_value() is not None else "-1"58 )59 # Print header row60 print(parse_pattern.name(), end="\t")61 print("")62 for key, value in extra_params_parsed.items():63 print(value, end="\t")64 # Group parse patterns by filename so that we only need to read each log file from disk once65 parse_patterns_by_filename = defaultdict(list)66 for parse_pattern in parse_patterns.values():67 parse_patterns_by_filename[parse_pattern.filename()].append(parse_pattern)68 # Process each pattern69 for filename, patterns in parse_patterns_by_filename.items():70 parse_file_and_update_results(str(Path(parse_path) / filename), patterns, results)71 # Print results72 for parse_pattern in parse_patterns.values():73 print(results[parse_pattern], end="\t")74 print("")75 return 076if __name__ == "__main__":...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!