Best Python code snippet using lisa_python
variable.py
Source:variable.py
...103 if variable:104 variable.update(new_variable)105 else:106 variables[name] = new_variable.copy()107def _get_undefined_variables(108 value: str, variables: Dict[str, VariableEntry]109) -> List[str]:110 undefined_variables: List[str] = []111 # check if there is variable or not in a value112 matches = _VARIABLE_PATTERN.findall(value)113 for variable_name in matches:114 lower_variable_name = variable_name[2:-1].lower()115 if lower_variable_name not in variables:116 undefined_variables.append(variable_name)117 return undefined_variables118def _load_from_env() -> Dict[str, VariableEntry]:119 results: Dict[str, VariableEntry] = {}120 for env_name in os.environ:121 is_lisa_variable = True122 is_secret = False123 name = ""124 if env_name.startswith(_ENV_START):125 name = env_name[len(_ENV_START) :]126 value = os.environ[env_name]127 elif env_name.startswith(_SECRET_ENV_START):128 name = env_name[len(_SECRET_ENV_START) :]129 is_secret = True130 else:131 is_lisa_variable = False132 if is_lisa_variable:133 value = os.environ[env_name]134 _add_variable(name, value, results, is_secret=is_secret)135 return results136def _load_from_runbook(137 runbook_data: Any, higher_level_variables: Dict[str, VariableEntry]138) -> Dict[str, VariableEntry]:139 # make a copy to prevent modifying existing dict140 current_variables = higher_level_variables.copy()141 if constants.VARIABLE in runbook_data:142 variable_entries: List[schema.Variable] = schema.load_by_type_many(143 schema.Variable, runbook_data[constants.VARIABLE]144 )145 left_variables = variable_entries.copy()146 undefined_variables: List[str] = []147 is_current_updated = True148 # when is_current_updated, it means one of variable is processed, and149 # it's ok to loop again. If it's false, there are some variables cannot150 # be resolved.151 while left_variables and is_current_updated:152 is_current_updated = False153 undefined_variables = []154 # solved variable will be removed later, so use a copy here to prevent155 # operate enumerating collection.156 for entry in left_variables.copy():157 # this value is used to detect whether value used undefined variables.158 # in final merge, the referred variable may be defined later than used.159 # So it should to load referred variables firstly.160 checked_value = f"{entry.file}{entry.value}"161 current_undefined_variables = _get_undefined_variables(162 checked_value, current_variables163 )164 if current_undefined_variables:165 undefined_variables.extend(current_undefined_variables)166 continue167 if entry.file:168 path = replace_variables(entry.file, current_variables)169 loaded_variables = _load_from_file(path, is_secret=entry.is_secret)170 else:171 value = replace_variables(entry.value, current_variables)172 loaded_variables = load_from_variable_entry(173 entry.name,174 value,175 is_case_visible=entry.is_case_visible,...
fixes.py
Source:fixes.py
...42 output = stdout.getvalue()43 for line in output.splitlines():44 if re_pattern.match(line):45 yield line46def _get_undefined_variables(content: str) -> Collection[str]:47 variables = set()48 for line in _find_pylint_errors(content, "undefined-variable"):49 try:50 _, *_, error_msg = _deconstruct_pylint_warning(line)51 _, variable_name, _ = error_msg.split("'")52 variables.add(variable_name)53 except ValueError:54 pass55 return variables56def _get_unused_imports(content: str) -> Iterable[Tuple[int, str, str]]:57 for line in _find_pylint_errors(content, "unused-import"):58 try:59 _, lineno, *_, message = _deconstruct_pylint_warning(line)60 if " from " not in message:61 *_, package = message.strip().split(" ")62 yield lineno, package, None63 else:64 _, variable, *_, package, _ = message.strip().split(" ")65 yield lineno, package, variable66 except ValueError:67 pass68def _is_uppercase_static_name(variable: str) -> bool:69 return re.match(r"^_[A-Z_]+$", variable) is not None70def _is_magic_variable(variable: str) -> bool:71 return re.match(r"^__[a-z_]+__$", variable) is not None72def _is_regular_variable(variable: str) -> bool:73 if variable.startswith("__"):74 return False75 if variable.endswith("_"):76 return False77 return re.match(r"^[a-z_]+$", variable) is not None78def _is_private(variable: str) -> bool:79 return variable.startswith("_")80def _rename_variable(variable: str, *, static: bool, private: bool) -> str:81 renamed_variable = variable.upper() if static else variable.lower()82 renamed_variable = re.sub("_{1,}", "_", renamed_variable)83 if renamed_variable.endswith("_"):84 renamed_variable = renamed_variable[:-1]85 if private and not _is_private(renamed_variable):86 renamed_variable = f"_{renamed_variable}"87 if not private and _is_private(renamed_variable):88 renamed_variable = renamed_variable.lstrip("_")89 if renamed_variable:90 return renamed_variable91 raise RuntimeError(f"Unable to find a replacement name for {variable}")92def _rename_class(name: str, *, private: bool) -> str:93 name = re.sub("_{1,}", "_", name)94 if len(name) == 0:95 raise ValueError("Cannot rename empty name")96 if len(name) == 1:97 name = name.upper()98 else:99 accum = (last := name[0].upper())100 for char in name[1:]:101 if last == "_":102 accum += (last := char.upper())103 else:104 accum += char105 last = char106 name = accum107 if private and not _is_private(name):108 return f"_{name}"109 if not private and _is_private(name):110 return name[1:]111 return name112def _substitute_name(113 variable: str, variable_type: parsing.VariableType, scope: Literal["class", "def", "enum", None]114) -> str:115 if variable == "_":116 return variable117 if variable_type == parsing.VariableType.CLASS:118 private = _is_private(variable)119 return _rename_class(variable, private=private)120 if variable_type == parsing.VariableType.CALLABLE:121 return _rename_variable(variable, static=False, private=_is_private(variable))122 if variable_type == parsing.VariableType.VARIABLE and scope not in {None, "enum"}:123 private = scope == "class" and _is_private(variable)124 return _rename_variable(variable, static=False, private=private)125 if variable_type == parsing.VariableType.VARIABLE:126 return _rename_variable(variable, static=True, private=_is_private(variable))127 raise NotImplementedError(f"Unknown variable type: {variable_type}")128def _get_variable_name_substitutions(content: str) -> Iterable[str]:129 for variable, scopes, variable_type in parsing.iter_definitions(content):130 substitute = _substitute_name(variable, variable_type, scopes[-1][0] if scopes else None)131 if variable != substitute and substitute not in parsing.PYTHON_KEYWORDS:132 print(f"{variable} should be named {substitute}")133 yield variable, substitute134def _get_variable_re_pattern(variable) -> str:135 return r"(?<![A-Za-z_\.])" + variable + r"(?![A-Za-z_])"136def _fix_variable_names(content: str, renamings: Iterable[Tuple[str, str]]) -> str:137 code_mask = parsing.get_is_code_mask(content)138 paranthesis_map = parsing.get_paren_depths(content, code_mask)139 for variable, substiture in renamings:140 replacements = []141 for match in re.finditer(_get_variable_re_pattern(variable), content):142 start = match.start()143 end = match.end()144 # Ignore string contents or comments145 if not all(code_mask[start:end]):146 continue147 is_in_paranthesis = max(paranthesis_map[start:end]) > 0148 if not is_in_paranthesis:149 replacements.append((start, end))150 continue151 # If inside paranthesis, a = means a keyword argument is being assigned,152 # which should be ignored.153 # The only valid assignment syntax is with the walrus operator :=154 substring = content[end : min(len(content) - 1, end + 3)]155 is_assigned_by_equals = re.match(parsing.ASSIGN_RE_PATTERN, substring) is not None156 if not is_assigned_by_equals:157 replacements.append((start, end))158 if not replacements:159 raise RuntimeError(f"Unable to find '{variable}' in content")160 for start, end in sorted(replacements, reverse=True):161 content = content[:start] + substiture + content[end:]162 return content163def _fix_undefined_variables(content: str, variables: Collection[str]) -> str:164 variables = set(variables)165 lines = content.splitlines()166 change_count = -len(lines)167 lineno = next(168 i169 for i, line in enumerate(lines)170 if not line.startswith("#")171 and not line.startswith("'''")172 and not line.startswith('"""')173 and not line.startswith("from __future__ import")174 )175 for package, package_variables in _ASSUMED_SOURCES.items():176 overlap = variables.intersection(package_variables)177 if overlap:178 fix = f"from {package} import " + ", ".join(sorted(overlap))179 print(f"Inserting '{fix}' at line {lineno}")180 lines.insert(lineno, fix)181 for package in _PACKAGE_SOURCES & variables:182 fix = f"import {package}"183 print(f"Inserting '{fix}' at line {lineno}")184 lines.insert(lineno, fix)185 for alias in _PACKAGE_ALIASES.keys() & variables:186 package = _PACKAGE_ALIASES[alias]187 fix = f"import {package} as {alias}"188 print(f"Inserting '{fix}' at line {lineno}")189 lines.insert(lineno, fix)190 change_count += len(lines)191 assert change_count >= 0192 if change_count == 0:193 return content194 return "\n".join(lines) + "\n"195def _fix_unused_imports(content: str, problems: Collection[Tuple[int, str, str]]) -> bool:196 lineno_problems = collections.defaultdict(set)197 for lineno, package, variable in problems:198 lineno_problems[int(lineno)].add((package, variable))199 change_count = 0200 new_lines = []201 for i, line in enumerate(content.splitlines(keepends=True)):202 if i + 1 in lineno_problems:203 if re.match(r"from .*? import .*", line):204 packages = {package for package, variable in lineno_problems[i + 1]}205 if len(packages) != 1:206 raise RuntimeError("Unable to parse unique package")207 bad_variables = {variable for package, variable in lineno_problems[i + 1]}208 _, existing_variables = line.split(" import ")209 existing_variables = set(x.strip() for x in existing_variables.split(","))210 keep_variables = existing_variables - bad_variables211 if keep_variables:212 fix = f"from {package} import " + ", ".join(sorted(keep_variables)) + "\n"213 new_lines.append(fix)214 print(f"Replacing {line.strip()} \nwith {fix.strip()}")215 change_count += 1216 continue217 print(f"Removing '{line.strip()}'")218 change_count += 1219 continue220 new_lines.append(line)221 assert change_count >= 0222 if change_count == 0:223 return content224 return "".join(new_lines)225def define_undefined_variables(content: str) -> str:226 """Attempt to find imports matching all undefined variables.227 Args:228 content (str): Python source code229 Returns:230 str: Source code with added imports231 """232 undefined_variables = _get_undefined_variables(content)233 if undefined_variables:234 return _fix_undefined_variables(content, undefined_variables)235 return content236def remove_unused_imports(content: str) -> str:237 """Remove unused imports from source code.238 Args:239 content (str): Python source code240 Returns:241 str: Source code, with added imports removed242 """243 unused_import_linenos = set(_get_unused_imports(content))244 if unused_import_linenos:245 return _fix_unused_imports(content, unused_import_linenos)246 return content...
trial.py
Source:trial.py
...72 def update_executed_at(self) -> None:73 self.executed_at = timezone.datetime.now()74 self.save()75 def get_undefined_variables(self) -> Set[str]:76 undefined_variables = self._get_undefined_variables(77 self.experiment.steps)78 undefined_variables.update(79 self._get_undefined_variables(self.experiment.pre_steps))80 undefined_variables.update(81 self._get_undefined_variables(self.experiment.post_steps))82 return undefined_variables83 def _get_undefined_variables(self, steps):84 undefined_variables = set()85 for step in steps:86 variables = step.get_where_clause_template_variables()87 for variable in variables:88 if variable not in self.get_populated_parameters():89 undefined_variables.add(variable)90 return undefined_variables91 def get_populated_parameters(self):92 experiment_parameters = self.experiment.parameters93 if experiment_parameters:94 experiment_parameters.update(self.parameters)95 return experiment_parameters96 else:97 return self.parameters...
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!!