Best Python code snippet using ATX
test_domains.py
Source:test_domains.py  
...66        response = self.client.post(uri, {})67        self.assertEqual(68            http.client.BAD_REQUEST, response.status_code, response.content69        )70    def test_can_set_serial(self):71        zone_serial.create_if_not_exists()72        self.become_admin()73        uri = get_domains_uri()74        serial = random.randint(1, INT_MAX)75        response = self.client.post(76            uri, {"op": "set_serial", "serial": str(serial)}77        )78        self.assertEqual(79            http.client.OK, response.status_code, response.content80        )81        # The handler forces a DNS reload by creating a new DNS publication,82        # so the serial has already been incremented.83        self.assertEqual(serial + 1, next(zone_serial))84    def test_set_serial_rejects_serials_less_than_1(self):...line_follower.py
Source:line_follower.py  
...7        print("FPS succesfully set to "+str(fps_count))8    else:9        print("FPS setting FAILED!")10    11def set_serial(left,right):12    line=str(int(left))+' '+str(int(right))+'\n'13    wiringpi.serialPuts(serial,line)14    15def process_angle_error(angle,error):16    global clock17    clock+=118    if(clock>divider):19        clock=020        KP=1.2521        if abs(angle)<15: #1022            KP=1.7523        AP=1.2524        speed=10025        steering = error * KP + angle * AP26        if steering == 0:27            port1=speed28            port2=speed29        else:30            if steering > 0:31                steering = 100 - steering32                port2=speed33                port1=speed*steering/10034            else:35                steering = steering * -136                steering = 100 - steering;37                port1=speed38                port2=speed*steering/10039        max_backward_speed=5040        if port1<-max_backward_speed:41            port1=-max_backward_speed #right42        if port2<-max_backward_speed:43            port2=-max_backward_speed #left44        set_serial(port2,port1)45        46def remove_noise(followed_line,errode_iterations,dilate_iterations):47    kernel = numpy.ones((3,3), numpy.uint8)48    followed_line = cv2.erode(followed_line, kernel, iterations=errode_iterations) #549    followed_line = cv2.dilate(followed_line, kernel, iterations=dilate_iterations) #1050    return followed_line51def compute_angle(width, height, angle):52    if angle < -90 or (width > height and angle < 0):53        return 90 + angle54    if width < height and angle > 0:55        return (90 - angle) * -1 56    return angle57def print_numbers(frame, angle, distance,intersection):58    angle_pos=(0,97*resolution[1]//100)59    error_pos=(0,12*resolution[1]//100)60    inters_pos=(93*resolution[0]//100,97*resolution[1]//100)61    cv2.putText(frame, str(angle), angle_pos, cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,255), 1)62    cv2.putText(frame, str(distance), error_pos, cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255,0,0), 1)63    if(intersection):64       cv2.putText(frame, "S", inters_pos, cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,255,255), 2)65     66def pre_process_frame(frame):67    #wykrywanie niebieskiej linii68    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) 69    lower_bound_blue = numpy.array([100,40,40]) 70    upper_bound_blue = numpy.array([140,255,255]) 71    followed_line = cv2.inRange(hsv, lower_bound_blue, upper_bound_blue)72    followed_line = remove_noise(followed_line,5,5)73    cv2.imshow(WINDOW_2, followed_line)74    #return clean image used to find rectangles75    return followed_line 76   77def process_frame(frame, resolution, last_followed):78    x_res, y_res = resolution79    x_last, y_last = last_followed80    distance, angle = 0, 081    followed_line=pre_process_frame(frame)82    image, contours, hierarchy = cv2.findContours(followed_line, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)83    84    if len(contours) > 0:85        if len(contours) == 1:86            selection = cv2.minAreaRect(contours[0])87        else:88            possible_selections = []89            off_bottom_selections = []90       91            for i in range(len(contours)):92                selection = cv2.minAreaRect(contours[i])93                (x, y), (width, height), angle = selection94                (x_bottom, y_bottom) = cv2.boxPoints(selection)[0] #first vertex is always the closest to the bottom95                if y_bottom >= y_res - 1:96                    last_followed_distance = ((x_last - x) ** 2 + (y_last - y) ** 2) ** 0.597                    off_bottom_selections.append((last_followed_distance, i))98                99                possible_selections.append((y_bottom, i))100            101            off_bottom_selections = sorted(off_bottom_selections)102            if len(off_bottom_selections) > 0:103                last_followed_distance, i = off_bottom_selections[0]104                selection = cv2.minAreaRect(contours[i])105            else:106                possible_selections = sorted(possible_selections)107                y_bottom, i = possible_selections[-1]108                109                selection = cv2.minAreaRect(contours[i])110        111        (x, y), (width, height), angle = selection112        113        if width*height>0.6*resolution[0]*resolution[1]:114            intersection=True115            angle=0116        else:117            intersection=False118            angle = int(compute_angle(width, height, angle))119        #right-angle turns detection routine120        if width*height>0.2*resolution[0]*resolution[1] and not width*height>0.6*resolution[0]*resolution[1]:121            if abs(angle)>70:122                if angle<0 and x>resolution[0]*0.5 and y>resolution[0]*0.5:123                    print("Right!")124                    angle=80125                elif angle>0 and x<resolution[0]*0.5 and y>resolution[0]*0.5:126                    print("Left!")127                    angle=-80      128        middle = int(x_res / 2)129        distance = int(x - middle)130        box = cv2.boxPoints(selection)131        box = numpy.int0(box)132        133        box_color=(0, 0, 255) #red normal134        if intersection:135            box_color= (0, 255, 255) #yellow on intersection136        cv2.drawContours(frame, [box], 0, box_color, 3)137        cv2.line(frame, (int(x), int(resolution[1]*0.6)), (int(x), int(resolution[1]*0.4)), (255,0,0), 1)138        print_numbers(frame, angle, distance,intersection)139        process_angle_error(angle, 100*distance/(x_res/2.0)) #process angle accepts percents now only!140    return distance, angle, (x_last, y_last)141def finish():142    global serial143    cap.release()144    cv2.destroyAllWindows()145    wiringpi.serialPuts(serial,"0 0\n")146    wiringpi.serialClose(serial)147    exit()148def manual_steering():149    set_serial(0,0)150    manual_steering_speed=100151    while(True):152        ret, frame = cap.read()153        cv2.imshow(PROGRAM_NAME, frame)154        pre_process_frame(frame)155        key=cv2.waitKey(1) & 0xFF156        if key == ord(' '):157            set_serial(0,0)158        if key == ord('w'):159            set_serial(manual_steering_speed,manual_steering_speed)160        if key == ord('s'):161            set_serial(-manual_steering_speed,-manual_steering_speed)162        if key == ord('a'):163            set_serial(-manual_steering_speed*0.8,manual_steering_speed*0.8)164        if key == ord('d'):165            set_serial(manual_steering_speed*0.8,-manual_steering_speed*0.8)166        if key == ord('e'):167            break168        if key == ord('q'):169            finish()170            171def main_loop():172    last_followed = int(resolution[0] / 2), int(resolution[1] / 2)173    while(True):174        ret, frame = cap.read()175        distance, angle, last_followed = process_frame(frame, resolution, last_followed)176        temp_frame=cv2.resize(frame,(resolution[0]*2,2*resolution[1]))177        cv2.imshow(PROGRAM_NAME, temp_frame)178        key=cv2.waitKey(1) & 0xFF179        if key == ord('q'):...py_engine.py
Source:py_engine.py  
...6class EngineAsync():7    def __init__(self):8        self.__sn = None910    def set_serial(self, sn, done_id):11        self.__sn = sn12        log.debug("done_id: {} set_serial: {}".format(done_id, self.__sn))13    14    def get_serial(self, done_id):15        log.debug("done_id: {} get_serial: {}".format(done_id, self.__sn))16        return self.__sn1718class Engine(EngineAsync):19    def __init__(self):20        super().__init__()21        self.__name = "name"22        self.__done_id = 12324    def __quard(self, func):25        def inner(*args, **kwargs):26            log.debug("Decorated")27            return func(*args, self.__done_id, **kwargs)28        return inner2930    def set_serial(self, sn):31        self.__quard(super().set_serial)(sn)32    33    def get_serial(self):34        return self.__quard(super().get_serial)()3536def main():37    engine = Engine()38    engine.set_serial(123321)39    log.debug(engine.get_serial())4041if __name__ == "__main__":
...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!!
