Best Python code snippet using ATX
piece.py
Source:piece.py  
1from copy import copy2import client.globals as g3from common.enums import *4class Piece:5    def __init__(self, piece_type, player_number, spawn_column, player_count):6        self.piece_type = 07        self.tile_type = 08        self.rotation = 09        self.locations = [None, None, None, None]  # col,row10        self.piece_type = piece_type11        self.player_number = player_number12        if self.piece_type == PieceType.I:13            self.locations[0] = (spawn_column - 2, 2)  # [-][-][-][-] | [-][-][0][-]14            self.locations[1] = (spawn_column - 1, 2)  # [-][-][-][-] | [-][-][1][-]15            self.locations[2] = (spawn_column, 2)      # [0][1][2][3] | [-][-][2][-]16            self.locations[3] = (spawn_column + 1, 2)  # [-][-][-][-] | [-][-][3][-]17            self.tile_type = TileType.IOT.value18        elif self.piece_type == PieceType.O:19            self.locations[0] = (spawn_column - 1, 2)  #20            self.locations[1] = (spawn_column, 2)      # [0][1]21            self.locations[2] = (spawn_column - 1, 3)  # [2][3]22            self.locations[3] = (spawn_column, 3)      #23            self.tile_type = TileType.IOT.value24        elif self.piece_type == PieceType.T:25            self.locations[0] = (spawn_column - 1, 2)  # [-][-][-] | [-][0][-] | [-][3][-] | [-][2][-]26            self.locations[1] = (spawn_column, 2)      # [0][1][2] | [3][1][-] | [2][1][0] | [-][1][3]27            self.locations[2] = (spawn_column + 1, 2)  # [-][3][-] | [-][2][-] | [-][-][-] | [-][0][-]28            self.locations[3] = (spawn_column, 3)      # |           |           |29            self.tile_type = TileType.IOT.value30        elif self.piece_type == PieceType.L:31            self.locations[0] = (spawn_column - 1, 2)  # [-][-][-] | [3][0][-] | [-][-][3] | [-][2][-]32            self.locations[1] = (spawn_column, 2)      # [0][1][2] | [-][1][-] | [2][1][0] | [-][1][-]33            self.locations[2] = (spawn_column + 1, 2)  # [3][-][-] | [-][2][-] | [-][-][-] | [-][0][3]34            self.locations[3] = (spawn_column - 1, 3)  # |           |           |35            self.tile_type = TileType.LZ.value36        elif self.piece_type == PieceType.J:37            self.locations[0] = (spawn_column - 1, 2)  # [-][-][-] | [-][0][-] | [3][-][-] | [-][2][3]38            self.locations[1] = (spawn_column, 2)      # [0][1][2] | [-][1][-] | [2][1][0] | [-][1][-]39            self.locations[2] = (spawn_column + 1, 2)  # [-][-][3] | [3][2][-] | [-][-][-] | [-][0][-]40            self.locations[3] = (spawn_column + 1, 3)  # |           |           |41            self.tile_type = TileType.JS.value42        elif self.piece_type == PieceType.Z:43            self.locations[0] = (spawn_column - 1, 2)  # [-][-][-] | [-][-][3]44            self.locations[1] = (spawn_column, 2)      # [0][1][-] | [-][1][2]45            self.locations[2] = (spawn_column, 3)      # [-][2][3] | [-][0][-]46            self.locations[3] = (spawn_column + 1, 3)  # |47            self.tile_type = TileType.LZ.value48        elif self.piece_type == PieceType.S:49            self.locations[0] = (spawn_column, 2)      # [-][-][-] | [-][1][-]50            self.locations[1] = (spawn_column + 1, 2)  # [-][0][1] | [-][0][3]51            self.locations[2] = (spawn_column - 1, 3)  # [2][3][-] | [-][-][2]52            self.locations[3] = (spawn_column, 3)      # |53            self.tile_type = TileType.JS.value54        if player_count != 1:55            self.tile_type = player_number56    def move(self, direction=Direction.DOWN, locations=None):57        if locations is None:58            locations = self.locations59        if direction == Direction.DOWN:60            for index, location in enumerate(locations):61                locations[index] = (location[0], location[1] + 1)62        elif direction == Direction.LEFT:63            for index, location in enumerate(locations):64                locations[index] = (location[0] - 1, location[1])65        elif direction == Direction.RIGHT:66            for index, location in enumerate(locations):67                locations[index] = (location[0] + 1, location[1])68    # Direction == None for no direction (spawning)69    def can_move(self, board, players, direction):70        test_locations = copy(self.locations)71        # If not spawning piece72        if direction is not None:73            self.move(direction, test_locations)74        for location in test_locations:75            if location[1] >= len(board) \76                    or location[0] < 0 \77                    or location[0] >= len(board[0]):78                return MoveAllowance.CANT_BOARD79            if direction is not None:80                if board[location[1]][location[0]].tile_type != TileType.BLANK:81                    return MoveAllowance.CANT_BOARD82            for player in players:83                if player.active_piece is not None:84                    if player.active_piece.player_number != self.player_number:85                        for other_location in player.active_piece.locations:86                            if other_location == location:87                                return MoveAllowance.CANT_PIECE88        return MoveAllowance.CAN89    def rotate(self, rotation_direction, locations=None, rotation=None):90        save_rotation = False91        if locations is None:92            locations = self.locations93        if rotation is None:94            save_rotation = True95            rotation = self.rotation96        new_rotation = rotation97        if self.piece_type == PieceType.O:  # for the meme98            pass99        elif self.piece_type == PieceType.I or self.piece_type == PieceType.S or self.piece_type == PieceType.Z:  # the two-rotation-position pieces100            turn = None101            pivot = None102            if self.piece_type == PieceType.I:103                pivot = locations[2]104                if rotation in [0, 180]:105                    turn = Turn.CW  # turn to vertical106                else:107                    turn = Turn.CCW  # turn to horizontal108            elif self.piece_type == PieceType.S:109                pivot = copy(locations[0])110                if rotation in [0, 180]:111                    turn = Turn.CCW  # turn to vertical112                else:113                    turn = Turn.CW  # turn to horizontal114            elif self.piece_type == PieceType.Z:115                pivot = copy(locations[1])116                if rotation in [0, 180]:117                    turn = Turn.CCW  # turn to vertical118                else:119                    turn = Turn.CW  # turn to horizontal120            if turn == Turn.CW:121                # General rotate CW:122                for i, location in enumerate(locations):123                    locations[i] = ((pivot[1] - location[1]) + pivot[0], (location[0] - pivot[0]) + pivot[1])124                new_rotation = (rotation + 90) % 360125            elif turn == Turn.CCW:126                # General rotate CCW:127                for i, location in enumerate(locations):128                    locations[i] = ((location[1] - pivot[1]) + pivot[0], (pivot[0] - location[0]) + pivot[1])129                new_rotation = (rotation - 90) % 360130        elif self.piece_type == PieceType.T or self.piece_type == PieceType.L or self.piece_type == PieceType.J:  # the four-rotation-position pieces131            pivot = copy(locations[1])132            if rotation_direction == Rotation.CW:133                # General rotate CW:134                for i, location in enumerate(locations):135                    locations[i] = ((pivot[1] - location[1]) + pivot[0], (location[0] - pivot[0]) + pivot[1])136                new_rotation = (rotation + 90) % 360137            elif rotation_direction == Rotation.CCW:138                # General rotate CCW:139                for i, location in enumerate(locations):140                    locations[i] = ((location[1] - pivot[1]) + pivot[0], (pivot[0] - location[0]) + pivot[1])141                new_rotation = (rotation - 90) % 360142        if save_rotation:143            self.rotation = new_rotation144    def can_rotate(self, board, players, rotation_direction):145        test_locations = copy(self.locations)146        test_rotation = copy(self.rotation)147        self.rotate(rotation_direction, test_locations, test_rotation)148        for location in test_locations:149            if location[1] >= len(board) or location[0] < 0 or location[0] >= len(board[0]):150                return False151            if board[location[1]][location[0]].tile_type != TileType.BLANK:152                return False153            for player in players:154                if player.active_piece is not None:155                    if player.active_piece.player_number != self.player_number:156                        for other_location in player.active_piece.locations:157                            if other_location == location:158                                return False...rotateimage_gentxt_yform.py
Source:rotateimage_gentxt_yform.py  
...6import cv27import os8import imutils9import numpy as np10def save_rotation(path, filename) :11    image = cv2.imread(path+filename, cv2.IMREAD_COLOR)12    13    #90 degree clockwise rotation14    image = imutils.rotate_bound(image, 90)15    cv2.imwrite(path+'90_{}'.format(filename), image)16    17    #rotate one more(180 degree)18    image = imutils.rotate_bound(image, 90)19    cv2.imwrite(path+'180_{}'.format(filename), image)20    #rotate one more(270 degree = 90 degree towards counter-clockwise)21    image = imutils.rotate_bound(image, 90)22    cv2.imwrite(path+'270_{}'.format(filename), image) 23def save_rotated_annotation(txt_path, filename) :24    ori_txt = open(txt_path+filename, 'r')25    txt_90 = open(txt_path+'90_{}'.format(filename), 'w')26    txt_180 = open(txt_path+'180_{}'.format(filename), 'w')27    txt_270 = open(txt_path+'270_{}'.format(filename), 'w') 28    29    for line in ori_txt :        30        line = line.split()31        dist = line[0]        32        x = float(line[1])33        y = float(line[2])34        w = float(line[3])35        h = float(line[4])36        txt_90.write('{} {} {} {} {}\n'.format(dist, 1-y, x, h, w))37        txt_180.write('{} {} {} {} {}\n'.format(dist, 1-x, 1-y, w, h))38        txt_270.write('{} {} {} {} {}\n'.format(dist, y, 1-x, h, w))39        40    ori_txt.close()41    txt_90.close()42    txt_180.close()43    txt_270.close()44reg_path = './reg_yolo_annotation/'45file_list = os.listdir(reg_path)46file_list = [ file for file in file_list if file.endswith('.txt')]47file_list.sort()48#49#print('calculating max value...')50#max_value = 0.51#for file in file_list :52#    a = open(reg_path+file, 'r')53#    54#    for line in a :55#        line = line.split()56#        dist = float(line[0])57#        max_value = np.maximum(max_value, dist)58#    a.close()59#print("max value is {}".format(max_value))60#61#62#print('saving rotated annotations...')63#for file in file_list :64#    save_rotated_annotation(reg_path, file)65#print('completed saving rotated annotation')66#67#68#print("reg to class converting...")69#clas_path = './clas_yolo_annotation/'70#for file in file_list :    71#    reg = open(reg_path + file, 'r')72#    clas = open(clas_path + file, 'w')73#    lines = reg.readlines()74#    for j in range(len(lines)) :75#        line = lines[j]76#        line = line.split()77#        line[0] = round(float(line[0]))78#        clas.write("{} {} {} {} {}\n".format(line[0], line[1], line[2], line[3], line[4]))        79#    clas.close()80#    reg.close()       81#print('completed converting reg to class')82#    83    84    85print('saving rotated images...')86path = './image/'87file_list = os.listdir(path)88img_files = [file for file in file_list if file.endswith('.png')]89img_files.sort()90for image in img_files :91    save_rotation(path, image)92print('completed saving rotated images')...generate_augmented_images.py
Source:generate_augmented_images.py  
1import numpy as np2import math3import glob4import random5import os, sys, stat6from save_inDir import * 7import transformations8#generate and save aumentation in separated folders9def generate_augmentation(filename, dir_save_fig):10	filename2 = sorted(glob.glob(os.path.join(filename, '*','*')))11	save_crop = '/crop/'12	if not os.path.isdir(dir_save_fig+save_crop):13		os.mkdir(dir_save_fig+save_crop)14	save_rotation = '/rotated/'15	if not os.path.isdir(dir_save_fig+save_rotation):16		os.mkdir(dir_save_fig+save_rotation)17	save_zoom = '/zoom/'18	if not os.path.isdir(dir_save_fig+save_zoom):19		os.mkdir(dir_save_fig+save_zoom)20	for i in range(5):21		random.seed(a=i)22		transformations.crop_generator(filename2, 224,dir_save_fig+save_crop)23		transformations.zoom_generator(filename2, 5.0,dir_save_fig+save_zoom)24		transformations.rotation_generator(filename2, 30,dir_save_fig+save_rotation)25##########################MAIN################################26filename = '../dataset/'27dataset = 'museu_nacional' #'wedding', 'fire', 'bombing' or 'bangladesh_fire'28n = 'train' #or val29name = os.path.join(filename, dataset,n)30df_paths = pd.read_csv(name+'.csv', header=0)...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!!
