Best Python code snippet using avocado_python
shell.py
Source:shell.py
1from django.urls import reverse2from django.test.utils import setup_test_environment3from django.contrib.auth.models import User4from django.db.models import Q5from home.models import *6from home.search import *7# from searchengine.forms import *8# import requests9import os10import sys11import random12import cv213# from tabulate import tabulate14setup_test_environment()15from django.test import Client16c = Client()17from django.core.files import File18def populate_user_database():19 os.chdir('./static/books')20 id_=int(input('Enter ID: '))21 b=Book.objects.get(id=id_)22 os.chdir(b.title)23 os.chdir('content')24 ids=[]25 for file in os.listdir('.'):26 print(file)27 with open(file,'r') as f:28 r=f.readlines()29 r=[i.strip() for i in r]30 content='\n'.join(r)31 p=Page(pagetitle=file.strip(), content=content, book=b)32 p.save()33 ids.append(p.id)34 os.chdir('../images')35 for i in ids:36 pt='.'.join(Page.objects.get(id=i).pagetitle.split('.')[:-1])37 # pt=pt.split('.')[:-1]+'jpg'38 p=Page.objects.get(id=i)39 p.image.save(pt,File(open(pt,'rb')))40def flush_user_database():41 p = Page.objects.all()42 if len(p):43 for i in p:44 i.delete()45 b = Book.objects.all()46 if len(b):47 for i in b:48 i.delete()49def processing():50 import os51 import shutil52 import glob53 import xml.etree.ElementTree as et54 valid_image_ext = ['jpg','tif','jpeg','TIF','JPG','JPEG','PNG','png']55 cwd = os.getcwd()56 os.chdir("/home/ndlsearch19/rename_telugu/Telugu")57 book_path_folder = os.getcwd()58 book_name_lan = os.getcwd().split("/")[-1]59 60 #print(book_path_folder)61 book_list = Book.objects.all()62 test_list=[]63 for i in book_list:64 test_list.append(i.title)65 # print(test_list)66 count = 167 book_path_list = glob.glob('*')68 book_path_list = [i for i in book_path_list if os.path.isdir(i)]69 print(book_path_list)70 # cwd = os.getcwd()71 test=[]72 for book_path in book_path_list:73 print(book_path)74 print("/n/n")75 test_list_content = []76 test_list_images = []77 test_list_segment = []78 nameofthebook = book_path.split("/")[-1]79 #print(nameofthebook)80 os.chdir(book_path_folder+"/"+book_path)81 print(os.getcwd())82 images = []83 for i in valid_image_ext:84 images += glob.glob('Images/*.{}'.format(i))85 images = list(set(images))86 # print(images)87 content = glob.glob('Predictions_CRNN/*.txt')88 # print(content)89 #print(len(content))90 #print(len(images))91 segment = glob.glob('Segmentations/*.txt') 92 for i in images:93 i=i.split(".")[0].split("/")[-1]94 test_list_images.append(i)95 if len(content)!=0 and len(content) is not None: 96 for i in content:97 i = i.split(".")[0].split("/")[-1]98 test_list_content.append(i)99 else:100 print("No predictions")101 for i in segment:102 i = i.split(".")[0].split("/")[-1]103 test_list_segment.append(i)104 105 106 107 common_list = list(set(test_list_content) & (set(test_list_images)))108 # common_list_2 = list(set(test_list_content) & (set(test_list_segment)))109 110 print("common list")111 print(len(common_list))112 dir_name_img = os.getcwd() + "/" + "Images"113 dir_name_cont = os.getcwd() + "/" + "Predictions_CRNN"114 dir_name_seg = os.getcwd() + "/" + "Segmentations"115 116 for i in test_list_images:117 if i not in common_list:118 test_list_images.remove(i)119 for i in test_list_content:120 if i not in common_list:121 test_list_content.remove(i)122 for i in test_list_segment:123 if i not in common_list:124 test_list_segment.remove(i)125 126 test1=[]127 test2=[]128 test3=[]129 for i in test_list_images:130 i = i + "." + "png" 131 test1.append(i)132 for i in test_list_content:133 i = i + "." + "png.txt"134 test2.append(i)135 for i in test_list_segment:136 i = i + "." + "png.lines.txt"137 test3.append(i)138 for i in images : 139 if i.split("/")[-1] not in test1:140 images.remove(i)141 os.remove(os.path.join(dir_name_img,i.split("/")[-1]))142 for i in content : 143 if i.split("/")[-1] not in test2:144 content.remove(i)145 os.remove(os.path.join(dir_name_cont, i.split("/")[-1]))146 for i in segment: 147 if i.split("/")[-1] not in test3:148 segment.remove(i)149 os.remove(os.path.join(dir_name_seg, i.split("/")[-1]))150 print(len(images))151 print(len(content))152 print(len(segment))153 print(count)154 count+=1155 assert len(content) == len(images)156 #print(len(content[0].split('_')))157 if len(content[0].split('_')) > 2:158 content = ['_'.join(i.split('_')[1:]) for i in content]159 #print(content)160 root = et.parse('./META.XML').getroot()161 print(root)162 meta = {i.tag:i.text for i in root}163 164 if "title" not in meta.keys():165 meta["title"] = None166 167 if "totalpages" not in meta.keys():168 meta["totalpages"] = None169 if meta["title"] is None:170 meta["title"] = "NDLI_"+book_name_lan+str(count)171 count+=1172 #print(count)173 meta["title"] = meta["title"].lower()174 meta["title"] = meta["title"].title()175 if str(meta["title"]) in test_list :176 print("Already Present")177 else:178 if "creator" not in meta.keys():179 meta["creator"] = None180 181 if "digitalrepublisher" not in meta.keys():182 meta["digitalrepublisher"]= None183 if 'creator1' not in meta.keys() or meta["creator1"] is None:184 meta["creator1"] = "Anonymous"185 if meta["creator"] is None:186 meta["creator"]= meta["creator1"]187 188 if meta["digitalrepublisher"] is None:189 meta["digitalrepublisher"] = "Digital Library Of India"190 191 if "subject" not in meta.keys():192 meta["subject"] = "Unavailable"193 194 if meta["digitalrepublisher"] == "PAR Informatics, Hyderabad" or meta["digitalrepublisher"]=="UDL T.T.D, TIRUPATHI" or meta["digitalrepublisher"]=="UDL TTD TIRUPATHI" or meta["digitalrepublisher"]=="PAR INFORMATICS,HYDERBAD" or meta["digitalrepublisher"]=="UDL T.T.D.Tirupati" or meta["digitalrepublisher"]=="UDL , T.T.D. TIRUPATI" or meta["digitalrepublisher"]=="UDL TTD TIRUPATI" or meta["digitalrepublisher"]=="PAR INFORMATICS,HYD" or meta["digitalrepublisher"]=="<>" or meta['digitalrepublisher']=="Sarvesh Mishra" or meta["digitalrepublisher"]=="UDL TTD TIRUPATI" or meta["digitalrepublisher"]=="Udl Ttd Tirupathi":195 meta["digitalrepublisher"] = "Others"196 if meta["subject"] is None or meta["subject"]=="<>" or meta['subject'] == "<>" or meta["subject"]=="-" or meta["subject"]=="<enter subject of the book>" or meta["subject"]=="<enter subject of the book>":197 meta["subject"] = "Unavailable"198 if meta["subject"]=="RELIGION" or meta['subject']=="Religion" or meta['subject']=="HINDUISM " or meta["subject"]=="hinduism" or meta["subject"]=="HINDUISM" or meta["subject"]=="RELIGION. THEOLOGY" or meta["subject"]=="Religion. Theology":199 meta["subject"] = "Religion and Theology"200 if meta["subject"]=="Literature" or meta["subject"]=="Litrature" or meta["subject"]=="Nobel" or meta["subject"]=="Novel" or meta["subject"] == "Hindi literature" or meta["subject"]=="Hindi Literature" or meta["subject"]=="LITERATURE" or meta["subject"]=="Hindi Literature" or meta["subject"]=="Literature " or meta["subject"]=="Language" or meta["subject"]=="Drama" or meta["subject"]=='drama' or meta["subject"]=="Hindi drama" or meta["subject"]=="Hindi Drama" or meta["subject"]=="poetry" or meta["subject"]=="lictraturl" or meta["subject"]=="Story" or meta["subject"]=="literature" or meta['subject']=="poetry" or meta["subject"]=="Hindi poetry" or meta["subject"]=="Poetry":201 meta["subject"] = "Language. Linguistics. Literature"202 if meta["subject"]=="Others " or meta["subject"]=="OTHERS":203 meta["subject"]="Digital Library Of India"204 if meta["barcode"] is None:205 meta["barcode"] = "Unavailable"206 207 if meta["totalpages"] is None:208 meta["totalpages"] = "-"209 b = Book(210 isbn= meta['barcode'],211 title=meta['title'].lower().rstrip().title(),212 author=meta['creator'].lower().rstrip().title(),213 language=meta['language'].lower().rstrip().title(),214 genre=meta['subject'].lower().rstrip().title(),215 source=meta['digitalrepublisher'].lower().rstrip().title(),216 numpages = meta["totalpages"]217 )218 shutil.make_archive('Images','zip','.',base_dir='Images')219 shutil.make_archive('Predictions_CRNN','zip','.',base_dir='Predictions_CRNN')220 shutil.make_archive('Segmentations','zip','.',base_dir='Segmentations')221 image_zip = os.path.abspath('Images.zip')222 content_zip= os.path.abspath('Predictions_CRNN.zip')223 segment_zip= os.path.abspath('Segmentations.zip')224 os.chdir(cwd)225 b.book_pdf.save('Images.zip',File(open(image_zip,'rb')), save=False)226 b.book_content.save('Predictions_CRNN.zip', File(open(content_zip,'rb')),save=False)227 b.book_segment.save('Segmentations.zip', File(open(segment_zip,'rb')),save=False)228 os.remove(image_zip)229 os.remove(content_zip)230 os.remove(segment_zip)231 b.save()232 print(meta)233 print(os.getcwd())...
test.py
Source:test.py
...11run = 'test_api'12host = 'http://121.199.9.187:8085'13class TestCase(unittest.TestCase):14 @unittest.skipUnless(run == 'test_list_images', 'reason')15 def test_list_images(self):16 p = processor.AliyunImageServiceProcessor("")17 r = p.queryImages(None, None, None, None, None, None, None, None, None, None, None, None, None,)18 print('test_list_images', r, len(r))19 @unittest.skipUnless(run == 'test_create_image', 'reason')20 def test_create_image(self):21 p = processor.AliyunImageServiceProcessor("")22 r = p.createImage(None, None, 'NewImage', 's-62ev59pgw')23 print('test_create_image', r)24 @unittest.skipUnless(run == 'test_query_image_by_id', 'reason')25 def test_query_image_by_id(self):26 p = processor.AliyunImageServiceProcessor("")27 r = p.queryImageId('coreos681_64_20G_aliaegis_20150618.vhd')28 print('test_query_image_by_id', r)29 @unittest.skipUnless(run == 'test_delete_image_by_id', 'reason')...
main.py
Source:main.py
1from model import Model2from layers.activation import Activation3from layers.max_pooling import MaxPooling4from layers.convolution_layer import ConvolutionLayer5from layers.dense_layer import DenseLayer6from layers.flatten_layer import FlattenLayer7from sklearn.model_selection import train_test_split8import os9os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 10import tensorflow as tf11import numpy as np12from tensorflow import keras13from tensorflow.keras import preprocessing14from tensorflow.keras.preprocessing import image_dataset_from_directory15from tensorflow.keras.preprocessing.image import img_to_array16import pickle17train_ds = image_dataset_from_directory(18 directory='train/',19 labels='inferred',20 label_mode='int',21 batch_size=40,22 shuffle=False,23 image_size=(100, 100))24test = image_dataset_from_directory(25 directory='test/',26 labels='inferred',27 label_mode='int',28 batch_size=40,29 shuffle=False,30 image_size=(100, 100))31train_list_images = []32train_list_labels=[]33for images, labels in train_ds.take(1):34 # print(labels[0].numpy()) 35 for i in range(len(images)):36 train_list_images.append(images[i].numpy()*(1/255))37 train_list_labels.append(labels[i].numpy())38test_list_images = []39test_list_labels=[]40for images, labels in test.take(1):41 # print(labels[0].numpy()) 42 for i in range(len(images)):43 test_list_images.append(images[i].numpy()*(1/255))44 test_list_labels.append(labels[i].numpy())45model = Model()46model.add(ConvolutionLayer(inputs_size=(100,100,3), padding=0, n_filter=2, filter_size=(3,3), n_stride=1))47model.add(Activation())48model.add(MaxPooling((2,2), 2))49model.add(FlattenLayer())50model.add(DenseLayer(units=8, activation='relu'))51model.add(DenseLayer(units=1, activation='sigmoid'))52# for image in list_images:53# prediction = model.forward(image)54# print("prediction : " + str(prediction))55# prediction = model.forward(list_images[0])56from sklearn.metrics import accuracy_score57# split traing 90%58X_train, X_test, y_train, y_test = train_test_split(train_list_images, train_list_labels, test_size=0.1)59model.fit(X_train,y_train,2,3,0.1,0.1)60y_predict = np.zeros(len(y_test))61for i, image in enumerate(X_test):62 predict = model.forward(image)63 if (predict[0] > 0.5):64 y_predict[i] = 165 else:66 y_predict[i] = 067accuracy = accuracy_score(y_test,y_predict)68print(accuracy)69# save the model to disk70filename = 'finalized_model.sav'71pickle.dump(model, open(filename, 'wb'))72 73# load the model from disk74loaded_model = pickle.load(open(filename, 'rb'))75y_predict = np.zeros(len(y_test))76for i, image in enumerate(X_test):77 predict = loaded_model.forward(image)78 if (predict[0] > 0.5):79 y_predict[i] = 180 else:81 y_predict[i] = 082accuracy = accuracy_score(y_test,y_predict)83print(accuracy)84# using data test85y_predict = np.zeros(len(test_list_images))86for i, image in enumerate(test_list_images):87 predict = loaded_model.forward(image)88 if (predict[0] > 0.5):89 y_predict[i] = 190 else:91 y_predict[i] = 092accuracy = accuracy_score(test_list_labels,y_predict)...
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!!