Best Python code snippet using autotest_python
main.py
Source:main.py  
...80        self.enemy_1 = Enemy(self.empty_path, (825+1, 75))81        self.enemy_2 = Enemy(self.empty_path, (75+1, 825))82        self.enemy_3 = Enemy(self.empty_path, (825+1, 825))83        self.enemy_group.add(self.enemy_1, self.enemy_2, self.enemy_3)84    def empty_path(self):85        self.path = []86    def draw_active_cell(self):87        mouse_pos = pygame.mouse.get_pos()88        row =  mouse_pos[1] // 3089        col =  mouse_pos[0] // 3090        current_cell_value = self.matrix[row][col]91        if current_cell_value == 1:92            rect_1 = pygame.Rect((col * 30,row * 30),(30,30))93            rect_2 = pygame.Rect((col * 30+2,row * 30+2),(26,26))94            screen.blit(self.select_surf_1,rect_1)95            screen.blit(self.select_surf_2,rect_2)96    def create_path(self): # 第ä¸ä¸ªåè½, æä¸é¼ æ åå¼å§æ§è¡97        # start98        start_x, start_y = self.player.sprite.get_coord() # è·å¾playerä½ç½®å¯¹åºå¤çç©éµç´¢å¼99        start = self.grid.node(start_x,start_y)100        # end101        mouse_pos = pygame.mouse.get_pos()102        end_x,end_y =  mouse_pos[0] // 30, mouse_pos[1] // 30   # åä¸é¢è·å¾playerä½ç½®çå¸¦é©¬åºæ¬ç¸å103        end = self.grid.node(end_x,end_y) 104        # path105        if start_x != end_x or start_y != end_y:106            finder = AStarFinder(diagonal_movement = DiagonalMovement.always)107            self.path,_ = finder.find_path(start,end,self.grid)108            self.grid.cleanup()109            self.player.sprite.set_path(self.path)  # å°çæçpathä¼ å
¥player110    def create_path_enemy(self): # 第ä¸ä¸ªåè½, æä¸é¼ æ åå¼å§æ§è¡111        print(self.enemy_group.sprites())112        for enemy in self.enemy_group.sprites():113            # start114            start_x, start_y = enemy.get_coord() # è·å¾playerä½ç½®å¯¹åºå¤çç©éµç´¢å¼115            start = self.grid.node(start_x,start_y)116            # end117            # mouse_pos = pygame.mouse.get_pos()118            # debug(self.rect.x, info_name='player.rect.x', y =30)119            # end_x, end_y = self.player.pos[0]//30, self.player.pos[1]//30120            end_x, end_y = self.player.sprite.get_coord()121            # end_x,end_y =  mouse_pos[0] // 30, mouse_pos[1] // 30   # åä¸é¢è·å¾playerä½ç½®çå¸¦é©¬åºæ¬ç¸å122            end = self.grid.node(end_x,end_y) 123            # path124            if start_x != end_x or start_y != end_y:125                finder = AStarFinder(diagonal_movement = DiagonalMovement.always)126                self.path_enemy,_ = finder.find_path(start,end,self.grid)127                self.grid.cleanup()128                enemy.set_path(self.path_enemy)  # å°çæçpathä¼ å
¥enemy129    def update(self, dt):130        self.draw_active_cell()131        # player updating and drawing132        self.player.update(dt)133        self.player.draw(screen)134        self.enemy_group.update(dt)135        self.enemy_group.draw(screen)136class player(pygame.sprite.Sprite):137    def __init__(self,empty_path):138        # basic139        super().__init__()140        self.image = pygame.Surface((20, 20))141        self.image.fill(BLUE)142        self.rect = self.image.get_rect(center = (46,45))143        # movement 144        self.pos = self.rect.center145        self.speed = 400146        self.direction = pygame.math.Vector2(0,0)147        self.old_rect = self.rect.copy()148        # path149        self.path = []150        self.collision_rects = []151        self.empty_path = empty_path152    def get_coord(self):153        col = self.rect.centerx // 30154        row = self.rect.centery // 30155        return (col,row)156    def set_path(self,path):157        self.path = path158        self.create_collision_rects()159        self.get_direction()160    def create_collision_rects(self): # 卿¯ä¸ªè½¬æç¹å¤å建ä¸ä¸ª161        if self.path:162            self.collision_rects = []163            for point in self.path: # æ¤å¤åèä¸é¢çdraw_pathæ¹æ³164                x = (point[0] * 30) + 15165                y = (point[1] * 30) + 15166                rect = pygame.Rect((x - 2,y - 2),(4,4))167                self.collision_rects.append(rect) # rectæ¯pygameä¸é常éè¦çä¸ä¸ªæ¦å¿µ, é¦å
æ¯ä¸ªsurface...?168    def get_direction(self):169        if self.collision_rects:170            start = pygame.math.Vector2(self.pos)171            end = pygame.math.Vector2(self.collision_rects[0].center)172            self.direction = (end - start).normalize()173        else:174            self.direction = pygame.math.Vector2(0,0)175            self.path = []176    def check_collisions(self):177        if self.collision_rects:178            for rect in self.collision_rects:179                if rect.collidepoint(self.pos):180                    del self.collision_rects[0]181                    self.get_direction()182        else:183            self.empty_path()184    def reposition_player(self):185        if self.path == []:186            row =  self.pos[1] // 30187            col =  self.pos[0] // 30188            self.rect.center = (col*30+15, row*30+15)189        else:190            # print('not repositioned')191            self.rect.center = self.pos192    def draw_path(self):193        if self.path:194            points = []195            for point in self.path:196                x = (point[0] * 30) + 15197                y = (point[1] * 30) + 15198                points.append((x,y))199                pygame.draw.circle(screen, BLUE,(x, y), 5)200            pygame.draw.lines(screen, BLUE,False,points,5)201    def update(self, dt):202        self.pos += self.direction * self.speed * dt203        self.check_collisions()204        self.reposition_player()205        self.draw_path()206        # debug(self.rect.x, info_name='player.rect.x', y =30)207        # debug(self.path, y = 50, info_name='self.path')208        self.old_rect = self.rect.copy() 209class Enemy(pygame.sprite.Sprite):210    def __init__(self,empty_path, enemy_start_position):211        # basic212        super().__init__()213        self.image = pygame.Surface((20, 20))214        self.image.fill(RED)215        self.rect = self.image.get_rect(center = enemy_start_position)216        # movement217        self.pos = self.rect.center218        self.speed = 350219        self.direction = pygame.math.Vector2(0,0)220        self.old_rect = self.rect.copy()221        # path 222        self.path = []223        self.collision_rects = []224        self.empty_path = empty_path225    def get_coord(self):226        col = self.rect.centerx // 30227        row = self.rect.centery // 30228        return (col, row)229    def set_path(self, path):230        self.path = path231        self.create_collsion_rects()232        self.get_direction()233    def create_collsion_rects(self):234        if self.path:235            self.collision_rects = []236            for points in self.path:237                x = (points[0]*30)+15238                y = (points[1]*30)+15239                rect = pygame.Rect((x-2, y-2), (4,4))240                self.collision_rects.append(rect)241    def get_direction(self):242        if self.collision_rects:243            start = pygame.math.Vector2(self.pos)244            end = pygame.math.Vector2(self.collision_rects[0].center)245            self.direction = (end - start).normalize()246        else:247            self.direction = pygame.math.Vector2(0,0)248            self.path = []249    def check_collisions(self):250        if self.collision_rects:251            for rect in self.collision_rects:252                if rect.collidepoint(self.pos):253                    del self.collision_rects[0]254                    self.get_direction()255        else:256            self.empty_path()257    def update(self, dt):258        self.pos += self.direction * self.speed * dt259        self.check_collisions()260        self.rect.center = self.pos261        self.old_rect = self.rect.copy() 262# functions ------------------------------------------------------------------------------------------------- #263def draw_background(matrix, white_surf, black_surf):264    for row in range(len(matrix)):265        for col in range(len(matrix[row])):266            if matrix[row][col] == 0:  # ä¸å¯ä»¥éè¿, æ¶é»267                screen.blit(black_surf, (col*30, row*30))268            if matrix[row][col] != 0:  # å¯ä»¥éè¿, æ¶ç½269                screen.blit(white_surf, (col*30, row*30))270# main ------------------------------------------------------------------------------------------------------ #...lib_programname.py
Source:lib_programname.py  
...73    """74    if not hasattr(sys.modules["__main__"], "__file__"):75        return empty_path76    arg_string = str(sys.modules["__main__"].__file__)77    valid_executable_path = get_valid_executable_path_or_empty_path(arg_string)78    return valid_executable_path79def get_fullpath_from_sys_argv() -> pathlib.Path:80    """try to get it from sys_argv - does not work when loaded from uwsgi, works in eclipse and pydev81    >>> # force test invalid sys.path82    >>> save_sys_argv = list(sys.argv)83    >>> invalid_path = str((pathlib.Path(__file__).parent / 'invalid_file.py'))  # .resolve() seems not to work on a non existing file in python 3.584    >>> sys.argv = [invalid_path]85    >>> assert get_fullpath_from_sys_argv() == pathlib.Path()86    >>> sys.argv = list(save_sys_argv)87    >>> # force test valid sys.path88    >>> save_sys_path = list(sys.argv)89    >>> valid_path = str((pathlib.Path(__file__).resolve()))90    >>> sys.argv = [valid_path]91    >>> assert get_fullpath_from_sys_argv() == pathlib.Path(valid_path)92    >>> sys.argv = list(save_sys_argv)93    """94    for arg_string in sys.argv:95        valid_executable_path = get_valid_executable_path_or_empty_path(arg_string)96        if valid_executable_path != empty_path:97            return valid_executable_path98    return empty_path99def get_valid_executable_path_or_empty_path(arg_string: str) -> pathlib.Path:100    arg_string = remove_doctest_and_docrunner_parameters(arg_string)101    arg_string = add_python_extension_if_not_there(arg_string)102    path = pathlib.Path(arg_string)103    if path.is_file():104        path = path.resolve()  # .resolve does not work on a non existing file in python 3.5105        return path106    else:107        return empty_path108def remove_doctest_and_docrunner_parameters(arg_string: str) -> str:109    """110    >>> # Setup111    >>> arg_string_with_parameter = __file__ + '::::::some docrunner parameter'112    >>> arg_string_without_parameter = __file__113    >>> # Test with and without docrunner parameters...test_filesystem.py
Source:test_filesystem.py  
...12path2 = fs.path("dir 3")13platform_system = platform.system()14empty_path = fs.path()15empty_path.clear()16def test00_empty_path():17    assert empty_path.empty()18    assert empty_path.is_relative()19    assert empty_path.parent_path() == fs.path("..")20    assert not path_here_relative.empty()21def test01_path_status():22    assert not fs.exists(fs.path("some random" + sep + "path"))23    assert fs.is_directory(path_here_relative)24    assert fs.is_directory(path_here_relative)25    assert not fs.is_regular_file(path_here_relative)26    assert fs.is_directory(path_here)27    assert fs.is_directory(path_here)28    assert not fs.is_regular_file(path_here)29def test02_path_to_string():30    assert path1.__str__() == \...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!!
