Best Python code snippet using pandera_python
candidate.py
Source:candidate.py
1import numpy2import random3Nd = 9 # number of digits4class Candidate(object):5 def __init__(self):6 self.values = numpy.zeros((Nd, Nd), dtype=int) # Set the 9x9 Sudoku into 0s7 self.fitness = None8 return9 def update_fitness(self):10 row_count = numpy.zeros(Nd,dtype=int)11 column_count = numpy.zeros(Nd,dtype=int)12 block_count = numpy.zeros(Nd,dtype=int)13 row_sum = 014 column_sum = 015 block_sum = 016 17 print('--------------Row Sum--------------------')18 for i in range(0, Nd): # For each row...19 for j in range(0, Nd): # For each number within it...20 # ...Update list with occurrence of a particular number.21 row_count[self.values[i][j]-1] += 122 row_sum += (1.0/len(set(row_count)))/Nd23 print(f'{row_sum} += (1.0/{len(set(row_count))})/{Nd}')24 row_count = numpy.zeros(Nd)25 26 print('-------------Column Sum------------------')27 for i in range(0, Nd): # For each column...28 for j in range(0, Nd): # For each number within it...29 # ...Update list with occurrence of a particular number.30 column_count[self.values[j][i]-1] += 131 column_sum += (1.0 / len(set(column_count)))/Nd32 print(f'{column_sum} += (1.0/{len(set(column_count))})/{Nd}')33 column_count = numpy.zeros(Nd)34 35 print('-------------Block Sum--------------------')36 # For each block...37 for i in range(0, Nd, 3):38 for j in range(0, Nd, 3):39 block_count[self.values[i][j]-1] += 140 block_count[self.values[i][j+1]-1] += 141 block_count[self.values[i][j+2]-1] += 142 block_count[self.values[i+1][j]-1] += 143 block_count[self.values[i+1][j+1]-1] += 144 block_count[self.values[i+1][j+2]-1] += 145 block_count[self.values[i+2][j]-1] += 146 block_count[self.values[i+2][j+1]-1] += 147 block_count[self.values[i+2][j+2]-1] += 148 block_sum += (1.0/len(set(block_count)))/Nd49 print(f'{block_sum} += (1.0/{len(set(block_count))})/{Nd}')50 block_count = numpy.zeros(Nd)51 52 print('------------Overall Fitness--------------------')53 # Calculate overall fitness.54 if (int(row_sum) == 1 and int(column_sum) == 1 and int(block_sum) == 1):55 fitness = 1.056 print(f'Row Sum = {row_sum} Column Sum = {column_sum} Block Sum = {block_sum} Fitness = {fitness}')57 else:58 fitness = column_sum * block_sum59 print('Fitness =\t\t Column Sum\t * Block Sum')60 print(f'{fitness} = {column_sum} * {block_sum}')61 self.fitness = fitness62 return63 def mutate(self, mutation_rate, given):64 """ Mutate a candidate by picking a row, and then picking two values within that row to swap. """65 r = random.uniform(0, 1.1)66 while(r > 1): # Outside [0, 1] boundary - choose another67 r = random.uniform(0, 1.1)68 print(f'r = {r}')69 70 success = False71 if (r < mutation_rate): # Mutate.72 while(not success):73 row1 = random.randint(0, 8)74 row2 = random.randint(0, 8)75 row2 = row176 print(f'row1 = {row1} row2 = {row2}')77 78 from_column = random.randint(0, 8)79 to_column = random.randint(0, 8)80 while(from_column == to_column):81 from_column = random.randint(0, 8)82 to_column = random.randint(0, 8) 83 # Check if the two places are free...84 if(given[row1][from_column] == 0 and given[row1][to_column] == 0):85 # ...and that we are not causing a duplicate in the rows' columns.86 if self.is_column_duplicate(to_column, self.values[row1][from_column]): return87 if self.is_column_duplicate(from_column, self.values[row2][to_column]): return88 if self.is_block_duplicate(row2, to_column, self.values[row1][from_column]): return89 if self.is_block_duplicate(row1, from_column, self.values[row2][to_column]): return90 # Swap values.91 temp = self.values[row2][to_column]92 self.values[row2][to_column] = self.values[row1][from_column]93 self.values[row1][from_column] = temp94 success = True95 96 return success97 def is_row_duplicate(self, row, value):98 """ Check whether there is a duplicate of a fixed/given value in a row. """99 for column in range(0, Nd):100 if(self.values[row][column] == value):101 return True102 return False103 def is_column_duplicate(self, column, value):104 """ Check whether there is a duplicate of a fixed/given value in a column. """105 for row in range(0, Nd):106 if(self.values[row][column] == value):107 return True108 return False109 def is_block_duplicate(self, row, column, value):110 """ Check whether there is a duplicate of a fixed/given value in a 3 x 3 block. """111 i = 3*(int(row/3))112 j = 3*(int(column/3))113 if((self.values[i][j] == value)114 or (self.values[i][j+1] == value)115 or (self.values[i][j+2] == value)116 or (self.values[i+1][j] == value)117 or (self.values[i+1][j+1] == value)118 or (self.values[i+1][j+2] == value)119 or (self.values[i+2][j] == value)120 or (self.values[i+2][j+1] == value)121 or (self.values[i+2][j+2] == value)):122 return True123 else:...
Mutation.py
Source:Mutation.py
12import numpy as np3import random4import operator5import setup as v6from past.builtins import range78class Mutation(object):9 10 11 def __init__(self):12 return13 14 15###############################################################################################16def swap_mutation(self, mutation_rate, given):17 1819 r = random.uniform(0, 1.1)20 while r > 1: 21 r = random.uniform(0, 1.1)2223 success = False24 if r < mutation_rate: 25 while not success:26 row1 = random.randint(0, 8)27 row2 = random.randint(0, 8)28 2930 from_column = random.randint(0, 8)31 to_column = random.randint(0, 8)32 while from_column == to_column:33 from_column = random.randint(0, 8)34 to_column = random.randint(0, 8)3536 37 if given.values[row1][from_column] == 0 and given.values[row1][to_column] == 0:38 39 if not given.is_column_duplicate(to_column, self.values[row1][from_column]) and not given.is_column_duplicate(from_column, self.values[row2][to_column]) and not given.is_block_duplicate(row2, to_column, self.values[row1][from_column]) and not given.is_block_duplicate(row1, from_column, self.values[row2][to_column]):40 41 temp = self.values[row2][to_column]42 self.values[row2][to_column] = self.values[row1][from_column]43 self.values[row1][from_column] = temp44 success = True4546 return success4748###############################################################################################49def inversion_mutation(self, mutation_rate, given):5051 r = random.uniform(0, 1.1)52 while r > 1: 53 r = random.uniform(0, 1.1)5455 success = False56 if r < mutation_rate: 57 while not success:58 row1 = random.randint(0, 8)59 row2 = random.randint(0, 8)60 6162 from_column = random.randint(0, 8)63 to_column = random.randint(0, 8)64 while from_column == to_column:65 from_column = random.randint(0, 8)66 to_column = random.randint(0, 8)6768 69 if given.values[row1][from_column] == 0 and given.values[row1][to_column] == 0:70 71 if not given.is_column_duplicate(to_column, self.values[row1][from_column]) and not given.is_column_duplicate(from_column, self.values[row2][to_column]) and not given.is_block_duplicate(row2, to_column, self.values[row1][from_column]) and not given.is_block_duplicate(row1, from_column, self.values[row2][to_column]):72 73 reversed(self.values[row1:row2][from_column:to_column])74 75 success = True76 77 return True7879###############################################################################################80def scramble_mutation(self, mutation_rate, given):8182 r = random.uniform(0, 1.1)83 while r > 1: 84 r = random.uniform(0, 1.1)8586 success = False87 if r < mutation_rate: 88 while not success:89 row1 = random.randint(0, 8)90 row2 = random.randint(0, 8)91 9293 from_column = random.randint(0, 8)94 to_column = random.randint(0, 8)95 while from_column == to_column:96 from_column = random.randint(0, 8)97 to_column = random.randint(0, 8)9899 100 if given.values[row1][from_column] == 0 and given.values[row1][to_column] == 0:101 102 if not given.is_column_duplicate(to_column, self.values[row1][from_column]) and not given.is_column_duplicate(from_column, self.values[row2][to_column]) and not given.is_block_duplicate(row2, to_column, self.values[row1][from_column]) and not given.is_block_duplicate(row1, from_column, self.values[row2][to_column]):103 104 random.shuffle(self.values[row1:row2][from_column:to_column])105 106 107 success = True108 109 110 return True
...
test_column_format.py
Source:test_column_format.py
2import pytest3from json2db.core import ColumnFormat, FrameworkNotSupport4def test_not_support():5 with pytest.raises(FrameworkNotSupport):6 ColumnFormat.to_column("test", "NotSupportFormat")7def test_to_camel():8 assert "abcAbc" == ColumnFormat.to_column("abc_abc", ColumnFormat.CAMEL)9 assert "abcAbcId" == ColumnFormat.to_column("abc_abc", ColumnFormat.CAMEL, "id")10 assert "abcAbcEfg" == ColumnFormat.to_column("abc_abc_efg", ColumnFormat.CAMEL, "")11 assert "abcAbcEfgId" == ColumnFormat.to_column("abc_abc_efg", ColumnFormat.CAMEL, "id")12 assert "AbcAbc" == ColumnFormat.to_column("AbcAbc", ColumnFormat.CAMEL)13 assert "AbcAbcFakeId" == ColumnFormat.to_column("AbcAbc", ColumnFormat.CAMEL, "fake_id")14def test_to_underline():15 assert "abc_abc" == ColumnFormat.to_column("abcAbc", ColumnFormat.UNDERLINE)16 assert "abc_abc_id" == ColumnFormat.to_column("abcAbc", ColumnFormat.UNDERLINE, 'id')17 assert "abc_Abc_id" == ColumnFormat.to_column("abc_Abc", ColumnFormat.UNDERLINE, 'id')18def test_to_default():19 assert "abcAbc" == ColumnFormat.to_column("abcAbc", ColumnFormat.DEFAULT)20 assert "abcAbcid" == ColumnFormat.to_column("abcAbc", ColumnFormat.DEFAULT, 'id')21def test_column_object_camel():22 fmt = ColumnFormat(ColumnFormat.CAMEL)23 assert fmt.rename("abcABC") == "abcABC"24 assert fmt.rename("abcABC", "id") == "abcABCId"25 assert fmt.rename("abc_aBC", "id") == "abcABCId"26def test_column_object_default():27 fmt = ColumnFormat()28 assert fmt.rename("abcABC") == "abcABC"29 assert fmt.rename("abcABC", "id") == "abcABCid"30 assert fmt.rename("abc_aBC", "id") == "abc_aBCid"31def test_column_object_underline():32 fmt = ColumnFormat(ColumnFormat.UNDERLINE)33 assert fmt.rename("abcABC") == "abc_aBC"34 assert fmt.rename("abcABC", "id") == "abc_aBC_id"...
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!!