Best Python code snippet using slash
GameOfSocialDistancing.py
Source:GameOfSocialDistancing.py  
1def convert_to_list(data):2    '''3    Converts a string to a list of strings.4    '''5    entirity = []6    for i in range(len(data)):7        output = []8        row = data[i]9        for j in range(len(row)):10            output.append(row[j])11        entirity.append(output)12    return entirity13data = open("Day 11\input11", "r+")14data = data.read()15data = data.split("\n")16data = convert_to_list(data)17def fill_seats(data):18    '''19    Plays minesweeper and leaves the seat if 4 occupied seats are adjacent.20    '''21    still_simulating = True22    while(still_simulating == True):23        still_simulating = False24        #print("Iteration start")25        for i in range(len(data)):26            row = data[i]27            occupied = 028            for j in range(len(row)):29                if(row[j] == "L"):30                    if(j == 0):31                        if(row[j+1] != "#" and row[j+1] != "1"):32                            if(i == len(data) - 1):33                                above = data[i-1]34                                if(above[j] != "#" and above[j+1] != "#"35                                and above[j] != "1" and above[j+1] != "1"):36                                    row[j] = "0"37                                    still_simulating = True38                            elif(i == 0):39                                below = data[i+1]40                                if(below[j] != "#" and below[j+1] != "#"41                                and below[j] != "1" and below[j+1] != "1"):42                                    row[j] = "0"43                                    still_simulating = True44                            else:45                                above = data[i-1]46                                below = data[i+1]47                                if(below[j] != "#" and below[j+1] != "#"48                                and below[j] != "1" and below[j+1] != "1"49                                and above[j] != "#" and above[j+1] != "#"50                                and above[j] != "1" and above[j+1] != "1"):51                                    row[j] = "0"52                                    still_simulating = True53                    elif(j == len(row) - 1):54                        if(row[j-1] != "#" and row[j-1] != "1"):55                            if(i == len(data) - 1):56                                above = data[i-1]57                                if(above[j] != "#" and above[j-1] != "#"58                                and above[j] != "1" and above[j-1] != "1"):59                                    row[j] = "0"60                                    still_simulating = True61                            elif(i == 0):62                                below = data[i+1]63                                if(below[j] != "#" and below[j-1] != "#"64                                and below[j] != "1" and below[j-1] != "1"):65                                    row[j] = "0"66                                    still_simulating = True67                            else:68                                above = data[i-1]69                                below = data[i+1]70                                if(below[j] != "#" and below[j-1] != "#"71                                and below[j] != "1" and below[j-1] != "1"72                                and above[j] != "#" and above[j-1] != "#"73                                and above[j] != "1" and above[j-1] != "1"):74                                    row[j] = "0"75                                    still_simulating = True76                    else:77                        if(row[j-1] != "#" and row[j+1] != "#"78                        and row[j-1] != "1" and row[j+1] != "1"):79                            if(i == len(data) - 1):80                                above = data[i-1]81                                if(above[j] != "#" and above[j-1] != "#" and above[j+1] != "#"82                                and above[j] != "1" and above[j-1] != "1" and above[j+1] != "1"):83                                    row[j] = "0"84                                    still_simulating = True85                            elif(i == 0):86                                below = data[i+1]87                                if(below[j] != "#" and below[j-1] != "#" and below[j+1] != "#"88                                and below[j] != "1" and below[j-1] != "1" and below[j+1] != "1"):89                                    row[j] = "0"90                                    still_simulating = True91                            else:92                                above = data[i-1]93                                below = data[i+1]94                                if(below[j] != "#" and below[j-1] != "#" and below[j+1] != "#"95                                and below[j] != "1" and below[j-1] != "1" and below[j+1] != "1"96                                and above[j] != "#" and above[j-1] != "#" and above[j+1] != "#"97                                and above[j] != "1" and above[j-1] != "1" and above[j+1] != "1"):98                                    row[j] = "0"99                                    still_simulating = True100                elif(row[j] == "#"):101                    occupied = 0102                    if(j == 0):103                        if(row[j+1] == "#" or row[j+1] == "1"):104                            occupied += 1105                        if(i == len(data) - 1):106                            above = data[i-1]107                            if(above[j] == "#" or above[j] == "1"):108                                occupied +=1109                            if(above[j+1] == "#" or above[j+1] == "1"):110                                occupied +=1111                        elif(i == 0):112                            below = data[i+1]113                            if(below[j] == "#" or below[j] == "1"):114                                occupied +=1115                            if(below[j+1] == "#" or below[j+1] == "1"):116                                occupied +=1117                        else:118                            above = data[i-1]119                            below = data[i+1]120                            if(above[j] == "#" or above[j] == "1"):121                                occupied +=1122                            if(above[j+1] == "#" or above[j+1] == "1"):123                                occupied +=1124                            if(below[j] == "#" or below[j] == "1"):125                                occupied +=1126                            if(below[j+1] == "#" or below[j+1] == "1"):127                                occupied +=1128                    elif(j == len(row) - 1):129                        if(row[j-1] == "#" or row[j-1] == "1"):130                            occupied += 1131                        if(i == len(data) - 1):132                            above = data[i-1]133                            if(above[j] == "#" or above[j] == "1"):134                                occupied +=1135                            if(above[j-1] == "#" or above[j-1] == "1"):136                                occupied +=1137                        elif(i == 0):138                            below = data[i+1]139                            if(below[j] == "#" or below[j] == "1"):140                                occupied +=1141                            if(below[j-1] == "#" or below[j-1] == "1"):142                                occupied +=1143                        else:144                            above = data[i-1]145                            below = data[i+1]146                            if(above[j] == "#" or above[j] == "1"):147                                occupied +=1148                            if(above[j-1] == "#" or above[j-1] == "1"):149                                occupied +=1150                            if(below[j] == "#" or below[j] == "1"):151                                occupied +=1152                            if(below[j-1] == "#" or below[j-1] == "1"):153                                occupied +=1154                    else:155                        if(row[j+1] == "#" or row[j+1] == "1"):156                            occupied += 1157                        if(row[j-1] == "#" or row[j-1] == "1"):158                            occupied += 1159                        if(i == len(data) - 1):160                            above = data[i-1]161                            if(above[j] == "#" or above[j] == "1"):162                                occupied +=1163                            if(above[j-1] == "#" or above[j-1] == "1"):164                                occupied +=1165                            if(above[j+1] == "#" or above[j+1] == "1"):166                                occupied +=1167                        elif(i == 0):168                            below = data[i+1]169                            if(below[j] == "#" or below[j] == "1"):170                                occupied +=1171                            if(below[j-1] == "#" or below[j-1] == "1"):172                                occupied +=1173                            if(below[j+1] == "#" or below[j+1] == "1"):174                                occupied +=1175                        else:176                            above = data[i-1]177                            below = data[i+1]178                            if(above[j] == "#" or above[j] == "1"):179                                occupied +=1180                            if(above[j-1] == "#" or above[j-1] == "1"):181                                occupied +=1182                            if(below[j] == "#" or below[j] == "1"):183                                occupied +=1184                            if(below[j-1] == "#" or below[j-1] == "1"):185                                occupied +=1186                            if(below[j+1] == "#" or below[j+1] == "1"):187                                occupied +=1188                            if(above[j+1] == "#" or above[j+1] == "1"):189                                occupied +=1190                    if(occupied >= 4):191                        row[j] = "1"192                        still_simulating = True193            #print(i, row)194            data[i] = row195        for x in range(len(data)):196            row = data[x]197            for y in range(len(row)):198                if(row[y] == "0"):199                    row[y] = "#"200                elif(row[y] == "1"):201                    row[y] = "L"202            data[x] = row203    return data204def social_distancing(data):205    '''206    Plays minesweeper^2 leaves the seat if at least 5 of the first seats of each direction (diagonals included) are occupied seats.207    '''208    still_simulating = True209    while(still_simulating == True):210        still_simulating = False211        for i in range(len(data)):212            row = data[i]213            occupied = 0214            for j in range(len(row)):215                if(row[j] == "L"):216                    occupied = 0217                    iteration = j - 1218                    while(iteration >= 0):219                        if(row[iteration] == "#" or row[iteration] == "1"):220                            occupied +=1221                            break222                        if(row[iteration] == "L" or row[iteration] == "0"):223                            break224                        iteration -=1225                    iteration = j + 1226                    while(iteration < len(row)):227                        if(row[iteration] == "#" or row[iteration] == "1"):228                            occupied +=1229                            break230                        if(row[iteration] == "L" or row[iteration] == "0"):231                            break232                        iteration +=1233                    iterationI = i - 1234                    iterationJ = j - 1235                    while(iterationJ >= 0 and iterationI >= 0):236                        if(data[iterationI][iterationJ] == "#"237                        or data[iterationI][iterationJ] == "1"):238                            occupied +=1239                            break240                        if(data[iterationI][iterationJ] == "L"241                        or data[iterationI][iterationJ] == "0"):242                            break243                        iterationI -=1244                        iterationJ -=1245                    iterationI = i - 1246                    while(iterationI >= 0):247                        if(data[iterationI][j] == "#"248                        or data[iterationI][j] == "1"):249                            occupied +=1250                            break251                        if(data[iterationI][j] == "L"252                        or data[iterationI][j] == "0"):253                            break254                        iterationI -=1255                    iterationI = i - 1256                    iterationJ = j + 1257                    while(iterationJ < len(row) and iterationI >= 0):258                        if(data[iterationI][iterationJ] == "#"259                        or data[iterationI][iterationJ] == "1"):260                            occupied +=1261                            break262                        if(data[iterationI][iterationJ] == "L"263                        or data[iterationI][iterationJ] == "0"):264                            break265                        iterationI -=1266                        iterationJ +=1267                    iterationI = i + 1268                    iterationJ = j - 1269                    while(iterationJ >= 0 and iterationI < len(data)):270                        if(data[iterationI][iterationJ] == "#"271                        or data[iterationI][iterationJ] == "1"):272                            occupied +=1273                            break274                        if(data[iterationI][iterationJ] == "L"275                        or data[iterationI][iterationJ] == "0"):276                            break277                        iterationI +=1278                        iterationJ -=1279                    iterationI = i + 1280                    while(iterationI < len(data)):281                        if(data[iterationI][j] == "#"282                        or data[iterationI][j] == "1"):283                            occupied +=1284                            break285                        if(data[iterationI][j] == "L"286                        or data[iterationI][j] == "0"):287                            break288                        iterationI +=1289                    iterationI = i + 1290                    iterationJ = j + 1291                    while(iterationJ < len(row) and iterationI < len(data)):292                        if(data[iterationI][iterationJ] == "#"293                        or data[iterationI][iterationJ] == "1"):294                            occupied +=1295                            break296                        if(data[iterationI][iterationJ] == "L"297                        or data[iterationI][iterationJ] == "0"):298                            break299                        iterationI +=1300                        iterationJ +=1301                    if(occupied == 0):302                        row[j] = "0"303                        still_simulating = True304                elif(row[j] == "#"):305                    occupied = 0306                    iteration = j - 1307                    while(iteration >= 0):308                        if(row[iteration] == "#" or row[iteration] == "1"):309                            occupied +=1310                            break311                        if(row[iteration] == "L" or row[iteration] == "0"):312                            break313                        iteration -=1314                    iteration = j + 1315                    while(iteration < len(row)):316                        if(row[iteration] == "#" or row[iteration] == "1"):317                            occupied +=1318                            break319                        if(row[iteration] == "L" or row[iteration] == "0"):320                            break321                        iteration +=1322                    iterationI = i - 1323                    iterationJ = j - 1324                    while(iterationJ >= 0 and iterationI >= 0):325                        if(data[iterationI][iterationJ] == "#"326                        or data[iterationI][iterationJ] == "1"):327                            occupied +=1328                            break329                        if(data[iterationI][iterationJ] == "L"330                        or data[iterationI][iterationJ] == "0"):331                            break332                        iterationI -=1333                        iterationJ -=1334                    iterationI = i - 1335                    while(iterationI >= 0):336                        if(data[iterationI][j] == "#"337                        or data[iterationI][j] == "1"):338                            occupied +=1339                            break340                        if(data[iterationI][j] == "L"341                        or data[iterationI][j] == "0"):342                            break343                        iterationI -=1344                    iterationI = i - 1345                    iterationJ = j + 1346                    while(iterationJ < len(row) and iterationI >= 0):347                        if(data[iterationI][iterationJ] == "#"348                        or data[iterationI][iterationJ] == "1"):349                            occupied +=1350                            break351                        if(data[iterationI][iterationJ] == "L"352                        or data[iterationI][iterationJ] == "0"):353                            break354                        iterationI -=1355                        iterationJ +=1356                    iterationI = i + 1357                    iterationJ = j - 1358                    while(iterationJ >= 0 and iterationI < len(data)):359                        if(data[iterationI][iterationJ] == "#"360                        or data[iterationI][iterationJ] == "1"):361                            occupied +=1362                            break363                        if(data[iterationI][iterationJ] == "L"364                        or data[iterationI][iterationJ] == "0"):365                            break366                        iterationI +=1367                        iterationJ -=1368                    iterationI = i + 1369                    while(iterationI < len(data)):370                        if(data[iterationI][j] == "#"371                        or data[iterationI][j] == "1"):372                            occupied +=1373                            break374                        if(data[iterationI][j] == "L"375                        or data[iterationI][j] == "0"):376                            break377                        iterationI +=1378                    iterationI = i + 1379                    iterationJ = j + 1380                    while(iterationJ < len(row) and iterationI < len(data)):381                        if(data[iterationI][iterationJ] == "#"382                        or data[iterationI][iterationJ] == "1"):383                            occupied +=1384                            break385                        if(data[iterationI][iterationJ] == "L"386                        or data[iterationI][iterationJ] == "0"):387                            break388                        iterationI +=1389                        iterationJ +=1390                    if(occupied >= 5):391                        row[j] = "1"392                        still_simulating = True393            #print(i, row)394            data[i] = row395        for x in range(len(data)):396            row = data[x]397            for y in range(len(row)):398                if(row[y] == "0"):399                    row[y] = "#"400                elif(row[y] == "1"):401                    row[y] = "L"402            data[x] = row403    return data404def count_occupied_seats(data):405    '''406    counts all the occupied seats407    '''408    count = 0409    for i in range(len(data)):410        row = data[i]411        for j in range(len(row)):412            if (row[j] == "#"):413                count += 1414    return count415seats = fill_seats(data)416puzzle1 = count_occupied_seats(seats)417data = open("Day 11\input11", "r+")418data = data.read()419data = data.split("\n")420data = convert_to_list(data)421seats = social_distancing(data)422puzzle2 = count_occupied_seats(seats)...query_optimizer_v2.py
Source:query_optimizer_v2.py  
1import json2from collections import Counter3from collections import defaultdict4from django.db.models.query import QuerySet5from django.core.exceptions import FieldError6from django.db.models.fields.related import ForeignKey7from django.db.models.fields.related import ManyToManyField8from django.db.models.fields.reverse_related import ManyToManyRel9from django.db.models.fields.reverse_related import ManyToOneRel10from django.db.models.fields.reverse_related import OneToOneRel11"""12query AllCities {13  allCities {14    id15    name16    state {17      name18    }19    # mayor {20    #   firstName21    #   city {22    #     id23    #     name24    #   }25    # }26    district {27      name28      city {29        name30        mayor {31          lastName32          city {33            name34            district {35              name36            }37          }38        }39      }40    }41  }42}43"""44# FIXME: Uncomment mayor query and run the code. Fix this case!45class GQOptimizer():46    """47    Class optimizes QuerySet base on information extracted from graphql query.48    """49    def __init__(self, info):50        self.info = info51        self.gql_query = info.field_nodes[0].loc.source.body52        self.select_related = set()53        self.prefetch_related = set()54        print(self.gql_query)55    def optimize(self, queryset: QuerySet, stop_fields=[]) -> QuerySet:56        self.stop_fields = stop_fields57        paths = self.__extract_paths()58        print('before normalization', paths)59        if self.stop_fields:60            paths = self.__normalize_paths(paths)61        print('normalized paths', paths)62        select_related = []63        prefetch_related = []64        for path in paths:65            # Get first model fro the model relations path66            first_model = path67            if '__' in path:68                first_model = path.split('__')[0]69            # Extract filed from first model70            field = queryset.model._meta.get_field(first_model)71            # Select related models72            if isinstance(field, (ForeignKey, ManyToManyField, OneToOneRel)):73                select_related.append(path)74            # Prefetch related models75            if isinstance(field, (ManyToOneRel, ManyToManyRel)):76                if path not in select_related:77                    prefetch_related.append(path)78        print(f'Select related: {select_related}')79        if select_related:80            queryset = queryset.select_related(*select_related)81        print(f'Prefetch_related: {prefetch_related}')82        if prefetch_related:83            queryset = queryset.prefetch_related(*prefetch_related)84        return queryset85    def print_types(self):86        """Print all graphql types"""87        for k, v in self.info.schema.type_map.items():88            print('Types', k, v)89    def __normalize_paths(self, paths: list):90        normalized_paths = []91        for stop_field in self.stop_fields:92            for path in paths:93                path = path.replace(stop_field, '')94                path = path.strip('_')95                models = path.split('__')96                if len(models) == 1:97                    normalized_paths.append(path)98                else:99                    models_count = dict(Counter(models))100                    for model, count in models_count.items():101                        if count >= 2:102                            path = model.join(path.split(model, 2)[:2])103                            path = path.strip('_')104                    normalized_paths.append(path)105        return normalized_paths106    def __extract_paths(self):107        root = self.info.field_name108        paths = defaultdict(list)109        iteration = 0110        has_leaves = True111        while has_leaves:112            has_leaves = False113            # 1st level on nesting114            if iteration == 0:115                print('Iteration', iteration)116                leaves = []117                # Extract fields from root of the query118                selection_set = self.info.field_nodes[0].selection_set119                for idx, selection in enumerate(selection_set.selections):120                    # Check if extracted field is a Type121                    if self.__filed_type(selection.name.value):122                        # Check for nested leaves inside selection123                        selection_set = self.info.field_nodes[0] \124                            .selection_set.selections[idx] \125                            .selection_set126                        # has_leaves = self.__selection_has_leaves(selections)127                        has_leaves = self.__selection_has_leaves(selection_set)128                        # Create metadata filed129                        paths[iteration].append({130                            'has_leaves': has_leaves,131                            'index': idx,132                            'root': root,133                            'parent': root,134                            'field_name': selection.name.value,135                            'selection': root + '__' + selection.name.value136                        })137                        leaves.append(True)138                    else:139                        leaves.append(False)140                # Check that leaves exist141                has_leaves, iteration = self.__increment(iteration, leaves)142            # 2nd level of nesting143            if iteration == 1:144                print('Iteration', iteration)145                leaves = []146                first_iteration = iteration - 1147                for first_leaf_meta_field in paths[first_iteration]:148                    first_leaf_idx = first_leaf_meta_field['index']149                    selection_set = self.info.field_nodes[0] \150                        .selection_set.selections[first_leaf_idx] \151                        .selection_set152                    for idx, selection in enumerate(selection_set.selections):153                        if self.__filed_type(selection.name.value):154                            # Check for nested leaves inside selection155                            selection_set = self.info.field_nodes[0] \156                                .selection_set.selections[first_leaf_idx] \157                                .selection_set.selections[idx] \158                                .selection_set159                            has_leaves = self.__selection_has_leaves(selection_set)160                            # Create metadata filed and add it to current iteration161                            paths[iteration].append({162                                'has_leaves': has_leaves,163                                'index': idx,164                                'root': root,165                                'parent': first_leaf_meta_field['field_name'],166                                'field_name': selection.name.value,167                                'selection': first_leaf_meta_field['selection'] + '__' + selection.name.value168                            })169                            leaves.append(True)170                        else:171                            leaves.append(False)172                # Check that leaves exist173                has_leaves, iteration = self.__increment(iteration, leaves)174            # 3rd level of nesting175            if iteration == 2:176                print('Iteration', iteration)177                leaves = []178                first_iteration = iteration - 2179                second_iteration = iteration - 1180                for first_leaf_meta_field in paths[first_iteration]:181                    first_leaf_idx = first_leaf_meta_field['index']182                    if first_leaf_meta_field['has_leaves']:183                        for second_leaf_meta_field in paths[second_iteration]:184                            second_leaf_idx = second_leaf_meta_field['index']185                            if second_leaf_meta_field['has_leaves']:186                                selection_set = self.info.field_nodes[0] \187                                    .selection_set.selections[first_leaf_idx] \188                                    .selection_set.selections[second_leaf_idx] \189                                    .selection_set190                                for idx, selection in enumerate(selection_set.selections):191                                    # Create metadata filed and add it to current iteration192                                    if self.__filed_type(selection.name.value):193                                        selection_set = self.info.field_nodes[0] \194                                            .selection_set.selections[first_leaf_idx] \195                                            .selection_set.selections[second_leaf_idx] \196                                            .selection_set.selections[idx] \197                                            .selection_set198                                        # Check for nested leaves inside selection199                                        has_leaves = self.__selection_has_leaves(selection_set)200                                        paths[iteration].append({201                                            'has_leaves': has_leaves,202                                            'index': idx,203                                            'root': root,204                                            'parent': second_leaf_meta_field['field_name'],205                                            'field_name': selection.name.value,206                                            'selection': second_leaf_meta_field['selection'] + '__' + selection.name.value207                                        })208                                        leaves.append(True)209                                    else:210                                        leaves.append(False)211                # Check that leaves exist212                has_leaves, iteration = self.__increment(iteration, leaves)213            # 4th level of nesting214            if iteration == 3:215                print('Iteration', iteration)216                leaves = []217                first_iteration = iteration - 3218                second_iteration = iteration - 2219                third_iteration = iteration - 1220                for first_leaf_meta_field in paths[first_iteration]:221                    first_leaf_idx = first_leaf_meta_field['index']222                    if first_leaf_meta_field['has_leaves']:223                        for second_leaf_meta_field in paths[second_iteration]:224                            second_leaf_idx = second_leaf_meta_field['index']225                            for third_leaf_meta_field in paths[third_iteration]:226                                third_leaf_idx = third_leaf_meta_field['index']227                                if third_leaf_meta_field['has_leaves']:228                                    selection_set = self.info.field_nodes[0] \229                                        .selection_set.selections[first_leaf_idx] \230                                        .selection_set.selections[second_leaf_idx] \231                                        .selection_set.selections[third_leaf_idx] \232                                        .selection_set233                                    for idx, selection in enumerate(selection_set.selections):234                                        # Create metadata filed and add it to current iteration235                                        if self.__filed_type(selection.name.value):236                                            selection_set = self.info.field_nodes[0] \237                                                .selection_set.selections[first_leaf_idx] \238                                                .selection_set.selections[second_leaf_idx] \239                                                .selection_set.selections[third_leaf_idx] \240                                                .selection_set.selections[idx] \241                                                .selection_set242                                            # Check for nested leaves inside selection243                                            has_leaves = self.__selection_has_leaves(selection_set)244                                            paths[iteration].append({245                                                'has_leaves': has_leaves,246                                                'index': idx,247                                                'root': root,248                                                'parent': third_leaf_meta_field['field_name'],249                                                'field_name': selection.name.value,250                                                'selection': third_leaf_meta_field['selection'] + '__' + selection.name.value251                                            })252                                            leaves.append(True)253                                        else:254                                            leaves.append(False)255                # Check that leaves exist256                has_leaves, iteration = self.__increment(iteration, leaves)257            # 5th level of nesting258            if iteration == 4:259                print('Iteration', iteration)260                leaves = []261                first_iteration = iteration - 4262                second_iteration = iteration - 3263                third_iteration = iteration - 2264                fourth_iteration = iteration - 1265                for first_leaf_meta_field in paths[first_iteration]:266                    first_leaf_idx = first_leaf_meta_field['index']267                    if first_leaf_meta_field['has_leaves']:268                        for second_leaf_meta_field in paths[second_iteration]:269                            second_leaf_idx = second_leaf_meta_field['index']270                            for third_leaf_meta_field in paths[third_iteration]:271                                third_leaf_idx = third_leaf_meta_field['index']272                                for fourth_leaf_meta_field in paths[fourth_iteration]:273                                    fourth_leaf_idx = fourth_leaf_meta_field['index']274                                    if third_leaf_meta_field['has_leaves']:275                                        selection_set = self.info.field_nodes[0] \276                                            .selection_set.selections[first_leaf_idx] \277                                            .selection_set.selections[second_leaf_idx] \278                                            .selection_set.selections[third_leaf_idx] \279                                            .selection_set.selections[fourth_leaf_idx] \280                                            .selection_set281                                        for idx, selection in enumerate(selection_set.selections):282                                            # Create metadata filed and add it to current iteration283                                            if self.__filed_type(selection.name.value):284                                                selection_set = self.info.field_nodes[0] \285                                                    .selection_set.selections[first_leaf_idx] \286                                                    .selection_set.selections[second_leaf_idx] \287                                                    .selection_set.selections[third_leaf_idx] \288                                                    .selection_set.selections[fourth_leaf_idx] \289                                                    .selection_set.selections[idx] \290                                                    .selection_set291                                                # Check for nested leaves inside selection292                                                has_leaves = self.__selection_has_leaves(selection_set)293                                                paths[iteration].append({294                                                    'has_leaves': has_leaves,295                                                    'index': idx,296                                                    'root': root,297                                                    'parent': fourth_leaf_meta_field['field_name'],298                                                    'field_name': selection.name.value,299                                                    'selection': fourth_leaf_meta_field['selection'] + '__' + selection.name.value300                                                })301                                                leaves.append(True)302                                            else:303                                                leaves.append(False)304                # Check that leaves exist305                has_leaves, iteration = self.__increment(iteration, leaves)306        print(json.dumps(paths, indent=2, sort_keys=False))307        filtered_paths = set()308        for paths in paths.values():309            for field in paths:310                if not field['has_leaves']:311                    filtered_paths.add(field['selection'])312        return filtered_paths313    def __increment(self, iteration: int, leaves: list) -> tuple:314        """Check that section_has leaves"""315        has_leaves = any(leaves)316        if has_leaves:317            iteration += 1318        return (has_leaves, iteration)319    def __selection_has_leaves(self, selection_set) -> bool:320        """Check if selection has leaves"""321        selection = self.gql_query[selection_set.loc.start:selection_set.loc.end]322        return selection.count('{') >= 2323    def __filed_type(self, field_name) -> bool:324        """Check if field name has its Type"""325        field_name = field_name[0].upper() + field_name[1:] + 'Type'...SleepyCustoms.py
Source:SleepyCustoms.py  
1data = open("Day 6\input6", "r+")2data = data.read()3data = data.split("\n")4def split_entries_up(data=None):5    '''6    Splits all answers into groups as a two dimensional list!7    '''8    answers = [[]]9    entry = 010    group = 011    while(entry < len(data)):12        if(data[entry]==""):13            group += 114            entry += 115            answers.append([])16            answers[group].append(data[entry])17        else:18            answers[group].append(data[entry])19        entry+=120    return answers21def count_group_or(group=None):22    '''23    How not to do it:24    Count up all the occurances one-by-one (because someone underestimated the task)25    Returns the sum for each letter that has occured within in a group26    (not the total numbers of letters).27    '''28    a = 029    b = 030    c = 031    d = 032    e = 033    f = 034    g = 035    h = 036    i = 037    j = 038    k = 039    l = 040    m = 041    n = 042    o = 043    p = 044    q = 045    r = 046    s = 047    t = 048    u = 049    v = 050    w = 051    x = 052    y = 053    z = 054    for firstIteration in range(len(group)):55        answers = group[firstIteration]56        for secondIteration in range(len(answers)):57            if(answers[secondIteration]=="a"):58                a = 159            elif(answers[secondIteration]=="b"):60                b = 161            elif(answers[secondIteration]=="c"):62                c = 163            elif(answers[secondIteration]=="d"):64                d = 165            elif(answers[secondIteration]=="e"):66                e = 167            elif(answers[secondIteration]=="f"):68                f = 169            elif(answers[secondIteration]=="g"):70                g = 171            elif(answers[secondIteration]=="h"):72                h = 173            elif(answers[secondIteration]=="i"):74                i = 175            elif(answers[secondIteration]=="j"):76                j = 177            elif(answers[secondIteration]=="k"):78                k = 179            elif(answers[secondIteration]=="l"):80                l = 181            elif(answers[secondIteration]=="m"):82                m = 183            elif(answers[secondIteration]=="n"):84                n = 185            elif(answers[secondIteration]=="o"):86                o = 187            elif(answers[secondIteration]=="p"):88                p = 189            elif(answers[secondIteration]=="q"):90                q = 191            elif(answers[secondIteration]=="r"):92                r = 193            elif(answers[secondIteration]=="s"):94                s = 195            elif(answers[secondIteration]=="t"):96                t = 197            elif(answers[secondIteration]=="u"):98                u = 199            elif(answers[secondIteration]=="v"):100                v = 1101            elif(answers[secondIteration]=="w"):102                w = 1103            elif(answers[secondIteration]=="x"):104                x = 1105            elif(answers[secondIteration]=="y"):106                y = 1107            elif(answers[secondIteration]=="z"):108                z = 1109    return(a+b+c+d+e+f+g+h+i+j+k+l+m+n+o+p+q+r+s+t+u+v+w+x+y+z)110def count_group_and(group=None):111    '''112    How not to do it:113    Count up all the occurances one-by-one (because someone underestimated the task)114    Returns the sum for each letter that has occured for all members in a group115    (not the total numbers of letters).116    '''117    a = 0118    b = 0119    c = 0120    d = 0121    e = 0122    f = 0123    g = 0124    h = 0125    i = 0126    j = 0127    k = 0128    l = 0129    m = 0130    n = 0131    o = 0132    p = 0133    q = 0134    r = 0135    s = 0136    t = 0137    u = 0138    v = 0139    w = 0140    x = 0141    y = 0142    z = 0143    for firstIteration in range(len(group)):144        answers = group[firstIteration]145        hasA = False146        hasB = False147        hasC = False148        hasD = False149        hasE = False150        hasF = False151        hasG = False152        hasH = False153        hasI = False154        hasJ = False155        hasK = False156        hasL = False157        hasM = False158        hasN = False159        hasO = False160        hasP = False161        hasQ = False162        hasR = False163        hasS = False164        hasT = False165        hasU = False166        hasV = False167        hasW = False168        hasX = False169        hasY = False170        hasZ = False171        172        for secondIteration in range(len(answers)):173            if(answers[secondIteration]=="a" and hasA == False):174                a += 1175            elif(answers[secondIteration]=="b" and hasB == False):176                b += 1177            elif(answers[secondIteration]=="c" and hasC == False):178                c += 1179            elif(answers[secondIteration]=="d" and hasD == False):180                d += 1181            elif(answers[secondIteration]=="e" and hasE == False):182                e += 1183            elif(answers[secondIteration]=="f" and hasF == False):184                f += 1185            elif(answers[secondIteration]=="g" and hasG == False):186                g += 1187            elif(answers[secondIteration]=="h" and hasH == False):188                h += 1189            elif(answers[secondIteration]=="i" and hasI == False):190                i += 1191            elif(answers[secondIteration]=="j" and hasJ == False):192                j += 1193            elif(answers[secondIteration]=="k" and hasK == False):194                k += 1195            elif(answers[secondIteration]=="l" and hasL == False):196                l += 1197            elif(answers[secondIteration]=="m" and hasM == False):198                m += 1199            elif(answers[secondIteration]=="n" and hasN == False):200                n += 1201            elif(answers[secondIteration]=="o" and hasO == False):202                o += 1203            elif(answers[secondIteration]=="p" and hasP == False):204                p += 1205            elif(answers[secondIteration]=="q" and hasQ == False):206                q += 1207            elif(answers[secondIteration]=="r" and hasR == False):208                r += 1209            elif(answers[secondIteration]=="s" and hasS == False):210                s += 1211            elif(answers[secondIteration]=="t" and hasT == False):212                t += 1213            elif(answers[secondIteration]=="u" and hasU == False):214                u += 1215            elif(answers[secondIteration]=="v" and hasV == False):216                v += 1217            elif(answers[secondIteration]=="w" and hasW == False):218                w += 1219            elif(answers[secondIteration]=="x" and hasX == False):220                x += 1221            elif(answers[secondIteration]=="y" and hasY == False):222                y += 1223            elif(answers[secondIteration]=="z" and hasZ == False):224                z += 1225    if(a == len(group)):226        a = 1227    else:228        a = 0229    if(b == len(group)):230        b = 1231    else:232        b = 0233    if(c == len(group)):234        c = 1235    else:236        c = 0237    if(d == len(group)):238        d = 1239    else:240        d = 0241    if(e == len(group)):242        e = 1243    else:244        e = 0245    if(f == len(group)):246        f = 1247    else:248        f = 0249    if(g == len(group)):250        g = 1251    else:252        g = 0253    if(h == len(group)):254        h = 1255    else:256        h = 0257    if(i == len(group)):258        i = 1259    else:260        i = 0261    if(j == len(group)):262        j = 1263    else:264        j = 0265    if(k == len(group)):266        k = 1267    else:268        k = 0269    if(l == len(group)):270        l = 1271    else:272        l = 0273    if(m == len(group)):274        m = 1275    else:276        m = 0277    if(n == len(group)):278        n = 1279    else:280        n = 0281    if(o == len(group)):282        o = 1283    else:284        o = 0285    if(p == len(group)):286        p = 1287    else:288        p = 0289    if(q == len(group)):290        q = 1291    else:292        q = 0293    if(r == len(group)):294        r = 1295    else:296        r = 0297    if(s == len(group)):298        s = 1299    else:300        s = 0301    if(t == len(group)):302        t = 1303    else:304        t = 0305    if(u == len(group)):306        u = 1307    else:308        u = 0309    if(v == len(group)):310        v = 1311    else:312        v = 0313    if(w == len(group)):314        w = 1315    else:316        w = 0317    if(x == len(group)):318        x = 1319    else:320        x = 0321    if(y == len(group)):322        y = 1323    else:324        y = 0325    if(z == len(group)):326        z = 1327    else:328        z = 0329    330    return(a+b+c+d+e+f+g+h+i+j+k+l+m+n+o+p+q+r+s+t+u+v+w+x+y+z)331def check_everyone(data=None):332    '''333    Checks the two functions above on all groups.334    '''335    sumOr = 0336    sumAnd = 0337    for i in range(len(data)):338        sumOr += count_group_or(data[i])339        sumAnd += count_group_and(data[i])340    return (sumOr, sumAnd)341groups_data = split_entries_up(data)342puzzle1, puzzle2 = check_everyone(groups_data)...lr_finder.py
Source:lr_finder.py  
1import matplotlib.pyplot as plt2import math3class LRFinder:4    def __init__(5        self, optimizer, 6        min_lr=1e-4, max_lr=2e-2, 7        steps_per_epoch=None, epochs=None8    ):9        self.optimizer = optimizer10        self.min_lr = min_lr11        self.max_lr = max_lr12        self.total_iterations = steps_per_epoch * epochs13        self.iteration = 014        self.history = {}15        self.batch_step(self.iteration)16    def get_lr(self):17        '''Calculate the learning rate.'''18        x = (self.iteration % self.total_iterations) / self.total_iterations19        lr = self.min_lr + (self.max_lr - self.min_lr) * x20        lrs = list()21        for param_group in self.optimizer.param_groups:22            lrs.append(lr)23        return lrs24    def batch_step(self, batch_iteration=None, logs=None):25        self.iteration = batch_iteration or self.iteration + 126        for param_group, lr in zip(self.optimizer.param_groups, self.get_lr()):27            param_group['lr'] = lr28        if logs is not None:29            self.history.setdefault('lr', []).append(lr)30            self.history.setdefault('iterations', []).append(self.iteration)31            for k, v in logs.items():32                self.history.setdefault(k, []).append(v)33 34    def plot_lr(self):35        '''Helper function to quickly inspect the learning rate schedule.'''36        plt.plot(self.history['iterations'], self.history['lr'])37        plt.yscale('log')38        plt.xlabel('Iteration')39        plt.ylabel('Learning rate')40        41    def plot_loss(self):42        '''Helper function to quickly observe the learning rate experiment results.'''43        plt.plot(self.history['lr'], self.history['loss'])44        plt.xscale('log')45        plt.xlabel('Learning rate')46        plt.ylabel('Loss')47def find_lr(model, datagen_params, min_lr=1e-5, max_lr=1e-2, epochs=3):48    train_datagen, val_datagen = get_datagens(max_negatives=2000, **datagen_params)49    opt = torch.optim.SGD(model.parameters(), lr=min_lr, momentum=.9, weight_decay=1e-4)50    learner = RetinaLearner(model=model, opt=opt, loss=None, clf_loss=None, metrics=[], clf_reg_alpha=.5, ignored_keys=['clf_out'])51    print('steps per epoch: {}'.format(len(train_datagen)))52    lr_scheduler = LRFinder(learner.opt, min_lr=min_lr, max_lr=max_lr, steps_per_epoch=len(train_datagen), epochs=epochs)53    learner, history = orchestrate(54        learner=learner, train_datagen=train_datagen, val_datagen=val_datagen, epochs=epochs,55        lr_scheduler=lr_scheduler, checkpoints_pth=None, nb_freezed_epchs=-1, df=datagen_params['df'],56    )57    return learner, lr_scheduler58class Pilo:59    def __init__(60        self, optimizer, 61        min_lr=1e-4, max_lr=2e-2, 62        coeff = 1.,63        steps_per_epoch=None64    ):65        self.optimizer = optimizer66        self.min_lr = min_lr67        self.max_lr = max_lr68        self.total_iterations = steps_per_epoch69        self.iteration = 070        self.history = {}71        self.coeff = coeff72        self.batch_step(self.iteration)73    def get_lr(self):74        '''Calculate the learning rate.'''75        x = float(self.iteration % self.total_iterations) / self.total_iterations76        lr = self.max_lr - (self.max_lr - self.min_lr) * x77        lrs = list()78        for param_group in self.optimizer.param_groups:79            lrs.append(lr)80        return lrs81    def batch_step(self, batch_iteration=None, logs=None):82        self.iteration = batch_iteration or self.iteration + 183        for param_group, lr in zip(self.optimizer.param_groups, self.get_lr()):84            param_group['lr'] = lr85        if logs is not None:86            self.history.setdefault('lr', []).append(lr)87            self.history.setdefault('iterations', []).append(self.iteration)88    def step(self, batch_iteration=None, logs=None):89        self.max_lr *= self.coeff90        self.min_lr *= self.coeff91 92    def plot_lr(self):93        '''Helper function to quickly inspect the learning rate schedule.'''94        plt.plot(self.history['lr'])95#         plt.yscale('log')96        plt.xlabel('Iteration')97        plt.ylabel('Learning rate')98class PiloExt:99    def __init__(100        self, optimizer, 101        multiplier=.1,102        coeff=1.,103        steps_per_epoch=None104    ):105        self.optimizer = optimizer106        self.multiplier = multiplier107        self.total_iterations = steps_per_epoch108        self.param_groups_old = list()109        for param_group in self.optimizer.param_groups:110            self.param_groups_old.append(float(param_group['lr']))111        self.iteration = 0112        self.history = {}113        self.coeff = coeff114        self.batch_step(self.iteration)115        116    def get_lr(self):117        '''Calculate the learning rate.'''118        x = float(self.iteration % self.total_iterations) / self.total_iterations119        lrs = list()120        for i, param_group in enumerate(self.optimizer.param_groups):121            lrs.append(self.param_groups_old[i] * (1 - x) + self.param_groups_old[i] * self.multiplier * x)122        return lrs123    def batch_step(self, batch_iteration=None, logs=None):124        self.iteration = batch_iteration or self.iteration + 1125        for param_group, lr in zip(self.optimizer.param_groups, self.get_lr()):126            param_group['lr'] = lr127        if logs is not None:128            self.history.setdefault('lr', []).append(lr)129            self.history.setdefault('iterations', []).append(self.iteration)130    def step(self, batch_iteration=None, logs=None):131        for i, param_group in enumerate(self.param_groups_old):132            self.param_groups_old[i] *= self.coeff133 134    def plot_lr(self):135        '''Helper function to quickly inspect the learning rate schedule.'''136        plt.plot(self.history['lr'])137#         plt.yscale('log')138        plt.xlabel('Iteration')139        plt.ylabel('Learning rate')140class CosinePiloExt(PiloExt):141    def get_lr(self):142        '''Calculate the learning rate.'''143        x = float(self.iteration % self.total_iterations) / self.total_iterations144        lrs = list()145        for i, param_group in enumerate(self.optimizer.param_groups):146            lrs.append(147                self.param_groups_old[i] * self.multiplier 148                + (self.param_groups_old[i] - self.param_groups_old[i] * self.multiplier) 149                * (1 + math.cos(math.pi * x)) / 2150            )...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!!
