Best Python code snippet using toolium_python
fileio.py
Source:fileio.py
1import psycopg2 as pg2import psycopg2.extras3import csv4import os56def read_csv(filepath):7 #filepath = 'assignment\\Fire_corp.csv'8 read_file = open(filepath, encoding = 'utf-8')9 reader = csv.reader(read_file, delimiter=',')10 result = []11 for row in reader:12 print(f"{row} \n", end='')13 result.append(row)1415 16 read_file.close()1718def write_csv(filepath, row):19 write_file = open(filepath,'a', encoding = 'utf-8',newline='')20 writer = csv.writer(write_file)21 row = row.rstrip('\n')22 row = row.split(',')2324 writer.writerow(row)2526 write_file.close()2728def filewrite_csv(filepath, rows):29 read_file = open(filepath, encoding = 'utf-8')30 reader = csv.reader(read_file, delimiter = ',')31 #rows = rows.rstrip('\n')32 #rows = rows.split(',')3334 for row in reader:35 if row[1] == rows[1]:36 print(row[1]+"ë ì´ë¯¸ ì ì¥ë ì°ë½ì²ì
ëë¤")37 break3839 else:40 41 write_file = open(filepath,'a', encoding = 'utf-8',newline='')42 writer = csv.writer(write_file)4344 45 writer.writerow(rows)4647 write_file.close()48 break4950 read_file.close()5152def changeadmin_csv(filepath,sid, rows):53 read_file = open(filepath, encoding = 'utf-8')54 reader = csv.reader(read_file, delimiter = ',')55 write_file = open('temp.csv','a', encoding='utf-8', newline='')56 writer = csv.writer(write_file)57 5859 for row in reader:60 if sid == row[0]:61 rows = sid+','+rows62 rows = rows.rstrip('\n')63 rows = rows.split(',')64 writer.writerow(rows)6566 else:67 writer.writerow(row)6869 read_file.close()70 write_file.close()7172 73 os.remove(filepath)7475 os.rename('temp.csv',filepath)7677def change_csv(filepath, rows):78 read_file = open(filepath, encoding = 'utf-8')79 reader = csv.reader(read_file, delimiter = ',')80 write_file = open('temp.csv','a', encoding='utf-8', newline='')81 writer = csv.writer(write_file)82 rows = rows.rstrip('\n')83 rows = rows.split(',')8485 for row in reader:86 if row[0] == rows[0]:87 write_file.writer(rows)8889 else:90 write_file.writer(row)9192 read_file.close()93 write_file.close()9495 96 os.remove(filepath)9798 os.rename('temp.csv',filepath)99 100def delete_csv(filepath,sid):101 read_file = open(filepath, encoding = 'utf-8')102 reader = csv.reader(read_file, delimiter = ',')103 write_file = open('temp.csv','a', encoding='utf-8', newline='')104 writer = csv.writer(write_file)105 #rows = rows.rstrip('\n')106 #rows = rows.split(',')107108 for row in reader:109 if sid != row[0]:110 writer.writerow(row)111112113114 read_file.close()115 write_file.close()116117 os.remove(filepath)118119 os.rename('temp.csv',filepath)120121def login(id, passwd):122 read_file = open('students.csv', encoding = 'utf-8')123 reader = csv.reader(read_file, delimiter = ',')124125 cnt = 0126 for row in reader:127 if cnt == 0:128 cnt = cnt + 1129 130 131 else:132 row[0] = row[0].rstrip(' ')133 row[1] = row[1].rstrip(' ')134135 if row[0] == id and row[1] == passwd:136 return 1137 138 else:139 return 0140141 read_file.close()142143def countDomain():144 read_file = open('íìì¸ì£¼ìë¡.csv', encoding = 'utf-8')145 reader = csv.reader(read_file, delimiter = ',')146147 domain = []148 cnt = []149150151 152153 for row in reader:154 d = row[2].split('@')155 156 if len(d) == 1:157 continue158159 elif domain.count(d[1])==0:160 domain.append(d[1])161 cnt.append(1)162163164 elif domain.count(d[1])!=0:165 cnt[domain.index(d[1])] += 1166167 print("ëë©ì¸, ê°ì")168 for i in range(len(domain)):169 print(domain[i], cnt[i])170171172if __name__ == "__main__":173 id = ''174 while id != 'quit':175 id = input("ID를 ì
ë ¥íì¸ì : ")176 if id == 'admin':177 command = ''178 while command != "lgout":179 command = input("ì°ë½ì² ì¶ê°, ë³ê²½, ìì , ì´ë ì¤ íë를 ì ííì¸ì : ")180 if command == "add":181 row = input("ì¶ê°í íì ì 보를 ì
ë ¥íì¸ì : ")182 write_csv('students.csv',row)183 read_csv('students.csv')184185186 rows = input("íìì íë², ì°ë½ì², ì´ë©ì¼ì ì
ë ¥íì¸ì : ")187 write_csv('íìì¸ì£¼ìë¡.csv',rows)188 read_csv('íìì¸ì£¼ìë¡.csv')189 190 elif command == "change":191 sname = input("íì ì´ë¦ì ì
ë ¥íì¸ì : ")192 read_file = open('students.csv', encoding = 'utf-8')193 reader = csv.reader(read_file, delimiter=',')194195 for row in reader:196 if row[2] == sname:197 rows = input("ì ì¥í ì°ë½ì², ì´ë©ì¼ì ì
ë ¥íì¸ì : ")198 changeadmin_csv('íìì¸ì£¼ìë¡.csv',row[0], rows)199 read_file.close()200 break201 202 read_csv('íìì¸ì£¼ìë¡.csv') 203204 elif command == "delete":205 sname = input("íì ì´ë¦ì ì
ë ¥íì¸ì : ")206 read_file = open('students.csv', encoding = 'utf-8')207 reader = csv.reader(read_file, delimiter=',')208209 for row in reader:210 if row[2] == sname:211 delete_csv('íìì¸ì£¼ìë¡.csv', row[0])212 read_file.close()213 break214 215 read_csv('íìì¸ì£¼ìë¡.csv')216217 elif command == "open":218 print("주ìë¡ ëª©ë¡ : ")219 for file in os.listdir('./'):220 if '.csv' in file:221 print(f"{file}")222223 filename = input("ì´ëí 주ìë¡ì ì ííì¸ì : ")224225 while 1:226 try:227 read_csv(filename)228 breaã
229 except FileNotFoundError:230 filename = input("íì¼ ì´ë¦ì ë¤ì ì
ë ¥íì¸ì : ")231232 elif command == "email address":233 countDomain()234 235 elif id != 'quit':236 passwd = input("ë¹ë°ë²í¸ë¥¼ ì
ë ¥íì¸ì : ")237 if login(id, passwd) :238 command = ''239 while command != "lgout":240 command = input("주ìë¡ ì´ë, ì°ë½ì² ì¶ê°, ë³ê²½, ìì ì¤ íë를 ì ííì¸ì : ")241 if command == "open":242 read_csv('íìì¸ì£¼ìë¡.csv')243 try:244 read_csv(id+'.csv')245 except FileNotFoundError:246 print("ê°ì¸ 주ìë¡ì ì무 ì ë³´ê° ììµëë¤.")247 248 elif command == "add":249 info = input("ì¶ê°í ì ë³´ ëë 주ìë¡ íì¼ ì´ë¦ì ì
ë ¥íì¸ì : ")250 if ".csv" in info:251 read_file = open(info, encoding = 'utf-8')252 reader = csv.reader(read_file, delimiter=',')253 for row in reader:254 filewrite_csv(id+'.csv',row)255256 read_file.close()257 else:258 write_csv(id+'.csv',info)259 260 read_csv(id+'.csv')261 262 elif command == "change":263 sid = input("íë²ì ì
ë ¥íì¸ì : ")264 read_file = open(id+'.csv', encoding = 'utf-8')265 reader = csv.reader(read_file, delimiter=',')266267 for row in reader:268 if row[0] == sid:269 rows = input("ì ì¥í ì°ë½ì², ì´ë©ì¼ì ì
ë ¥íì¸ì : ")270 rows = sid + rows271 change_csv(id+'.csv',rows)272 read_csv(id+'.csv')273 read_file.close()274 break275276 elif command == "delete":277 sid = input("íë²ì ì
ë ¥íì¸ì : ")278 delete_csv(id+'.csv',sid)279 read_csv(id+'.csv')280 else:281 print("ë¡ê·¸ì¸ ì ë³´ê° íë ¸ìµëë¤.")
...
tests.py
Source:tests.py
1import unittest2from parse import outputing3from parse import pretty_printing4class Tests1(unittest.TestCase):5 def test1(self):6 read_file = open('tests/test1.txt', 'r')7 text = read_file.read()8 read_file.close()9 result = outputing(text)10 read_file = open('tests/res1.txt', 'r')11 answer = read_file.read()12 read_file.close()13 self.assertEqual(str(result), answer)14 pretty_print = pretty_printing(result)15 read_file = open('tests/prettyprinting1.txt', 'r')16 answer = read_file.read()17 read_file.close()18 self.assertEqual(pretty_print, answer)19 def test2(self):20 read_file = open('tests/test2.txt', 'r')21 text = read_file.read()22 read_file.close()23 result = outputing(text)24 read_file = open('tests/res2.txt', 'r')25 answer = read_file.read()26 read_file.close()27 self.assertEqual(str(result), answer)28 pretty_print = pretty_printing(result)29 read_file = open('tests/prettyprinting2.txt', 'r')30 answer = read_file.read()31 read_file.close()32 self.assertEqual(pretty_print, answer)33 def test3(self):34 read_file = open('tests/test3.txt', 'r')35 text = read_file.read()36 read_file.close()37 result = outputing(text)38 read_file = open('tests/res3.txt', 'r')39 answer = read_file.read()40 read_file.close()41 self.assertEqual(str(result), answer)42 pretty_print = pretty_printing(result)43 read_file = open('tests/prettyprinting3.txt', 'r')44 answer = read_file.read()45 read_file.close()46 self.assertEqual(pretty_print, answer)47 48 def test4(self):49 read_file = open('tests/test4.txt', 'r')50 text = read_file.read()51 read_file.close()52 result = outputing(text)53 read_file = open('tests/res4.txt', 'r')54 answer = read_file.read()55 read_file.close()56 self.assertEqual(str(result), answer)57 pretty_print = pretty_printing(result)58 read_file = open('tests/prettyprinting4.txt', 'r')59 answer = read_file.read()60 read_file.close()61 self.assertEqual(pretty_print, answer)62 def test5(self):63 read_file = open('tests/test5.txt', 'r')64 text = read_file.read()65 read_file.close()66 result = outputing(text)67 read_file = open('tests/res5.txt', 'r')68 answer = read_file.read()69 read_file.close()70 self.assertEqual(str(result), answer)71 pretty_print = pretty_printing(result)72 read_file = open('tests/prettyprinting5.txt', 'r')73 answer = read_file.read()74 read_file.close()75 self.assertEqual(pretty_print, answer)76 def test6(self):77 read_file = open('tests/test6.txt', 'r')78 text = read_file.read()79 read_file.close()80 result = outputing(text)81 read_file = open('tests/res6.txt', 'r')82 answer = read_file.read()83 read_file.close()84 self.assertEqual(str(result), answer)85 pretty_print = pretty_printing(result)86 read_file = open('tests/prettyprinting6.txt', 'r')87 answer = read_file.read()88 read_file.close()89 self.assertEqual(pretty_print, answer)90 def test7(self):91 read_file = open('tests/test7.txt', 'r')92 text = read_file.read()93 read_file.close()94 result = outputing(text)95 read_file = open('tests/res7.txt', 'r')96 answer = read_file.read()97 read_file.close()98 self.assertEqual(str(result), answer)99 pretty_print = pretty_printing(result)100 read_file = open('tests/prettyprinting7.txt', 'r')101 answer = read_file.read()102 read_file.close()103 self.assertEqual(pretty_print, answer)104 def test8(self):105 read_file = open('tests/test8.txt', 'r')106 text = read_file.read()107 read_file.close()108 result = outputing(text)109 read_file = open('tests/res8.txt', 'r')110 answer = read_file.read()111 read_file.close()112 self.assertEqual(str(result), answer)113 pretty_print = pretty_printing(result)114 read_file = open('tests/prettyprinting8.txt', 'r')115 answer = read_file.read()116 read_file.close()117 self.assertEqual(pretty_print, answer)118 def test9(self):119 read_file = open('tests/test9.txt', 'r')120 text = read_file.read()121 read_file.close()122 result = outputing(text)123 read_file = open('tests/res9.txt', 'r')124 answer = read_file.read()125 read_file.close()126 self.assertEqual(str(result), answer)127 pretty_print = pretty_printing(result)128 read_file = open('tests/prettyprinting9.txt', 'r')129 answer = read_file.read()130 read_file.close()131 self.assertEqual(pretty_print, answer)132 def test10(self):133 read_file = open('tests/test10.txt', 'r')134 text = read_file.read()135 read_file.close()136 result = outputing(text)137 read_file = open('tests/res10.txt', 'r')138 answer = read_file.read()139 read_file.close()140 self.assertEqual(str(result), answer)141 pretty_print = pretty_printing(result)142 read_file = open('tests/prettyprinting10.txt', 'r')143 answer = read_file.read()144 read_file.close()145 self.assertEqual(pretty_print, answer)146 def test11(self):147 read_file = open('tests/test11.txt', 'r')148 text = read_file.read()149 read_file.close()150 result = outputing(text)151 read_file = open('tests/res11.txt', 'r')152 answer = read_file.read()153 read_file.close()154 self.assertEqual(str(result), answer)155 pretty_print = pretty_printing(result)156 read_file = open('tests/prettyprinting11.txt', 'r')157 answer = read_file.read()158 read_file.close()159 self.assertEqual(pretty_print, answer)160 def test12(self):161 read_file = open('tests/test12.txt', 'r')162 text = read_file.read()163 read_file.close()164 result = outputing(text)165 read_file = open('tests/res12.txt', 'r')166 answer = read_file.read()167 read_file.close()168 self.assertEqual(str(result), answer)169 pretty_print = pretty_printing(result)170 read_file = open('tests/prettyprinting12.txt', 'r')171 answer = read_file.read()172 read_file.close()173 self.assertEqual(pretty_print, answer)174if __name__ == '__main__':...
trumpclinton.py
Source:trumpclinton.py
...59 except:60 pass61 file.close()62 return;63def read_file(date):64 filelocation = "http://data.gdeltproject.org/gdeltv2/" + str(date) + ".mentions.CSV.zip"65 zipfilename = str(date) + ".mentions.CSV"66 from StringIO import StringIO67 from zipfile import ZipFile68 from urllib2 import urlopen69 print "now attempting to access GDELT file " + str(date)70 url = urlopen(filelocation, timeout = 5)71 zipfile = ZipFile(StringIO(url.read()))72 lines = zipfile.open(zipfilename).readlines()73 dateoffile = date74 print "accessed GDELT file " + str(date)75 websiteslist = list()76 for i,l in enumerate(lines):77 lineelements = l.split("\t")78 websiteurl = lineelements[5]79 websiteurl = websiteurl.strip()80 websiteslist.append(websiteurl)81 uniquewebsiteslist = set(websiteslist) #removing non-unique options82 #converting back to list object83 finallist = list()84 for i in uniquewebsiteslist:85 finallist.append(i)86 os.makedirs(date)87 pool = Pool() #uses all available Cores if set to empty. specify number of cores as argument if necessary88 pool.map(read_url, itertools.izip(finallist,itertools.repeat(websiteslist),itertools.repeat(dateoffile)))89 pool.close()90 pool.join()91 return;92def main():93 ##testing on sample file94 #done: 2016010100000095 #done: 2016020100000096 #done: 2016030100000097 #done: 2016040100000098 #done: 2016050100000099 #done: 20160601000000100 #done: 20160701000000101 #done: 20160801000000102 #done: 20160901000000103 # read_file('20160115000000')104 # read_file('20160215000000')105 # read_file('20160315000000')106 # read_file('20160415000000')107 # read_file('20160515000000')108 # read_file('20160615000000')109 # read_file('20160715000000')110 # read_file('20160815000000')111 # read_file('20160915000000')112 # read_file('20161019001500')113 # read_file('20161019003000')114 # read_file('20161019004500')115 # read_file('20161019010000')116 # read_file('20161019011500')117 # read_file('20161019013000')118 # read_file('20161019014500')119 # read_file('20161019020000')120 # read_file('20161019021500')121 # read_file('20161019023000')122 # read_file('20161019024500')123 # read_file('20161019030000')124 # read_file('20161019031500')125 # read_file('20161019033000')126 # read_file('20161019034500')127 # read_file('20161019040000')128 # read_file('20161019041500')129 # read_file('20161019043000')130 # read_file('20161019044500')131 # read_file('20161019050000')132 # read_file('20161019051500')133 # read_file('20161019053000')134 # read_file('20161019054500')135 # read_file('20161019060000')136 # read_file('20161019061500')137 # read_file('20161019063000')138 # read_file('20161019064500')139 # read_file('20161019070000')140 # read_file('20161019071500')141 # read_file('20161019073000')142 # read_file('20161019074500')143 # read_file('20161019080000')144 # read_file('20161019081500')145 # read_file('20161019083000')146 # read_file('20161019084500')147 # read_file('20161019090000')148 # read_file('20161019091500')149 # read_file('20161019093000')150 # read_file('20161019094500')151 # read_file('20161019100000')152 # read_file('20161019101500')153 # read_file('20161019103000')154 # read_file('20161019104500')155 # read_file('20161019110000')156 # read_file('20161019111500')157 # read_file('20161019113000')158 # read_file('20161019114500')159 # read_file('20161019120000')160 # read_file('20161019121500')161 # read_file('20161019123000')162 # read_file('20161019124500')163 # read_file('20161019130000')164 # read_file('20161019131500')165 # read_file('20161019133000')166 # read_file('20161019134500')167 # read_file('20161019140000')168 # read_file('20161019141500')169 # read_file('20161019143000')170 # read_file('20161019144500')171 # read_file('20161019150000')172 # read_file('20161019151500')173 # read_file('20161019153000')174 # read_file('20161019154500')175 # read_file('20161019160000')176 # read_file('20161019161500')177 # read_file('20161019163000')178 # read_file('20161019164500')179 # read_file('20161019170000')180 # read_file('20161019171500')181 # read_file('20161019173000')182 # read_file('20161019174500')183 # read_file('20161019180000')184 # read_file('20161019181500')185 # read_file('20161019183000')186 # read_file('20161019184500')187 # read_file('20161019190000')188 # read_file('20161019191500')189 # read_file('20161019193000')190 # read_file('20161019194500')191 # read_file('20161019200000')192 # read_file('20161019201500')193 # read_file('20161019203000')194 # read_file('20161019204500')195 # read_file('20161019210000')196 # read_file('20161019211500')197 # read_file('20161019213000')198 # read_file('20161019214500')199 # read_file('20161019220000')200 # read_file('20161019221500')201 read_file('20161019223000')202 # read_file('20161019224500')203 # read_file('20161019230000')204 # read_file('20161019231500')205 # read_file('20161019233000')206 # read_file('20161019234500')207 # read_file('20161020000000')208 return;209if __name__ == '__main__':...
build_MBRS_sc_times_plots.py
Source:build_MBRS_sc_times_plots.py
1'''Script to build plots for time measurements from LB_Keough and DTW combination given a query'''2import matplotlib.pyplot as plt3import pandas as pd4# 3D plot5read_file = pd.read_csv (r'DTW_lb_times.csv')6fig = plt.figure()7ax = plt.axes(projection ='3d')8ax.scatter3D(read_file["Sakoe_Chiba"], read_file["N"], read_file["Time (s)"])9plt.savefig('./plots/3D_MBRs_sc.png', format='png', bbox_inches='tight')10# 2D plots11sc_1 = 712sc_2 = 1313sc_3 = 1914sc_4 = 315plt.figure()16# add comparison with DTW only17plt.plot([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], [4680, 4680, 4680, 4680, 4680, 4680, 4680, 4680, 4680, 4680, 4680, 4680, 4680, 4680, 4680, 4680, 4680, 4680, 4680, 4680], label = "DTW only")18plt.plot(read_file["N"][read_file["Sakoe_Chiba"].isin([sc_4])], read_file["Time (s)"][read_file['Sakoe_Chiba'].isin([sc_4])], label = "Sakoe-Chiba = 3")19plt.plot(read_file["N"][read_file["Sakoe_Chiba"].isin([sc_1])], read_file["Time (s)"][read_file['Sakoe_Chiba'].isin([sc_1])], label = "Sakoe-Chiba = 7")20plt.plot(read_file["N"][read_file["Sakoe_Chiba"].isin([sc_2])], read_file["Time (s)"][read_file['Sakoe_Chiba'].isin([sc_2])], label = "Sakoe-Chiba = 13")21plt.plot(read_file["N"][read_file["Sakoe_Chiba"].isin([sc_3])], read_file["Time (s)"][read_file['Sakoe_Chiba'].isin([sc_3])], label = "Sakoe-Chiba = 19")22plt.legend()23plt.ylabel('Time (s)')24plt.xlabel('MBRs')25plt.title("Time VS MBRs")26plt.savefig('./plots/MBRs_with_set_sc.png', format='png', bbox_inches='tight')27N_1 = 1028N_2 = 1529N_3 = 1930N_4 = 531plt.figure()32# add comparison with DTW only33plt.plot([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], [4680, 4680, 4680, 4680, 4680, 4680, 4680, 4680, 4680, 4680, 4680, 4680, 4680, 4680, 4680, 4680, 4680, 4680, 4680, 4680], label = "DTW only")34plt.plot(read_file["Sakoe_Chiba"][read_file["N"].isin([N_4])], read_file["Time (s)"][read_file['N'].isin([N_4])], label = "MBRs = 5")35plt.plot(read_file["Sakoe_Chiba"][read_file["N"].isin([N_1])], read_file["Time (s)"][read_file['N'].isin([N_1])], label = "MBRs = 10")36plt.plot(read_file["Sakoe_Chiba"][read_file["N"].isin([N_2])], read_file["Time (s)"][read_file['N'].isin([N_2])], label = "MBRs = 15")37plt.plot(read_file["Sakoe_Chiba"][read_file["N"].isin([N_3])], read_file["Time (s)"][read_file['N'].isin([N_3])], label = "MBRs = 19")38plt.legend()39plt.ylabel('Time (s)')40plt.xlabel('Sakoe-Chiba lengths')41plt.title("Time VS Sakoe-Chiba lengths")...
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!!