Best Python code snippet using molecule_python
test_lab_test.py
Source:test_lab_test.py  
...32		lab_test.descriptive_test_items[0].result_value = 1233		lab_test.descriptive_test_items[2].result_value = 134		lab_test.save()35		self.assertRaises(frappe.ValidationError, lab_test.submit)36	def test_sample_collection(self):37		frappe.db.set_value('Healthcare Settings', 'Healthcare Settings', 'create_sample_collection_for_lab_test', 1)38		lab_template = create_lab_test_template()39		lab_test = create_lab_test(lab_template)40		lab_test.descriptive_test_items[0].result_value = 1241		lab_test.descriptive_test_items[1].result_value = 142		lab_test.descriptive_test_items[2].result_value = 2.343		lab_test.save()44		# check sample collection created45		self.assertTrue(frappe.db.exists('Sample Collection', {'sample': lab_template.sample}))46		frappe.db.set_value('Healthcare Settings', 'Healthcare Settings', 'create_sample_collection_for_lab_test', 0)47		lab_test = create_lab_test(lab_template)48		lab_test.descriptive_test_items[0].result_value = 1249		lab_test.descriptive_test_items[1].result_value = 150		lab_test.descriptive_test_items[2].result_value = 2.3...lane_dataset_builder.py
Source:lane_dataset_builder.py  
1import os2import time3import eyes4import controller as gamepad5import motor6import random7# Root path for train and test data8root = "images/lanes/"9# Controls motors based on curr_input and returns drive state10def controller_input_handler(curr_input, motor_control: motor):11    # If the start button is pressed:12    # Stop the training session and turn off motors13    if curr_input == "START":14        if motor_control.motor_state is True:15            motor_control.toggle_motor()16        return False    # Ends the training loop17    # Drive motor using curr_input18    if curr_input == "A":19        motor_control.toggle_motor()20    if curr_input == "UP":21        motor_control.forward()22    if curr_input == "DOWN":23        motor_control.backward()24    if curr_input == "LEFT":25        motor_control.turn_left()26    if curr_input == "RIGHT":27        motor_control.turn_right()28    if curr_input == "LEFT TRIGGER":29        motor_control.rotate_left()30    if curr_input == "RIGHT TRIGGER":31        motor_control.rotate_right()32    drive_label = motor_control.get_drive_state_label()33    # DEBUG OUTPUT34    if curr_input is not None:35        print(f'{curr_input}\t--->\t{drive_label}')36    return drive_label37# Creates motor state directories if missing38def data_dir_check(path):39    if not os.path.exists(path):40        os.makedirs(path, exist_ok=True)41# Collects CNN training data:42# User input is read in a loop which controls the motor.43# The current drive state will be saved to the corresponding folder44# which can then be used to train the CNNmodel.45def collect_train_data(test_sample_collection=False):46    # Initialize movement counter47    drive_state_counter = {48        "stopped": 0, "forward": 0, "backward": 0,49        "left": 0, "right": 0,50        "rotate_right": 0, "rotate_left": 051    }52    motor_state = True53    # If test_sample_collection is enabled (TRUE),54    # ask for user input to determine what percent55    # of sampled data should be saved to the test data set56    if test_sample_collection:57        sample_percent = int(input("\nEnter percent of samples to save for testing pool:\n>> "))58    else:59        sample_percent = 060    # Initialize input device61    input_device = gamepad.Controller(3)62    # Initialize motor controller63    driver = motor.MotorControl()64    driver.set_speed(1)     # Set drive speed to MIN65    # Initialize train/test data path66    train_save_path = os.path.join(root, 'train')67    test_save_path = os.path.join(root, 'test')68    save_path = train_save_path     # Default save path69    data_dir_check(train_save_path)70    data_dir_check(train_save_path)71    # Initialize camera controller72    camera = eyes.Eyes()73    start_time = time.time()74    milli_sec_counter = time.time_ns()75    try:76        camera.camera_warm_up()77        while motor_state:78            curr_input = input_device.read_command()79            motor_state = controller_input_handler(curr_input, driver)80            if (motor_state != "stopped") \81                    and (motor_state != "backward") \82                    and (motor_state is not False):83                # If sampling enabled by user:84                # swap directory path between test and train85                # within desired percentage (sample_percent).86                if test_sample_collection:87                    if random.randrange(0, 100 + 1) < sample_percent:88                        save_path = test_save_path89                    else:90                        save_path = train_save_path91                # Set image name and path92                img_name = str(time.time_ns()) + ".png"93                img_path = os.path.join(save_path, motor_state)94                img_save_path = os.path.join(img_path, img_name)95                # Add delay between image captures to avoid data flooding96                # Save a forward image once every half second (1ms == 1^(6)ns)97                if motor_state == "forward":98                    if (time.time_ns() - milli_sec_counter) > 500000000:99                        camera.get_thresh_img()100                        data_dir_check(img_path)101                        camera.save_thresh_img(img_save_path)102                        drive_state_counter[motor_state] += 1103                        milli_sec_counter = time.time_ns()104                else:105                    # Save an image once every quarter second106                    if (time.time_ns() - milli_sec_counter) > 100000000:107                        camera.get_thresh_img()108                        data_dir_check(img_path)109                        camera.save_thresh_img(img_save_path)110                        drive_state_counter[motor_state] += 1111                        milli_sec_counter = time.time_ns()112    except KeyboardInterrupt:113        print("\nInterrupt detected. Operation stopped.\n")114    except TypeError as e:115        print("\nTypeError:", e)116    except AssertionError as e:117        print(e)118    # Run device destructors119    motor.destroy()120    input_device.kill_device()121    camera.destroy()122    # Print elapsed train time123    print(f'\nCompleted Data Collection.')124    print(f'\tElapsed Time: {round(((time.time()-start_time)/60), 2)} minutes')125    # Print tally of saved images from session126    print('\n-----------------')127    for drive_state_lbl, count in drive_state_counter.items():128        print(f'{drive_state_lbl}:', count)129    print('-----------------')130if __name__ == '__main__':131    # ### Collect Train Data132    # Ask user for test data sampling133    enable_sampling = input("\nEnable train data sampling (y/n)?\n>> ")134    if enable_sampling.lower() == 'yes' or enable_sampling.lower() == 'y':135        enable_sampling = True136    else:137        enable_sampling = False138    # Start train data collection...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!!
