Best Python code snippet using lisa_python
fuzzer.py
Source:fuzzer.py
...206 self.args.problemdir = str(self.problem.directory.absolute())207 self.args.data_filter = re_argument(f"{self.seed}$")208 self.args.use_result_cache = False209 self.test_data = TestCaseGroup(problem.kattis_problem, fuzzing_directory)210 def _write_case(self, case: List[str]):211 with self.input_file.open(mode="wt", encoding="utf-8") as f:212 for line in case:213 f.write(line)214 f.write("\n")215 def _run_submission(self) -> Tuple[SubmissionResult, SubmissionResult]:216 time_limit_high = self.time_limit * 2217 self.problem.generate_answer_if_required(self.input_file, self.answer_file)218 return TestCase(self.problem.kattis_problem, str(self.input_file.with_suffix("")), self.test_data) \219 .run_submission(self.program, self.args, self.time_limit, time_limit_high)220 def __enter__(self):221 return self222 def evaluate(self) -> RunResult:223 FuzzingRun.randomize(self.case_seed_file, self.seed_file, FuzzingRun.RANDOM_RUNS, self.seed)224 self.problem.generate_input_if_required(self.seed_file, self.input_file)225 (result1, result2) = self._run_submission()226 logger.debug("Received initial feedback %s / %s", result1.verdict, result2.verdict)227 if result1.verdict is None or result1.runtime == -1.0:228 raise ValueError("No executions")229 if result1.verdict == "WA":230 logger.debug("Picking failing case")231 self.submission_logger.debug("Found problematic input, picking failing case")232 feedback_files = FuzzingRun.parse_feedback(result1)233 failing_case = ProblemLayout.first_failing_case(feedback_files["judgemessage.txt"], self.answer_file)234 layout = ProblemLayout(self.input_file)235 picked_case = layout.pick_case(self.input_file, failing_case)236 self._write_case(picked_case)237 self.submission_logger.debug("Running program again on singular case")238 self.problem.generate_answer_if_required(self.input_file, self.answer_file)239 (result1, result2) = self._run_submission()240 run_feedback = FuzzingRun.parse_feedback(result1)241 if result1.verdict == "WA":242 run_verdict = RunVerdict.WRONG_ANSWER243 else:244 run_verdict = RunVerdict.FEEDBACK_INCONSISTENCY245 elif result1.verdict == "RTE":246 # binary search for the error247 logger.debug("Search for RTE case")248 self.submission_logger.debug("Runtime error occurred, binary search for the test case")249 layout = ProblemLayout(self.input_file)250 first_half, second_half = layout.split_case(self.input_file)251 while first_half[0] != "0":252 self._write_case(first_half)253 self.submission_logger.debug("Running program again on half of remainder")254 self.problem.generate_answer_if_required(self.input_file, self.answer_file)255 (result1, result2) = self._run_submission()256 if result1.verdict == "RTE":257 self.submission_logger.debug("RTE occurred in first half")258 else:259 self.submission_logger.debug("RTE occurred in second half")260 self._write_case(second_half)261 first_half, second_half = layout.split_case(self.input_file)262 self.submission_logger.debug("Should have RTE case now")263 self._write_case(second_half)264 self.submission_logger.debug("Running program on RTE case")265 self.problem.generate_answer_if_required(self.input_file, self.answer_file)266 (result1, result2) = self._run_submission()267 run_feedback = FuzzingRun.parse_feedback(result1)268 if result1.verdict == "RTE":269 run_verdict = RunVerdict.RUNTIME_EXCEPTION270 else:271 run_verdict = RunVerdict.FEEDBACK_INCONSISTENCY272 else:273 run_feedback = FuzzingRun.parse_feedback(result1)274 run_verdict = RunVerdict.get(result1.verdict)275 logger.debug("Finished run on %s (with seed %s) with verdict %s",276 self.case_seed_file.name, self.seed, run_verdict)277 return RunResult(self.problem, self.seed_file, self.input_file, self.answer_file, run_verdict, run_feedback)...
test_spec_gen.py
Source:test_spec_gen.py
...36 func_visitor.visit(tree)37 for suite in extract_metadata(cls_visitor.get_suites()):38 _write_suite(test_spec, suite)39 for case in extract_metadata(func_visitor.get_cases()):40 _write_case(test_spec, case)41def _write_title(file: TextIO) -> None:42 """43 Writes the title of test specifications44 Args:45 file (TextIO): test spec file46 """47 title = "Test Specification"48 file.write(title + "\n")49 file.write("=" * len(title) + "\n")50 file.write("\n")51 file.write("This file lists all test cases' specifications.\n")52 file.write("\n")53def _write_suite(file: TextIO, metadata: Dict[str, str]) -> None:54 """55 Writes info of a test suite.56 Args:57 file (TextIO): test spec file58 metadata (Dict[str, str]): test suite metadata59 """60 file.write(".. _" + metadata["name"] + ":\n") # custom anchor61 file.write("\n")62 file.write(".. class:: ")63 file.write(metadata["name"] + "\n") # Test Suite Name64 file.write(" :noindex:" + "\n")65 file.write("\n")66 _write_description(file, metadata, True) # Description67 file.write(" :platform: ")68 file.write("``" + "Azure, Ready" + "``\n") # Platform69 file.write(" :area: ")70 file.write("``" + metadata["area"] + "``\n") # Area71 file.write(" :category: ")72 file.write("``" + metadata["category"] + "``\n") # Category73 file.write("\n")74def _write_case(file: TextIO, metadata: Dict[str, str]) -> None:75 """76 Writes info of a test case.77 Args:78 file (TextIO): test spec file79 metadata (Dict[str, str]): test case metadata80 """81 file.write(".. _" + metadata["name"] + ":\n") # custom anchor82 file.write("\n")83 file.write(" .. method:: ")84 file.write(metadata["name"] + "\n") # Test Case Name85 file.write(" :noindex:" + "\n")86 file.write("\n")87 file.write(" ") # 1-tab indentation88 _write_description(file, metadata) # Description...
xfoil.py
Source:xfoil.py
...34 xtr_bottom = 0.535 def __init__(self, airfoil: openglider.airfoil.Profile2D) -> None:36 super().__init__()37 self.airfoil = airfoil38 def _write_case(self, directory):39 command_file = os.path.join(directory, "xfoil_command.dat")40 alphas = "\n".join(["Alfa\n{}".format(alpha) for alpha in self.alpha])41 cmd_string = f"""42 PLOP43 g44 LOAD airfoil.dat45 CADD46 PANEL47 OPER48 VISC {self.re_number}49 VPAR50 n51 {self.ncrit}52 xtr53 {self.xtr_top}54 {self.xtr_bottom}55 PACC56 {self.file_result}57 dump.dat58 {alphas}59 quit60 """61 cmd_string = "\n".join([line.strip() for line in cmd_string.split("\n")])62 with open(command_file, "w") as outfile:63 outfile.write(cmd_string)64 65 self.airfoil.export_dat(os.path.join(directory, "airfoil.dat"))66 return command_file67 def _read_result(self, filename):68 rex_number = r"([+-]?\d+\.\d+)"69 rex_line_str = "\s+" + "\s+".join([rex_number]*9)70 rex_line = re.compile(rex_line_str)71 values = [72 "alpha",73 "ca",74 "cw",75 "cwp",76 "cm",77 "top_xtr",78 "bottom_xtr",79 "top_ltr",80 "bottom_ltr"81 ]82 data = []83 with open(filename, "r") as infile:84 for line in infile.readlines():85 match = rex_line.match(line)86 if match:87 dct = {}88 for name, value in zip(values, match.groups()):89 dct[name] = float(value)90 data.append(dct)91 df = pandas.DataFrame(data).set_index("alpha")92 df["glide"] = df["ca"]/df["cw"]93 94 return df95 async def run(self):96 with tempfile.TemporaryDirectory() as tempdir:97 cmd_file = self._write_case(tempdir)98 proc = await asyncio.create_subprocess_shell(99 f"xfoil < {cmd_file}",100 cwd=tempdir,101 stdout=asyncio.subprocess.PIPE,102 stderr=asyncio.subprocess.PIPE)103 104 await proc.communicate()105 result_file = os.path.join(tempdir, self.file_result)106 self.result = self._read_result(result_file)107 return self.result108 109 async def run_aoa(self, aoa):110 self.aoa = [aoa - 0.1, aoa, aoa + 0.1]111 result = await self.run()...
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!!