Best Python code snippet using localstack_python
open_html.py
Source:open_html.py
1# -*-coding:utf-8-*-2import os3import uniout4def file_name(file_dir):5 for root, dirs, files in os.walk(file_dir):6 return files7def dir_name(rootDir):8 return [os.path.join(rootDir, filename) for filename in os.listdir(rootDir) if os.path.isdir(os.path.join(rootDir, filename))]9def file_name2(file_dir):10 return [filename for filename in os.listdir(file_dir) if os.path.isfile(os.path.join(file_dir, filename))]11'''12if __name__ == '__main__':13 path_in = unicode('C:/Users/text/Desktop/ä¸å¸å
¬å¸å®ææ¥å/20160331ï¼600236ï¼', "utf-8")14 files = file_name(path_in)15 for single_file in files:16 try:17 f = open(path_in + "/" + single_file)18 except IOError:19 print path_in + "/" + single_file20 continue21 text = f.read()22 ind1 = text.find('<h3 >å段1_ææ¬</h3><p>')23 ind2 = text.find('</p><h3 >æ é¢</h3><p>')24 ind3 = text.find('</p><h3 >æ¶é´</h3><p>')25 ind4 = text.find('</p><h3 >å段2</h3><p>')26 if any(ind == -1 for ind in [ind1, ind2, ind3, ind4]):27 continue28 stock = text[ind1 + 27: ind2]29 title = text[ind2 + 23: ind3].replace("\n", "").replace(" ", "").replace("æ¥çPDFåæ", "")30 date = text[ind3 + 23: ind4]31 str_remove = ["æè¦", "æ´æ£", "è±æç", "è¡¥å
", "å年度"]32 if "年度æ¥å" in title and not any(name in title for name in str_remove):33 # print title34 sign_start1 = ' ç»è¥æ
åµè®¨è®ºä¸åæ\n'35 sign_end1 = '第äºèéè¦äºé¡¹\n'36 sign_start2 = '第åèç»è¥æ
åµè®¨è®ºä¸åæ\n'37 sign_end2 = ' éè¦äºé¡¹\n'38 not_fund = False39 if text.count(sign_start1) == 1:40 ind5 = text.find(sign_start1)41 elif text.count(sign_start2) == 1:42 ind5 = text.find(sign_start2)43 else:44 not_fund = True45 # print single_file, title46 continue47 if text.count(sign_end1) == 1:48 ind6 = text.find(sign_end1)49 elif text.count(sign_end2) == 1:50 ind6 = text.find(sign_end2)51 else:52 not_fund = True53 # print single_file, title54 continue55 if not not_fund:56 content = text[ind5: ind6]57 title = title.replace("*", "_").replace(":", "_").replace("/", "_")58 print title59 with open(unicode("C:/Users/text/Desktop/ä¸å¸å
¬å¸å®ææ¥å/å¹´æ¥ææ¬èé3/"60 + str(date) + "_" + str(title) + ".txt", "utf-8"), "w") as f2:61 f2.write(title + '\n')62 f2.write(stock + '\n')63 f2.write(date + '\n')64 f2.write(content + '\n')65'''66if __name__ == '__main__':67 path_in1 = unicode('D:\æ°æ®\åä¸æ¿å®æå
¬å\åä¸æ¿', "utf-8")68 # print files169 count = 070 num = 071 for single_dir in dir_name(path_in1):72 print single_dir73 for single_file in file_name(single_dir):74 try:75 f = open(single_dir + '/' + single_file)76 except IOError:77 print single_dir + '/' + single_file78 continue79 text = f.read()80 ind1 = text.find('<h3 >å段1</h3><p>')81 ind2 = text.find('</p><h3 >æ é¢</h3><p>')82 ind3 = text.find('</p><h3 >æ¶é´</h3><p>')83 ind4 = text.find('</p><h3 >å段2</h3><p>')84 if any(ind == -1 for ind in [ind1, ind2, ind3, ind4]):85 # print count86 count += 187 continue88 stock = text[ind1 + 23: ind2]89 title = text[ind2 + 23: ind3].replace("\n", "").replace(" ", "").replace("æ¥çPDFåæ", "")90 date = text[ind3 + 23: ind4]91 str_remove = ["æè¦", "æ´æ£", "è±æç", "è¡¥å
", "å年度", "说æ", "åå¹´æ¥"]92 # print stock93 if ("年度æ¥å" in title or "å¹´æ¥" in title) and not any(name in title for name in str_remove):94 content = text[ind4: ]95 title = title.replace("*", "_").replace(":", "_").replace("/", "_")96 stock = stock.replace("*", "+").replace(":", "+").replace("/", "+")97 with open(unicode("D:\ä¸å¸å
¬å¸å®ææ¥å\å¹´æ¥ææ¬èé4/"98 + str(date) + "_" + str(stock) + "_" + str(title) + ".txt", "utf-8"), "w") as f2:99 f2.write(title + '\n')100 f2.write(stock + '\n')101 f2.write(date + '\n')102 f2.write(content + '\n')103 num += 1104 105 print num106 107'''108path_in1 = unicode('C:/Users/text/Desktop/ä¸å¸å
¬å¸å®ææ¥å', "utf-8")109files1 = file_name(path_in1)110# print files1111for single_dir in dir_name(path_in1):112 print single_dir113 for single_file in file_name(single_dir):114 try:115 f = open(single_dir + "/" + single_file)116 except IOError:117 print single_dir + "/" + single_file118 continue119 text = f.read()120 ind1 = text.find('<h3 >å段1_ææ¬</h3><p>')121 ind2 = text.find('</p><h3 >æ é¢</h3><p>')122 ind3 = text.find('</p><h3 >æ¶é´</h3><p>')123 ind4 = text.find('</p><h3 >å段2</h3><p>')124 if any(ind == -1 for ind in [ind1, ind2, ind3, ind4]):125 continue126 stock = text[ind1 + 27: ind2]127 title = text[ind2 + 23: ind3].replace("\n", "").replace(" ", "").replace("æ¥çPDFåæ", "")128 date = text[ind3 + 23: ind4]129 str_remove = ["æè¦", "æ´æ£", "è±æç", "è¡¥å
", "å年度"]130 if ("年度æ¥å" in title or "å¹´å¹´æ¥" in title) and not any(name in title for name in str_remove):131 # print title132 sign_start1 = ' ç»è¥æ
åµè®¨è®ºä¸åæ\n'133 sign_end1 = '第äºèéè¦äºé¡¹\n'134 sign_start2 = '第åèç»è¥æ
åµè®¨è®ºä¸åæ\n'135 sign_end2 = ' éè¦äºé¡¹\n'136 not_fund = False137 if text.count(sign_start1) == 1:138 ind5 = text.find(sign_start1)139 elif text.count(sign_start2) == 1:140 ind5 = text.find(sign_start2)141 else:142 not_fund = True143 # print single_file, title144 continue145 if text.count(sign_end1) == 1:146 ind6 = text.find(sign_end1)147 elif text.count(sign_end2) == 1:148 ind6 = text.find(sign_end2)149 else:150 not_fund = True151 # print single_file, title152 continue153 if not not_fund:154 content = text[ind5: ind6]155 title = title.replace("*", "_").replace(":", "_").replace("/", "_")156 print title157 with open(unicode("C:/Users/text/Desktop/ä¸å¸å
¬å¸å®ææ¥å/å¹´æ¥ææ¬èé2/"158 + str(date) + "_" + str(title) + ".txt", "utf-8"), "w") as f2:159 f2.write(title + '\n')160 f2.write(stock + '\n')161 f2.write(date + '\n')162 f2.write(content + '\n')163'''164'''165if __name__ == '__main__':166 path_in1 = unicode('D:\æ°æ®\æ·±è¯Aè¡å®æå
¬å\å¼ ç´«ä¹¾', "utf-8")167 files1 = file_name(path_in1)168 # print files1169 count = 0170 for single_dir in dir_name(path_in1):171 print single_dir172 for single_file in file_name(single_dir):173 try:174 f = open(single_dir + '/' + single_file)175 except IOError:176 print single_dir + '/' + single_file177 continue178 text = f.read()179 ind1 = text.find('</p><h3 >å段1</h3><p>')180 ind2 = text.find('<h3 >æ é¢')181 ind3 = text.find('</p><h3>æ¶é´</h3><p>')182 ind4 = text.find('</p><h3 >å段2</h3><p>')183 ind5 = text[ind3 + 16: ind3 + 50].find('æ¥')184 print text[ind1 + 24: ind4].replace("\n", "").replace(" ", "")185 print text[ind2 + 19: ind1].replace("\n", "").replace(" ", "").replace("æ¥çPDFåæ", "")186 print text[ind3: ind5]187 if any(ind == -1 for ind in [ind1, ind2, ind3, ind4]):188 print count189 count += 1190 continue191 stock = text[ind1 + 27: ind2]192 title = text[ind2 + 19: ind1].replace("\n", "").replace(" ", "").replace("æ¥çPDFåæ", "")193 date = text[ind3 + 9: ind3 + 13 + 9]194 str_remove = ["æè¦", "æ´æ£", "è±æç", "è¡¥å
", "å年度", "说æ", "åå¹´æ¥"]195 # print stock196 if ("年度æ¥å" in title or "å¹´æ¥" in title) and not any(name in title for name in str_remove):197 # print title198 content = text[ind4: ]199 title = title.replace("*", "_").replace(":", "_").replace("/", "_")200 stock = stock.replace("*", "+").replace(":", "+").replace("/", "+")201 with open(unicode("C:/Users/text/Desktop/ä¸å¸å
¬å¸å®ææ¥å/å¹´æ¥ææ¬èé4/"202 + str(date) + "_" + str(title) + ".txt", "utf-8"), "w") as f2:203 f2.write(title + '\n')204 f2.write(stock + '\n')205 f2.write(date + '\n')206 f2.write(content + '\n')...
temp_Main.py
Source:temp_Main.py
1import csv2import sqlite33import tkinter as tk4from tkinter import filedialog5#flag in order to know when to finish the program6flag=17while flag!='0':8 print("____________________________________________________________________")9 print()10 print("This is a menu for NMEA convertor")11 print()12 name = input("Please insert your name")13 print()14 print("Hello" ,name , "Please choose the .nmea file")15 root = tk.Tk()16 root.withdraw()17 file_path = filedialog.askopenfilename()18 #create database19 with open(file_path, 'r') as input_file:20 reader = csv.reader(input_file)21 conn = sqlite3.connect('nmea_to_db.db')22 c = conn.cursor()23 c.execute('DROP TABLE IF EXISTS info')24 # flag will tell us if the GPGGA is good if yes continue to the GPRMC25 flag = 026 # Create table27 c.execute('''CREATE TABLE info28 (date text,time text,speed float, latitude text, latitude_direction text, longitude text, longitude_direction text,fix text,horizontal_dilution text,altitude text,direct_of_altitude text,altitude_location text)''')29 # create a csv reader object from the input file (nmea files are basically csv)30 for row in reader:31 # skip all lines that do not start with $GPGGA32 if not row:33 continue34 elif row[0].startswith('$GPGGA') and not row[6] == '0':35 time = row[1];36 latitude = row[2]37 lat_direction = row[3]38 longitude = row[4]39 lon_direction = row[5]40 fix = row[6]41 horizontal = row[7]42 altitude = row[8]43 direct_altitude = row[9]44 altitude_location = row[10]45 flag = 146 elif row[0].startswith('$GPRMC') and flag == 1:47 speed = row[7]48 date = row[9]49 warning = row[2]50 if warning == 'V':51 continue52 c.execute("INSERT INTO info VALUES (?,?,?,?,?,?,?,?,?,?,?,?)", (53 date, time, speed, latitude, lat_direction, longitude, lon_direction, fix, horizontal, altitude,54 direct_altitude, altitude_location))55 # Save (commit) the changes56 conn.commit()57 flag = 058 else:59 continue60 # We can also close the connection if we are done with it.61 # Just be sure any changes have been committed or they will be lost.62 conn.close()63 #end create database64 #ask the user what data he want to see65 print()66 print("What data do you want to see? - for defult leave it blank,if you dont want see something pleae write it in 1 line")67 str_remove = input("date, time, speed, latitude, lat_direction, longitude, lon_direction, fix, horizontal, altitude,direct_altitude, altitude_location")68 #change the data base according to what the user enter69#******************************70 if str_remove!="":71 conn = sqlite3.connect('nmea_to_db.db')72 c = conn.cursor()73 c.execute('DROP TABLE IF EXISTS info')74 # Create table75 c.execute('''CREATE TABLE info''')76 if "date" in str_remove:77 print("remove date")78 if "time" in str_remove:79 print("remove time")80 if "speed" in str_remove:81 print("remove speed")82 if "latitude" in str_remove:83 print("remove latitude")84 if "lat_direction" in str_remove:85 print("remove lat_direction")86 if "longitude" in str_remove:87 print("remove longitude")88 if "lon_direction" in str_remove:89 print("remove lon direction")90 if "fix" in str_remove:91 print("remove fix")92 if "horizontal" in str_remove:93 print("remove horizontal")94 if "altitude" in str_remove:95 print("remove altitude")96 if "direct_altitude" in str_remove:97 print("remove direct altitude")98 if "altitude_location" in str_remove:99 print("remove altitude location")100#********************************************************101 #ask the user to what he want to convert it102 print()103 print("What would you like to do? ")104 choice = input("If you want to convert to KML please insert 1, if you want to convert to CSV please insert 2")105 # convert db to kml106 if choice=='1':107 InputFile = "C://Users/danie/PycharmProjects/untitled2/Code/nmea_to_db.db"108 OutputFile = "C://Users/danie/PycharmProjects/untitled2/Code/nmea_to_kml.kml"109 connection = sqlite3.connect(InputFile)110 cursor = connection.cursor()111 cursor.execute("SELECT * FROM info")112 result = cursor.fetchall()113 f = open(OutputFile, "w")114 # Writing the kml file.115 f.write("<?xml version='1.0' encoding='UTF-8'?>\n")116 f.write("<kml xmlns='http://earth.google.com/kml/2.1'>\n")117 f.write("<Document>\n")118 f.write(" <name>" + 'to_kml' + '.kml' + "</name>\n")119 for row in result:120 f.write(" <Placemark>\n")121 f.write(" <name>" + str(row[1]) + "</name>\n")122 f.write(" <description>" + str(row[0]) + "</description>\n")123 day = str(row[0][:2])124 month = str(row[0][2:4])125 year = str(row[0][4:6])126 if (float(year) > 30):127 Date = "19" + year + "-" + month + "-" + day128 elif (float(year) <= 30):129 Date = "20" + year + "-" + month + "-" + day130 hour = str(row[1][:2])131 minute = str(row[1][2:4])132 second = str(row[1][4:6])133 Time = hour + ":" + minute + ":" + second134 f.write(" <TimeStamp>\n" + "<when>" + Date + "T" + Time + "Z</when> \n</TimeStamp>\n")135 f.write(" <Point>\n")136 # print("lat=" + row[3])137 # print("lon=" + row[5])138 lon = str(float(row[5][:3]) + (float(row[5][3:]) / 60))139 lat = str(float(row[3][:2]) + (float(row[3][2:]) / 60))140 # print(lat)141 # print(lon)142 a = float(row[5]) / 100143 b = float(row[3]) / 100144 f.write(" <coordinates>" + lon + "," + lat + "," + str(row[9]) + "</coordinates>\n")145 f.write(" </Point>\n")146 f.write(" </Placemark>\n")147 f.write("</Document>\n")148 f.write("</kml>\n")149 cursor.close()150 f.close()151 connection.close()152 #nmea to csv153 if choice == '2':154 INPUT_FILE = "C://Users/danie/PycharmProjects/untitled2/Code/nmea_to_db.db"155 OUTPUT_FILE = "C://Users/danie/PycharmProjects/untitled2/Code/nmea_to_csv.csv"156 file_name = open(OUTPUT_FILE, 'w', newline='')157 c = csv.writer(file_name)158 c.writerow(159 ['Date', 'Time', 'Speed', 'Latitude', 'Lat_direction', 'Longitude', 'Lon_direction', 'Fix', 'Horizontal',160 'Altitude', 'Direct_altitude', 'Altitude_location'])161 connection = sqlite3.connect(INPUT_FILE)162 cursor = connection.cursor()163 cursor.execute('SELECT * FROM info')164 result = cursor.fetchall()165 for r in result:166 c.writerow(r)167 cursor.close()168 file_name.close()169 connection.close()170 flag = input("if you would like quit please press 0")...
utils.py
Source:utils.py
1"""Utility module"""2from datetime import datetime3import json4def my_import(name):5 """Function converts a string based import into a module object, used for6 dynamically importing modules from config.7 Parameters8 ----------9 :str: `str`10 A string name of the module to import11 Returns12 -------13 :obj: `module`14 A module object converted from the string.15 """16 components = name.split('.')17 mod = __import__(components[0])18 for comp in components[1:]:19 mod = getattr(mod, comp)20 return mod21def get_date_based_folder():22 """Function to return the folder path based on todays date.23 Parameters24 ----------25 None26 Returns27 -------28 str: `str`29 A string that contains the current date YYYY/MM/DD30 """31 int_date = str(datetime.utcnow().date())32 return int_date.replace('-', '/')33def check_new_day(folder_date):34 """Function that checks what the current date is and determines if a new35 folder is required to be made"""36 return datetime.utcnow().date() != folder_date37def generate_event(file_name, datetime=datetime):38 """Function to generate json with a timestamp and filname headers.39 """40 return json.dumps({'timestamp': datetime.now().isoformat(),41 'filename': file_name})42def chop_end_of_string(str_input, str_remove):43 """Function that strips the supplied str_remove from the end of the input44 string45 Parameters46 ----------47 str_input: `str`48 A string to be chopped49 str_remove: `str`50 The string to be removed from the end of the input51 Returns52 -------53 str: `str`54 A string that contains the new string55 """56 if str_input.endswith(str_remove):57 return str_input[:-len(str_remove)]...
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!!