Best Python code snippet using Kiwi_python
unittest_checker_base.py
Source:unittest_checker_base.py
...146 """)147 with self.assertAddsMessages(Message('invalid-name', node=classes[0], args=('class', 'classb', ''))):148 for cls in classes:149 self.checker.visit_class(cls)150 self.checker.leave_module(cls.root)151 @set_config(class_rgx=MULTI_STYLE_RE)152 def test_multi_name_detection_first_invalid(self):153 classes = test_utils.extract_node("""154 class class_a(object): #@155 pass156 class classb(object): #@157 pass158 class CLASSC(object): #@159 pass160 """)161 with self.assertAddsMessages(Message('invalid-name', node=classes[0], args=('class', 'class_a', '')),162 Message('invalid-name', node=classes[2], args=('class', 'CLASSC', ''))):163 for cls in classes:164 self.checker.visit_class(cls)165 self.checker.leave_module(cls.root)166 @set_config(method_rgx=MULTI_STYLE_RE,167 function_rgx=MULTI_STYLE_RE,168 name_group=('function:method',))169 def test_multi_name_detection_group(self):170 function_defs = test_utils.extract_node("""171 class First(object):172 def func(self): #@173 pass174 def FUNC(): #@175 pass176 """, module_name='test')177 with self.assertAddsMessages(Message('invalid-name', node=function_defs[1], args=('function', 'FUNC', ''))):178 for func in function_defs:179 self.checker.visit_function(func)180 self.checker.leave_module(func.root)181 @set_config(function_rgx=re.compile('(?:(?P<ignore>FOO)|(?P<UP>[A-Z]+)|(?P<down>[a-z]+))$'))182 def test_multi_name_detection_exempt(self):183 function_defs = test_utils.extract_node("""184 def FOO(): #@185 pass186 def lower(): #@187 pass188 def FOO(): #@189 pass190 def UPPER(): #@191 pass192 """)193 with self.assertAddsMessages(Message('invalid-name', node=function_defs[3], args=('function', 'UPPER', ''))):194 for func in function_defs:195 self.checker.visit_function(func)196 self.checker.leave_module(func.root)197if __name__ == '__main__':...
functioncoverage.py
Source:functioncoverage.py
1# flake8: noqa2# type: ignore3"""Instrument an application for function coverage using libCST."""4from typing import List5from typing import Optional6from typing import Tuple7import libcst as cst8from discover_test_coverage import constants9from discover_test_coverage import output10from discover_test_coverage import transform11class FunctionCoverageTransformer(cst.CSTTransformer):12 """Transform program source code to collect fortified function coverage."""13 def __init__(self): # noqa14 # stack for storing the canonical name of the current function15 self.stack: List[Tuple[str, ...]] = []16 # construct a fully qualified name of the TestFixtureTransformer17 self.name = str(18 self.__module__ + constants.markers.Dot + type(self).__qualname__19 )20 def visit_ClassDef(self, node: cst.ClassDef) -> Optional[bool]: # noqa21 self.stack.append(node.name.value) # type: ignore22 def leave_ClassDef(23 self, original_node: cst.ClassDef, updated_node: cst.ClassDef # noqa24 ) -> cst.CSTNode: # noqa25 self.stack.pop()26 return updated_node27 def visit_FunctionDef(self, node: cst.FunctionDef) -> Optional[bool]: # noqa28 self.stack.append(node.name.value) # type:ignore29 return False30 def leave_FunctionDef(31 self, original_node: cst.FunctionDef, updated_node: cst.FunctionDef32 ) -> cst.CSTNode: # noqa33 # key = tuple(self.stack)34 self.stack.pop()35 output.logger.debug(36 f"current function definition node's name: {original_node.name}"37 )38 output.logger.debug(f"contents of the stack after the pop() call: {self.stack}")39 return updated_node.with_changes(decorators=[cst.Decorator(cst.Name("sample"))])40 def leave_Module(41 self, original_node: cst.Module, updated_node: cst.Module42 ) -> cst.CSTNode: # noqa43 output.logger.debug("start --->")44 output.logger.debug(f"current module node's header: {original_node.header}")45 output.logger.debug("---> end")46 # TODO: Add in correct location and add correct import to a47 # module that exists inside of the fortify-coverage package48 updated_node = updated_node.with_changes(49 header=(50 cst.parse_statement(51 "from fortify import sample # leave_Module",52 config=transform.source_tree_configuration,53 ),54 *updated_node.header,55 )56 )...
clean_imports.py
Source:clean_imports.py
1import libcst as cst2from isort import SortImports3from .base_pass import BasePass4class CleanImportsPass(BasePass):5 """6 Puts all imports at the top of the module and de-duplicates them.7 Example:8 x = 19 import foo10 import numpy11 y = foo.bar() + x12 >> becomes >>13 import foo14 x = 115 y = foo.bar() + x16 """17 def __init__(self):18 super().__init__()19 self.imports = []20 def leave_Import(self, original_node, updated_node):21 self.imports.append(original_node)22 return cst.RemoveFromParent()23 def leave_ImportFrom(self, original_node, updated_node):24 self.imports.append(original_node)25 return cst.RemoveFromParent()26 def leave_Module(self, original_node, updated_node):27 final_node = super().leave_Module(original_node, updated_node)28 imports_str = cst.Module(29 body=[cst.SimpleStatementLine([i]) for i in self.imports]).code30 sorted_imports = cst.parse_module(31 SortImports(file_contents=imports_str).output)32 # Add imports back to the top of the module33 new_body = sorted_imports.body + list(final_node.body)...
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!!