Best Python code snippet using autotest_python
views.py
Source:views.py
...16from .utils import resolve_file_name17from .utils import resolve_report18# Create your views here.19def index(request):20 return render(request, 'index.html', {"Labels": get_all_labels()})21def gallery(request, label):22 file_list = os.listdir(osp.join(osp.join(settings.BASE_DIR, "ImagesDB"), label))23 file_list = list(map(lambda item: r"/static/" + label + r"/" + item, file_list))24 return render(request, 'gallery.html', {"File": file_list, "Labels": get_all_labels()})25def gallery2(request):26 return render(request, 'index.html', {"File": get_all_labels()})27def slider(request):28 files = get_all_images()29 # print(files.shape)30 return render(request, 'slider.html', {"Labels": get_all_labels(), "File": files})31def overview(request):32 return render(request, 'overview.html', {"Labels": get_all_labels()})33def upload(request, is_single):34 if is_single == "single":35 return render(request, 'upload_s.html', {"Labels": get_all_labels()})36 else:37 return render(request, 'upload_m.html', {"Labels": get_all_labels()})38def classify(request):39 return render(request, "classify.html", {"Labels": get_all_labels()})40def classify_result(request):41 return render(request, "classify.html", {"Labels": get_all_labels()})42 # result = process_classify()43 # return render(request, "results_cls.html", {"Labels": get_all_labels(), "Results": result})44def train(request):45 form = TrainForm()46 return render(request, "train.html", {"Labels": get_all_labels(), "Form": form})47def validate(request):48 return render(request, "validate.html", {"Labels": get_all_labels()})49def dashboard(request):50 return render(request, 'dashboard.html', {"Labels": get_all_labels()})51def grids(request):52 return render(request, 'grids.html', {"Labels": get_all_labels()})53def media(request):54 return render(request, 'media.html', {"Labels": get_all_labels()})55def general(request):56 return render(request, 'general.html', {"Labels": get_all_labels()})57def typography(request):58 return render(request, 'typography.html', {"Labels": get_all_labels()})59def widgets(request):60 return render(request, 'widgets.html', {"Labels": get_all_labels()})61def inbox(request):62 return render(request, 'inbox.html', {"Labels": get_all_labels()})63def compose(request):64 return render(request, 'compose.html', {"Labels": get_all_labels()})65def tables(request):66 return render(request, 'tables.html', {"Labels": get_all_labels()})67def forms(request):68 return render(request, 'forms.html', {"Labels": get_all_labels()})69def validation(request):70 return render(request, 'validation.html', {"Labels": get_all_labels()})71def login(request):72 return render(request, 'login.html', {"Labels": get_all_labels()})73def signup(request):74 return render(request, 'signup.html', {"Labels": get_all_labels()})75def blank_page(request):76 return render(request, 'blank-page.html', {"Labels": get_all_labels()})77def charts(request):78 return render(request, 'charts.html', {"Labels": get_all_labels()})79# ç³»ç»å¤çé»è¾80def update_image_label(request):81 """82 æ´æ°å¾çç±»å«83 :param request: {oldURL: "æè°/zhanjian_01.jpg", newLabel: "å¦å
"}84 :return:85 """86 old_url = request.POST.get("oldURL").split("/")87 filename = old_url[1]88 old_label = old_url[0]89 new_label = request.POST.get("newLabel")90 old_url = osp.join(osp.join(osp.join(settings.BASE_DIR, "ImagesDB"), old_label), filename)91 new_url = osp.join(osp.join(osp.join(settings.BASE_DIR, "ImagesDB"), new_label), filename)92 if osp.exists(new_url):93 new_url = osp.splitext(new_url)[0] + ".1" + osp.splitext(new_url)[1]94 os.rename(old_url, new_url)95 response = {"status": 0}96 return JsonResponse(response)97def get_input_file_single(request):98 """99 è·åä¸ä¼ çå¾çï¼è¯¥æ¹æ³ä¸ºæç±»å«ä¸ä¼ çå¤çé»è¾100 :param request:101 :return:102 """103 response = {"status": 0}104 if request.method == "POST":105 label = request.POST.get("label")106 file_data = request.FILES.get(r"image_file", "没æå¾ç")107 filename = file_data.name108 file_path = osp.join(osp.join(settings.BASE_DIR, "media"), filename)109 write2disk(file_path, file_data)110 shutil.move(file_path, osp.join(osp.join(osp.join(settings.BASE_DIR, "ImagesDB"), label), filename))111 return JsonResponse(response)112def get_input_file_multiple(request):113 """114 æ¥æ¶ä¸ä¼ ç.zipæ件ï¼ä¿è¯æ件类åæ£ç¡®115 :param request:116 :return:117 """118 response = {"status": 0}119 if request.method == "POST":120 file_data = request.FILES.get(r"image_file_zip", "没ææ°æ®")121 file_name = file_data.name122 file_path = osp.join(osp.join(settings.BASE_DIR, "media"), file_name)123 write2disk(file_path, file_data)124 shutil.unpack_archive(file_path, osp.splitext(file_path)[0])125 return JsonResponse(response)126def delete_image(request):127 """128 å é¤å¾ç129 :param request: 130 :return: 131 """132 response = {"status": 0}133 label, filename = resolve_file_name(request.POST.get("file_path"))134 file_path = osp.join(osp.join(osp.join(settings.BASE_DIR, "ImagesDB"), label), filename)135 print(file_path)136 # os.remove(file_path)137 return JsonResponse(response)138def get_classify_image(request):139 """140 ä¿åå¾
æ 注çä¸ä¼ å¾å141 :param request:142 :return:143 """144 response = {"status": 0}145 if request.method == "POST":146 file_data = request.FILES.get(r"image", "没æå¾ç")147 filename = file_data.name148 file_path = osp.join(osp.join(settings.BASE_DIR, "media"), filename)149 write2disk(file_path, file_data)150 return JsonResponse(response)151def process_classify(request):152 """153 è¿è¡æ 注æä½å¹¶è¿åç»æ154 :param request:155 :return: [{156 "name": å¾çå称,157 "path": å¾çè·¯å¾,158 "probs": å10个类çæ¦ç{159 "ç±»å«1": æ¦ç1, .....160 }161 },162 {},...]163 """164 labels = [item["label"] for item in get_all_labels()]165 result = []166 image_nums = 10167 image_names = np.random.randint(1, 11000, 100)168 for name in image_names:169 item = {}170 item["name"] = str(name)171 item["path"] = "/static/å¦å
/tank_1.jpg"172 item["probs"] = {}173 for label in labels:174 prob = round(np.random.random() * 100, 2)175 item["probs"][label] = prob176 result.append(item)177 content = render_to_string("results_cls.html", {"Results": result})178 return HttpResponse(content)179def process_classify_1_by_1(request):180 images = ["tank_1.jpg", "zhishengji_1.jpg"]181 labels = ["å¦å
", "ç´åæº"]182 probs = [{"å¦å
": 97.35, "导弹": 2.79, "æ¥æª": 1.90, "æææº": 0.09, "æ½æ°´è": 0.03},183 {"ç´åæº": 87.23, "æææº": 1.98, "é·è¾¾": 0.82, "æé·": 0.05, "ææª": 0.02}]184 result = []185 for idx, name in enumerate(images):186 prob = sorted(probs[idx].items(), key=lambda item: item[1], reverse=True)187 content = render_to_string("results_cls_v2.html",188 {"name": name, "path": "/static/{}/{}".format(labels[idx], name), "probs": prob[:5]})189 result.append(content)190 return HttpResponse("".join(result))191 # image = request.POST.get("image", "没æå¾ç")192 # labels = [item["label"] for item in get_all_labels()]193 # probs = {label: round(np.random.random() * 100, 2) for label in labels}194 # probs = sorted(probs.items(), key=lambda item: item[1], reverse=True)195 # content = render_to_string("results_cls_v2.html", {"name": image, "path": "/static/å¦å
/tank_10.jpg", "probs": probs[:5]})196 # return HttpResponse(content)197def process_train(request):198 print(request.POST)199 form = TrainForm(request.POST)200 if form.is_valid():201 epochs = form.cleaned_data['epochs']202 batch_size = form.cleaned_data['batch_size']203 training_rate = form.cleaned_data['training_rate']204 decay_step = form.cleaned_data['decay_step']205 decay_rate = form.cleaned_data['decay_rate']206 layer = form.cleaned_data['layer']207 bottle_nodes = form.cleaned_data['bottle_nodes']208 param_initializer = form.cleaned_data['param_initializer']209 optimizer = form.cleaned_data['optimizer']210 classifier = form.cleaned_data['classifier']211 penalty = form.cleaned_data['penalty']212 penalty_param = form.cleaned_data['penalty_param']213 print("epochs: {}".format(epochs))214 print("batch_size: {}".format(batch_size))215 print("training_rate: {}".format(training_rate))216 print("decay_step: {}".format(decay_step))217 print("decay_rate: {}".format(decay_rate))218 print("layer: {}".format(layer))219 print("bottle_nodes: {}".format(bottle_nodes))220 print("param_initializer: {}".format(param_initializer))221 print("optimizer: {}".format(optimizer))222 print("classifier: {}".format(classifier))223 print("penalty: {}".format(penalty))224 print("penalty_param: {}".format(penalty_param))225 else:226 print("not valid")227 response = {"status": 0}228 # time.sleep(10)229 return JsonResponse(response)230def process_validate(request):231 """232 precision recall f1-score support233 0 1.00 0.99 0.99 88234 1 0.99 0.97 0.98 91235 2 0.99 0.99 0.99 86236 3 0.98 0.87 0.92 91237 4 0.99 0.96 0.97 92238 5 0.95 0.97 0.96 91239 6 0.99 0.99 0.99 91240 7 0.96 0.99 0.97 89241 8 0.94 1.00 0.97 88242 9 0.93 0.98 0.95 92243avg / total 0.97 0.97 0.97 899244Confusion confusion:245[[87 0 0 0 1 0 0 0 0 0]246 [ 0 88 1 0 0 0 0 0 1 1]247 [ 0 0 85 1 0 0 0 0 0 0]248 [ 0 0 0 79 0 3 0 4 5 0]249 [ 0 0 0 0 88 0 0 0 0 4]250 [ 0 0 0 0 0 88 1 0 0 2]251 [ 0 1 0 0 0 0 90 0 0 0]252 [ 0 0 0 0 0 1 0 88 0 0]253 [ 0 0 0 0 0 0 0 0 88 0]254 [ 0 0 0 1 0 1 0 0 0 90]]255 :param request:256 :return:257 """258 response = {"status": 0}259 report_str = ' precision recall f1-score support\n\n 0 1.00 0.99 0.99 88\n 1 0.99 0.97 0.98 91\n 2 0.99 0.99 0.99 86\n 3 0.98 0.87 0.92 91\n 4 0.99 0.96 0.97 92\n 5 0.95 0.97 0.96 91\n 6 0.99 0.99 0.99 91\n 7 0.96 0.99 0.97 89\n 8 0.94 1.00 0.97 88\n 9 0.93 0.98 0.95 92\n\navg / total 0.97 0.97 0.97 899\n'260 report_matrix = [['', 'precision', 'recall', 'f1-score', 'support'],261 ['0', '1.00', '0.99', '0.99', '88'],262 ['1', '0.99', '0.97', '0.98', '91'],263 ['2', '0.99', '0.99', '0.99', '86'],264 ['3', '0.98', '0.87', '0.92', '91'],265 ['4', '0.99', '0.96', '0.97', '92'],266 ['5', '0.95', '0.97', '0.96', '91'],267 ['6', '0.99', '0.99', '0.99', '91'],268 ['7', '0.96', '0.99', '0.97', '89'],269 ['8', '0.94', '1.00', '0.97', '88'],270 ['9', '0.93', '0.98', '0.95', '92'],271 ['total', '0.97', '0.97', '0.97', '899']]272 report = resolve_report(report_str)273 matrix = [[87, 0, 0, 0, 1, 0, 0, 0, 0, 0],274 [ 0, 88, 1, 0, 0, 0, 0, 0, 1, 1],275 [ 0, 0, 85, 1, 0, 0, 0, 0, 0, 0],276 [ 0, 0, 0, 79, 0, 3, 0, 4, 5, 0],277 [ 0, 0, 0, 0, 88, 0, 0, 0, 0, 4],278 [ 0, 0, 0, 0, 0, 88, 1, 0, 0, 2],279 [ 0, 1, 0, 0, 0, 0, 90, 0, 0, 0],280 [ 0, 0, 0, 0, 0, 1, 0, 88, 0, 0],281 [ 0, 0, 0, 0, 0, 0, 0, 0, 88, 0],282 [ 0, 0, 0, 1, 0, 1, 0, 0, 0, 90]]283 labels = [item["label"] for item in get_all_labels()]284 labels.insert(0, "")285 matrix.insert(0, labels)286 for idx, item in enumerate(matrix[1:]):287 item.insert(0, labels[idx+1])288 idx = render_to_string("results_val.html", {"data": report, "is_matrix": False})289 confusion = render_to_string("results_val.html", {"data": matrix, "is_matrix": True})290 response["index"] = idx291 response["confusion"] = confusion...
test_filter_ai_tiles.py
Source:test_filter_ai_tiles.py
...47 # Create an instance of a GET request.48 request = self.factory.get('get_all_labels')49 # an AnonymousUser instance.50 request.user = AnonymousUser()51 response = get_all_labels(request, submission)52 self.assertEqual(response.status_code, 200)53 def test_ai_tiles_no_tile_water(self):54 submission = '{"year": 2010, "label": "water"}'55 sub = json.loads(submission)56 # Create an instance of a GET request.57 request = self.factory.get('get_all_labels')58 # an AnonymousUser instance.59 request.user = AnonymousUser()60 response = get_all_labels(request, submission)61 self.assertEqual(response.status_code, 400)62 def test_ai_tiles_water(self):63 submission = '{"year": 2011, "label": "water"}'64 sub = json.loads(submission)65 # Create an instance of a GET request.66 request = self.factory.get('get_all_labels')67 # an AnonymousUser instance.68 request.user = AnonymousUser()69 response = get_all_labels(request, submission)70 self.assertEqual(response.status_code, 200)71 def test_ai_tiles_no_tile_land(self):72 submission = '{"year": 2011, "label": "land"}'73 sub = json.loads(submission)74 # Create an instance of a GET request.75 request = self.factory.get('get_all_labels')76 # an AnonymousUser instance.77 request.user = AnonymousUser()78 response = get_all_labels(request, submission)79 self.assertEqual(response.status_code, 400)80 def test_ai_tiles_no_tile_building(self):81 submission = '{"year": 2010, "label": "building"}'82 # Create an instance of a GET request.83 request = self.factory.get('get_all_labels')84 # an AnonymousUser instance.85 request.user = AnonymousUser()86 response = get_all_labels(request, submission)87 self.assertEqual(response.status_code, 400)88 def test_ai_tiles_building(self):89 submission = '{"year": 2011, "label": "building"}'90 # Create an instance of a GET request.91 request = self.factory.get('get_all_labels')92 # an AnonymousUser instance.93 request.user = AnonymousUser()94 response = get_all_labels(request, submission)95 self.assertEqual(response.status_code, 200)96 def test_ai_tiles_no_tile_from_label(self):97 submission = '{"year": 2020, "label": "water"}'98 sub = json.loads(submission)99 # Create an instance of a GET request.100 request = self.factory.get('get_all_labels')101 # an AnonymousUser instance.102 request.user = AnonymousUser()103 response = get_all_labels(request, submission)104 self.assertEqual(response.status_code, 400)105 def test_ai_tiles_wrong_label(self):106 submission = '{"year": 2010, "label": "windmill"}'107 sub = json.loads(submission)108 # Create an instance of a GET request.109 request = self.factory.get('get_all_labels')110 # an AnonymousUser instance.111 request.user = AnonymousUser()112 response = get_all_labels(request, submission)113 self.assertEqual(response.status_code, 400)114 def test_ai_tiles_object(self):115 submission = '{"year": 2011, "label": "church"}'116 sub = json.loads(submission)117 # Create an instance of a GET request.118 request = self.factory.get('get_all_labels')119 # an AnonymousUser instance.120 request.user = AnonymousUser()121 response = get_all_labels(request, submission)122 self.assertEqual(response.status_code, 200)123 def test_ai_tiles_no_object(self):124 submission = '{"year": 2010, "label": "church"}'125 sub = json.loads(submission)126 # Create an instance of a GET request.127 request = self.factory.get('get_all_labels')128 # an AnonymousUser instance.129 request.user = AnonymousUser()130 response = get_all_labels(request, submission)...
test_label.py
Source:test_label.py
...7use_labels = True8def test_label_init_exit(mach):9 pass10def test_label_add_remove_simple(mach):11 print("all:", get_all_labels())12 assert 0 == get_num_labels()13 l = add_label(0, 100, "huhu")14 print("label:", l)15 print("all:", get_all_labels())16 assert 1 == get_num_labels()17 remove_label(l)18 print("all:", get_all_labels())19 assert 0 == get_num_labels()20def _create_random(num, size, seed=42):21 seq = list(range(num))22 random.seed(seed)23 random.shuffle(seq)24 labels = [None] * num25 num = 026 for i in seq:27 addr = i * size28 l = add_label(addr, size, "@%d:%d" % (i, num))29 labels[i] = l30 num += 131 # check order32 for i in range(num):33 addr = i * size34 assert labels[i].addr() == addr35 # check size36 assert get_num_labels() == num37 return labels38def _delete_random(labels, seed=21):39 num = len(labels)40 seq = list(range(num))41 random.seed(seed)42 random.shuffle(seq)43 assert get_num_labels() == num44 for i in seq:45 remove_label(labels[i])46 # check final size47 assert get_num_labels() == 048def _check_find(labels):49 num = len(labels)50 size = labels[0].size()51 for i in range(num):52 # begin range53 addr = i * size54 l = find_label(addr)55 assert l == labels[i]56 # end range57 addr = i * size + size - 158 l = find_label(addr)59 assert l == labels[i]60def test_label_stress_add_remove(mach):61 labels = _create_random(2048, 64)62 _check_find(labels)63 _delete_random(labels)64def test_label_stress_add_remove_large(mach):65 labels = _create_random(1024, 128)66 _check_find(labels)67 _delete_random(labels)68def test_label_add_only(mach):69 assert None is get_all_labels()70 assert None is get_page_labels(0)71 assert 0 == get_num_labels()72 assert 0 == get_num_page_labels(0)73 l = add_label(0x0000, 0x2000, "label")74 assert 1 == get_num_labels()75 assert 1 == get_num_page_labels(0)76 assert [l] == get_all_labels()77 assert [l] == get_page_labels(0)78def test_label_add_remove(mach):79 assert None is get_all_labels()80 assert None is get_page_labels(0)81 assert 0 == get_num_labels()82 assert 0 == get_num_page_labels(0)83 l = add_label(0xf000, 0x2000, "label")84 assert 1 == get_num_labels()85 assert 1 == get_num_page_labels(0)86 assert [l] == get_all_labels()87 assert [l] == get_page_labels(0)88 remove_label(l)89 assert 0 == get_num_labels()90 assert 0 == get_num_page_labels(0)91 assert None is get_all_labels()92 assert None is get_page_labels(0)93def test_label_cross_add_only(mach):94 assert None is get_all_labels()95 assert None is get_page_labels(0)96 assert None is get_page_labels(1)97 l = add_label(0xf000, 0x2000, "cross")98 assert 1 == get_num_labels()99 assert [l] == get_all_labels()100 assert [l] == get_page_labels(0)101 assert [l] == get_page_labels(1)102def test_label_cross_add_remove(mach):103 assert None is get_all_labels()104 assert None is get_page_labels(0)105 assert None is get_page_labels(1)106 l = add_label(0xf000, 0x2000, "cross")107 assert 1 == get_num_labels()108 assert [l] == get_all_labels()109 assert [l] == get_page_labels(0)110 assert [l] == get_page_labels(1)111 remove_label(l)112 assert 0 == get_num_labels()113 assert None is get_all_labels()114 assert None is get_page_labels(0)115 assert None is get_page_labels(1)116def test_label_remove_inside(mach):117 labels = _create_random(1024, 128)118 assert 1024 == get_num_labels()119 remove_labels_inside(128, 128 * 2)120 assert 1022 == get_num_labels()121def test_label_find_intersecting(mach):122 labels = _create_random(1024, 128)123 l1 = find_intersecting_labels(128, 128)124 assert l1 == [labels[1]]125 l2 = find_intersecting_labels(128, 129)126 assert l2 == [labels[1], labels[2]]127 l3 = find_intersecting_labels(150, 128)...
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!!