Best Python code snippet using refurb_python
gen.py
Source:gen.py
...46 fzf_error_codes = (2, 130)47 if process.returncode in fzf_error_codes:48 sys.exit(1)49 return process.stdout[:-1].decode()50def folders_needing_init_file(path: Path) -> list[Path]:51 path = path.resolve()52 cwd = Path.cwd().resolve()53 if path.is_relative_to(cwd):54 to_remove = len(cwd.parents) + 155 return [path, *list(path.parents)[:-to_remove]]56 return []57def get_next_error_id(prefix: str) -> int:58 highest = 059 for module in get_modules([]):60 if error := get_error_class(module):61 error_code = ErrorCode.from_error(error)62 if error_code.prefix == prefix:63 highest = max(highest, error_code.id + 1)64 return highest65NODES: dict[str, type] = {x.__name__: x for x in METHOD_NODE_MAPPINGS.values()}66def node_type_prompt() -> list[str]:67 return sorted(68 fzf(69 list(NODES.keys()), args=["--prompt", "type> ", "--multi"]70 ).splitlines()71 )72def filename_prompt() -> Path:73 return Path(74 fzf(75 None,76 args=[77 "--prompt",78 "filename> ",79 "--print-query",80 "--query",81 "refurb/checks/",82 ],83 ).splitlines()[0]84 )85def prefix_prompt() -> str:86 return fzf(87 [""], args=["--prompt", "prefix> ", "--print-query", "--query", "FURB"]88 )89def build_imports(names: list[str]) -> str:90 modules: defaultdict[str, list[str]] = defaultdict(list)91 for name in names:92 modules[NODES[name].__module__].append(name)93 return "\n".join(94 f"from {module} import {', '.join(names)}"95 for module, names in sorted(modules.items(), key=lambda x: x[0])96 )97def main() -> None:98 selected = node_type_prompt()99 file = filename_prompt()100 if file.suffix != ".py":101 print('refurb: File must end in ".py"')102 sys.exit(1)103 prefix = prefix_prompt()104 template = FILE_TEMPLATE.format(105 accept_type=" | ".join(selected),106 imports=build_imports(selected),107 prefix=prefix,108 id=get_next_error_id(prefix) or 100,109 pattern=" | ".join(f"{x}()" for x in selected),110 )111 with suppress(FileExistsError):112 file.parent.mkdir(parents=True, exist_ok=True)113 for folder in folders_needing_init_file(file.parent):114 (folder / "__init__.py").touch(exist_ok=True)115 file.write_text(template, "utf8")116 print(f"Generated {file}")117if __name__ == "__main__":...
test_gen.py
Source:test_gen.py
2from unittest.mock import patch3from refurb.gen import folders_needing_init_file4def test_folder_not_in_cwd_is_ignored():5 with patch("pathlib.Path.cwd", lambda: Path("/some/random/path")):6 assert folders_needing_init_file(Path("./some/path")) == []7def test_relative_path_works():8 assert folders_needing_init_file(Path("./a/b/c")) == [9 Path.cwd() / "a" / "b" / "c",10 Path.cwd() / "a" / "b",11 Path.cwd() / "a",12 ]13def test_absolute_path_works():14 assert folders_needing_init_file(Path.cwd() / "a" / "b" / "c" / "d") == [15 Path.cwd() / "a" / "b" / "c" / "d",16 Path.cwd() / "a" / "b" / "c",17 Path.cwd() / "a" / "b",18 Path.cwd() / "a",...
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!!