Best Python code snippet using ATX
test.py
Source:test.py  
...2122    def _gen_grid(self, x1, y1, x2, y2):23        return product(range(x1, x2 + 1), range(y1, y2 + 1))2425    def process_line(self, line):26        match = self._line_pat.search(line)27        if not match:28            return29        state, toggle = match.group(1, 2)30        x1, y1, x2, y2 = map(int, match.group(3, 4, 5, 6))3132        if state == 'on':33            self._lights.update(self._gen_grid(x1, y1, x2, y2))34        elif state == 'off':35            self._lights.difference_update(self._gen_grid(x1, y1, x2, y2))36        else:37            grid = set(self._gen_grid(x1, y1, x2, y2))38            switch_on = grid - self._lights39            self._lights -= grid & self._lights40            self._lights |= switch_on414243# tests44g = Grid()45g.process_line('turn on 0,0 through 999,999')46assert len(g) == 10000004748g = Grid()49g.process_line('toggle 0,0 through 999,0')50assert len(g) == 10005152g = Grid()53g.process_line('turn on 0,0 through 499,0')54g.process_line('toggle 0,0 through 999,0')55assert len(g) == 5005657g = Grid()58g.process_line('turn on 499,0 through 500,999')59g.process_line('turn off 499,499 through 500,500')60assert len(g) == (2000 - 4)616263class BrightnessGrid(Grid):64    def __init__(self):65        self._lights = [[0 for _ in range(1000)] for _ in range(1000)]66        self._brightness = 06768    def __len__(self):69        return self._brightness7071    def process_line(self, line):72        match = self._line_pat.search(line)73        if not match:74            return75        state, toggle = match.group(1, 2)76        x1, y1, x2, y2 = map(int, match.group(3, 4, 5, 6))7778        lights = self._lights79        coords = self._gen_grid(x1, y1, x2, y2)80        if state == 'on' or toggle:81            amount = 2 if toggle else 182            for x, y in coords:83                lights[x][y] += amount84                self._brightness += amount85        else:86            for x, y in coords:87                if lights[x][y] > 0:88                    lights[x][y] -= 189                    self._brightness -= 1909192g = BrightnessGrid()93g.process_line('turn on 0,0 through 0,0')94assert len(g) == 19596g.process_line('toggle 0,0 through 999,999')97assert len(g) == 20000019899g.process_line('turn off 0,0 through 999,999')100assert len(g) == 1000001101102g.process_line('turn off 0,0 through 999,999')103assert len(g) == 1104105g.process_line('turn off 0,0 through 999,999')106assert len(g) == 0107108109if Image is not None:110    class ImageGrid(Grid):111        def __init__(self):112            self._lights = Image.new('L', (1000, 1000), 0)113114        def process_line(self, line):115            match = self._line_pat.search(line)116            if not match:117                return118            state, toggle = match.group(1, 2)119            box = [int(c) for c in match.group(3, 4, 5, 6)]120121            lights = self._lights122            region = lights.crop(box)123            if state == 'on':124                region = region.point(lambda p: 255)125            elif state == 'off':126                region = region.point(lambda p: 0)127            else:128                region = region.point(lambda p: 255 - p)129            lights.paste(region, box)130131        def save(self, filename):132            self._lights.save(filename)133134135    class BrightnessImageGrid(ImageGrid, BrightnessGrid):136        def process_line(self, line):137            match = self._line_pat.search(line)138            if not match:139                return140            state, toggle = match.group(1, 2)141            box = [int(c) for c in match.group(3, 4, 5, 6)]142143            lights = self._lights144            region = lights.crop(box)145            if state == 'on' or toggle:146                amount = 10 if toggle else 5147                region = region.point(lambda p: p + amount)148            else:149                region = region.point(lambda p: max(0, p - 5))150            lights.paste(region, box)151152153if __name__ == '__main__':154    import sys155    import os.path156157    filename = sys.argv[-1]158    filename = '../2015 Python/1506.txt'159    if '-1' in sys.argv:160        g = Grid()161        with open(filename) as f:162            for line in f:163                g.process_line(line)164        print('First part:', len(g))165166    if '-2' in sys.argv:167        bg = BrightnessGrid()168        with open(filename) as f:169            for line in f:170                bg.process_line(line)171        print('Second part:', len(bg))172173    if Image is not None:174        if '-img1' in sys.argv:175            output = os.path.join(176                os.path.dirname(filename),177                os.path.splitext(os.path.basename(filename))[0] + '_img1.png')178            ig = ImageGrid()179            with open(filename) as f:180                for line in f:181                    ig.process_line(line)182            ig.save(output)183            print('Saved', output)184185        if '-img2' in sys.argv:186            output = os.path.join(187                os.path.dirname(filename),188                os.path.splitext(os.path.basename(filename))[0] + '_img2.png')189            big = BrightnessImageGrid()190            with open(filename) as f:191                for line in f:192                    big.process_line(line)193            big.save(output)
...test_virtual_printer.py
Source:test_virtual_printer.py  
2from temp_injector import create_printer3class TestPrinter(unittest.TestCase):4    def test_g28(self):5        p = create_printer()6        p.process_line('G28')7        self.assertEqual(p.x, 0)8        self.assertEqual(p.y, 0)9        self.assertEqual(p.z, 0)10    def test_g0(self):11        p = create_printer()12        p.process_line('G28')13        p.process_line('G92 E0')14        p.process_line('G0 X10.5 Y5 Z1')15        self.assertEqual(p.x, 10.5)16        self.assertEqual(p.y, 5)17        self.assertEqual(p.z, 1)18        self.assertEqual(p.e, 0)19        p.process_line('G0 X20 E5')20        self.assertEqual(p.x, 20)21        self.assertEqual(p.y, 5)22        self.assertEqual(p.z, 1)23        self.assertEqual(p.e, 5)24    def test_set_temperature(self):25        p = create_printer()26        27        p.process_line('M104 S100')28        self.assertEqual(p.hotend_temp, 100)29        p.process_line('M109 S200.5')30        self.assertEqual(p.hotend_temp, 200.5)31        p.process_line('M140 S55.5')32        self.assertEqual(p.bed_temp, 55.5)33        p.process_line('M190 S60')34        self.assertEqual(p.bed_temp, 60)35    def test_positioning_mode(self):36        p = create_printer()37        38        #absolute positioning39        p.process_line('G28')40        p.process_line('G92 E0')41        p.process_line('G90')42        self.assertEqual(p.x, 0)43        self.assertEqual(p.y, 0)44        self.assertEqual(p.z, 0)45        self.assertEqual(p.e, 0)46        p.process_line('G0 X5 Y5 Z5 E5')47        self.assertEqual(p.x, 5)48        self.assertEqual(p.y, 5)49        self.assertEqual(p.z, 5)50        self.assertEqual(p.e, 5)51        p.process_line('G0 X5 Y5 Z5 E5')52        self.assertEqual(p.x, 5)53        self.assertEqual(p.y, 5)54        self.assertEqual(p.z, 5)55        self.assertEqual(p.e, 5)56        #relative positioning 57        p.process_line('G28')58        p.process_line('G92 E0')59        p.process_line('G91')60        self.assertEqual(p.x, 0)61        self.assertEqual(p.y, 0)62        self.assertEqual(p.z, 0)63        self.assertEqual(p.e, 0)64        p.process_line('G0 X5 Y5 Z5 E5')65        self.assertEqual(p.x, 5)66        self.assertEqual(p.y, 5)67        self.assertEqual(p.z, 5)68        self.assertEqual(p.e, 5)69        p.process_line('G0 X5 Y5 Z5 E5')70        self.assertEqual(p.x, 10)71        self.assertEqual(p.y, 10)72        self.assertEqual(p.z, 10)73        self.assertEqual(p.e, 10)74        #absolute positioning with relative extruder positioning75        p.process_line('G28')76        p.process_line('G92 E0')77        p.process_line('G90')78        p.process_line('M83')79        self.assertEqual(p.x, 0)80        self.assertEqual(p.y, 0)81        self.assertEqual(p.z, 0)82        self.assertEqual(p.e, 0)83        p.process_line('G0 X5 Y5 Z5 E5')84        self.assertEqual(p.x, 5)85        self.assertEqual(p.y, 5)86        self.assertEqual(p.z, 5)87        self.assertEqual(p.e, 5)88        p.process_line('G0 X5 Y5 Z5 E5')89        self.assertEqual(p.x, 5)90        self.assertEqual(p.y, 5)91        self.assertEqual(p.z, 5)92        self.assertEqual(p.e, 10)93        #relative positioning with absolute extruder positioning94        p.process_line('G28')95        p.process_line('G92 E0')96        p.process_line('G91')97        p.process_line('M82')98        self.assertEqual(p.x, 0)99        self.assertEqual(p.y, 0)100        self.assertEqual(p.z, 0)101        self.assertEqual(p.e, 0)102        p.process_line('G0 X5 Y5 Z5 E5')103        self.assertEqual(p.x, 5)104        self.assertEqual(p.y, 5)105        self.assertEqual(p.z, 5)106        self.assertEqual(p.e, 5)107        p.process_line('G0 X5 Y5 Z5 E5')108        self.assertEqual(p.x, 10)109        self.assertEqual(p.y, 10)110        self.assertEqual(p.z, 10)...assignment1.1.py
Source:assignment1.1.py  
1import sys2import copy3file_name = input('Which data file do you want to use?')4try:5    file = open(file_name,'r')6    Lines = file.readlines()7    if Lines == []:8        raise ValueError9        sys.exit()10except (IOError, ValueError):11    print('Incorrect Input/Output or Value!')12    sys.exit()13process_Line = []14process_data = []15for line in Lines:16    data = line.split()17    process_data.append(data)18for a in process_data:19    each_Line = []20    for b in a:21        each_Line.append(int(b))22    # print(each_Line)23    process_Line.append(each_Line)24    # process_Line.sort(key=len(process_Line),reverse=True)25    #print('process_Line is ', process_Line)  # å¾å°æ¯ä¸è¡çæ°åï¼æ¯è¡å为ä¸ä¸ªlist26# print('lenth=',len(process_Line))27count = 128l = len(process_Line)29storage = []30sum_outpot = 031for i in range(l - 1, -1, -1):32    L = []33    path=[]34    #for k in range(0,l):35    k = l - i - 136    for j in range(0, len(process_Line[i])):37        if i + 1 == l:38            L.append([process_Line[i][j],count,[process_Line[i][j]]])39            path.append([process_Line[i][j]])40            #print('when i+1==l', L)41         #print('L=',L)42         #print('storage=',storage)43        else:44            #print('L=',L)45            #L.append(process_Line[i][j])46            if storage[k - 1][j][0] < storage[k - 1][j + 1][0]:47                sum_output = process_Line[i][j] + storage[k - 1][j + 1][0]  # store larger one48                L.append([sum_output,1,copy.deepcopy(storage[k - 1][j+1][2])])49                L[j][2].append(copy.deepcopy(process_Line[i][j]))50                path.append(copy.deepcopy(process_Line[i][j]))51                path.append(copy.deepcopy(storage[k - 1][j + 1][0]))52            elif storage[k - 1][j][0] == storage[k - 1][j + 1][0]:53                sum_output = process_Line[i][j] + storage[k - 1][j][0]54                L.append([sum_output,count + 1,copy.deepcopy(storage[k - 1][j][2])])55                L[j][2].append(copy.deepcopy(process_Line[i][j]))56                count += 157                path.append(copy.deepcopy(process_Line[i][j]))58                path.append(storage[k - 1][j][0])59            elif storage[k - 1][j][0] > storage[k - 1][j + 1][0]:60                sum_output = process_Line[i][j] + storage[k - 1][j][0]61                L.append([sum_output,1,copy.deepcopy(storage[k - 1][j][2])])62                L[j][2].append(copy.deepcopy(process_Line[i][j]))63                path.append(copy.deepcopy(process_Line[i][j]))64                path.append(storage[k - 1][j][0])65    storage.append(copy.deepcopy(L))66# sum_outpot =sum(L)67#print('path=',path)68#print('storage=', storage)69#print('path yielding=',storage[-1][-1][2])70print('The largest sum is: ', storage[-1][-1][0])71print('The number of paths yielding this sum is: ', storage[-1][-1][1])...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!!
