Best Python code snippet using playwright-python
utils.py
Source:utils.py
1# encoding: utf-82"""3@author: renxiangyuan4@contact: xiangyuan_ren@shannonai.com5@file: utils.py6@time: 2020-04-16 11:547"""8import os9import random10import numpy as np11import torch12import sentencepiece as spm13import sentencepiece_pb214import re15filter_set ={16 '142f9472fc', 'eaf2942ee8', '66095230ee', '67ec0f5817', '833e9fb8d7', 'e2abcae593',17 '792063a20e', '780c673bca', '8a5155db71', 'bc84f21e3b', 'c6149b7abf', 'fdb77c3752',18}19new_input_dict = {20 "2cb67e64b4": "Aam these dogs are going to die if somebody doesn`t save them!",21}22new_label_dict = {23 "ef6552e483": "stinks",24 "fee6cd911d": "Wave looks interesting.",25 "bb30655041": "I just noticed that too",26 "170874d02b": "Desktop wallpaper like the inside of a monitor",27 "c5de5361d2": "Does anyone out there want to be REALLY awesome and buy me one of these for my birthday? .",28 '6dbdb64223': 'wish',29 '96ff964db0': 'hate',30 '130945d03c': 'lol',31 '7375cfdd5b': 'blessings',32 '70a0bcd877': 'sad',33 'd6a572f589': 'fab',34 '4b3fd4ec22': 'sad',35 'df398a774e': 'fun',36 '3a906c871f': 'sad.',37 '12f21c8f19': 'LOL',38 'af3fed7fc3': 'miss',39 '1760ca7893': 'best',40 '2863f435bd': 'A little happy',41 'ce69e99e71': 'I`m not sleeping at all',42 'a54d3c2825': 'worth',43 '322b61740c': 'perfect',44 '697887b4a1': 'nice',45 'ee9df322d1': 'Sorry',46 '72cfb17265': 'fail',47 '03f9f6f798': 'I don`t think I`ve ever been so tierd in my life.',48 '8a8c28f5ba': 'amazing',49 '31fa81e0ae': 'fun',50 '19d585c61b': 'sorry',51 '568ad4a905': 'Happy',52 'c400a6bf99': 'wish I could go too',53 '3d1318d372': 'yes now would be good',54 'e9c337f756': 'thanks to you',55 '5419aaf31e': 'nice',56 'ad94c81511': 'hurt',57 'adac9ee2e1': 'so good',58 '915c66ead8': 'I don`t want her dressed up though',59 'ad7be4d16e': 'nice',60 '26dfa4924b': 'Happy',61 '37e710afb0': 'almost better than',62 'e668df7ceb': 'nice',63 'cd5989172a': 'Sorry',64 '2225e0fa43': 'so sad',65 '09d0f8f088': 'wow',66 'ee5eb5337b': 'sad',67 '654d710fce': 'hate',68 '7972092a15': 'Eww',69 '7c1d73feef': 'Blessings and Joy',70 'c1c67d1a99': 'SMILE~life is good!',71 '89545ebb49': 'Have a very Happy Mother`s Day!',72 '8d91c2e24a': 'Happy',73 '8e4bd833da': 'Happy',74 'da48252d73': 'happy',75 '271f782910': 'Happy',76 '29afbab19c': 'Happy',77 '9d786a1526': 'Happy',78 '2780126312': 'Thanks!',79 '1211a1d91f': 'too bad',80 '18180bb2ec': 'Its so pretty',81 '9df7f02404': 'FunForBunny',82}83re_url = re.compile(r'https?://\S+')84def delete_url(text, select_text, sentiment):85 findall = re.findall(re_url, text)86 if len(findall) == 0:87 return text, select_text88# for url_str in findall:89# i_start = text.index(url_str)90# i_end = i_start + len(url_str) - 191# if url_str[-1] in {'.'}:92# i_end -= 193# text = text[:i_start] + text[i_end+1:]94 text = re_url.sub(r'.',text)95 select_text = re_url.sub(r'.',select_text)96 return text, select_text97re_html = re.compile(r'<.*?>')98def delete_html(text, select_text, sentiment, debug=False):99 text = re_html.sub(r'', text)100 select_text = re_html.sub(r'',select_text)101 return text, select_text102re_bracket = re.compile(r'\[.*?\]')103def delete_bracket(text, select_text, sentiment, debug=False):104 text = re_bracket.sub(r'', text)105 select_text = re_bracket.sub(r'',select_text)106 return text, select_text107re_space = re.compile(r'�(?=[vtms]|ll|re)')108def delete_space(text, select_text, sentiment, debug=False):109 if '�' not in text:110 return text, select_text111 text = re_space.sub(r'`', text)112 if '�' in text:113 text = text.replace('�', ' ')114 select_text = re_space.sub(r'`',select_text)115 if '�' in select_text:116 select_text = select_text.replace('�', ' ')117 if select_text[-1] == "ï":118 select_text = select_text[:-1]119 return text, select_text120def clean_item(item):121 id_, text, select_text, sentiment = item[0], item[1], item[2], item[3]122 if id_ in new_input_dict:123 item[1] = new_input_dict[id_]124 if id_ in new_label_dict:125 item[2] = new_label_dict[id_]126 item[1], item[2] = delete_url(item[1], item[2], sentiment)127 item[1], item[2] = delete_html(item[1], item[2], sentiment)128 item[1], item[2] = delete_bracket(item[1], item[2], sentiment)129 item[1], item[2] = delete_space(item[1], item[2], sentiment)130 if item[2] not in item[1]:131 print(id_, sentiment)132 print(text)133 print(select_text)134 print(item[1])135 print(item[2], '\n')136 return item137def post_process_shift(sentiment_val, original_tweet, filtered_output, orig_orig):138 """139 :param sentiment_val:140 :param original_tweet:141 :param filtered_output:142 :param orig_orig: robertaå¤çåçtweet143 :return:144 """145 if sentiment_val == "neutral" or len(original_tweet.split()) < 2 or len(filtered_output) >= 0.7 * len(original_tweet):146 filtered_output = original_tweet147 elif orig_orig is not None and orig_orig.startswith(' ') and sentiment_val != 'neutral':148 idx = orig_orig.find(filtered_output.strip())149 if idx > -1 and orig_orig.lstrip().find(filtered_output.strip()) > 0:150 # tmp = filtered_output151 shift = orig_orig[:idx].split(' ').count('')152 if shift == 2: # ä¸è¬shiftæ¯2çæ¶åå¨åé¢ç´æ¥å 两个char153 filtered_output = orig_orig[idx - 2 :idx] + filtered_output154 else:155 shifted_idx = idx-shift + 1156 filtered_output = orig_orig[shifted_idx: shifted_idx + len(filtered_output) + 1] # TODOï¼1å¯è½æ¯shift - 2157 return filtered_output158def calculate_jaccard_score(159 original_tweet,160 target_string,161 sentiment_val,162 idx_start,163 idx_end,164 offsets,165 io_prob=None,166 verbose=False,167 orig_orig=None):168 if idx_end < idx_start:169 idx_end = idx_start170 # idx_start = idx_end171 # return jaccard(target_string.strip(), original_tweet.strip()), original_tweet172 # if io_prob is not None:173 # # selected_offsets = offsets[np.where(io_prob >= 0.5)]174 # # # for o in selected_offsets:175 # # # if o[1] != 0:176 # # # io_start_index = min(o[0], io_start_index)177 # # # io_end_index = max(o[1], io_end_index)178 # io_idx_list = list(np.where(io_prob >= 0.5)[0])179 # if len(io_idx_list) > 0:180 # io_start_index = min(io_idx_list)181 # io_end_index = max(io_idx_list)182 io_start_index = len(original_tweet)183 io_end_index = -1184 io_tokens = []185 if io_prob is not None:186 selected_offsets = offsets[np.where(io_prob >= 0.5)]187 for o in selected_offsets:188 if o[1] != 0:189 io_tokens.append(original_tweet[o[0]:o[1]].lstrip(' '))190 idx_start = min(idx_start, io_start_index)191 idx_end = max(idx_end, io_end_index)192 filtered_output = ""193 for ix in range(idx_start, idx_end + 1):194 filtered_output += original_tweet[offsets[ix][0]: offsets[ix][1]]195 if (ix + 1) < len(offsets) and offsets[ix][1] < offsets[ix + 1][0]:196 filtered_output += " "197 filtered_output = post_process_shift(sentiment_val, original_tweet, filtered_output, orig_orig)198 if sentiment_val != "neutral" and verbose == True:199 if filtered_output.strip().lower() != target_string.strip().lower():200 print("********************************")201 print(f"Output= {filtered_output.strip()}")202 print(f"Target= {target_string.strip()}")203 print(f"Tweet= {original_tweet.strip()}")204 if io_prob is not None:205 print(f"Io= {io_tokens}")206 print("********************************")207 jac = jaccard(target_string.strip(), filtered_output.strip())208 return jac, filtered_output209def post_process(selected):210 return " ".join(set(selected.lower().split()))211def set_seed(seed):212 random.seed(seed)213 np.random.seed(seed)214 torch.manual_seed(seed)215 # if args.n_gpu > 0:216 # torch.cuda.manual_seed_all(seed)217def jaccard(str1, str2):218 a = set(str1.lower().split())219 b = set(str2.lower().split())220 c = a.intersection(b)221 return float(len(c)) / (len(a) + len(b) - len(c))222class AverageMeter(object):223 def __init__(self):224 self.count = 0225 self.avg = 0.0226 def update(self, num, size=1):227 self.avg = (self.avg * self.count + num * size) / (self.count + size)228 self.count += size229class EarlyStopping:230 def __init__(self, patience=7, mode="max", delta=0.0001):231 self.patience = patience232 self.counter = 0233 self.mode = mode234 self.best_score = None235 self.early_stop = False236 self.delta = delta237 if self.mode == "min":238 self.val_score = np.Inf239 else:240 self.val_score = -np.Inf241 def __call__(self, epoch_score, model, model_path):242 if self.mode == "min":243 score = -1.0 * epoch_score244 else:245 score = np.copy(epoch_score)246 if self.best_score is None:247 self.best_score = score248 self.save_checkpoint(epoch_score, model, model_path)249 elif score < self.best_score + self.delta:250 self.counter += 1251 print('EarlyStopping counter: {} out of {}'.format(self.counter, self.patience))252 if self.counter >= self.patience:253 self.early_stop = True254 else:255 self.best_score = score256 self.save_checkpoint(epoch_score, model, model_path)257 self.counter = 0258 def save_checkpoint(self, epoch_score, model, model_path):259 if epoch_score not in [-np.inf, np.inf, -np.nan, np.nan]:260 print('Validation score improved ({} --> {}). Saving model!'.format(self.val_score, epoch_score))261 torch.save(model.state_dict(), model_path)262 self.val_score = epoch_score263hardcode_dict = {264 '6dbdb64223': 'wish',265 '96ff964db0': 'hate',266 '130945d03c': 'lol',267 '7375cfdd5b': 'blessings',268 '70a0bcd877': 'sad',269 'd6a572f589': 'fab',270 '4b3fd4ec22': 'sad',271 'df398a774e': 'fun',272 '3a906c871f': 'sad.',273 '12f21c8f19': 'LOL',274 'af3fed7fc3': 'miss',275 '1760ca7893': 'best',276 '2863f435bd': 'A little happy',277 'ce69e99e71': 'I`m not sleeping at all',278 'a54d3c2825': 'worth',279 '322b61740c': 'perfect',280 '697887b4a1': 'nice',281 'ee9df322d1': 'Sorry',282 '72cfb17265': 'fail',283 '03f9f6f798': 'I don`t think I`ve ever been so tierd in my life.',284 '8a8c28f5ba': 'amazing',285 '31fa81e0ae': 'fun',286 '19d585c61b': 'sorry',287 '568ad4a905': 'Happy',288 'c400a6bf99': 'wish I could go too',289 '3d1318d372': 'yes now would be good',290 'e9c337f756': 'thanks to you',291 '5419aaf31e': 'nice',292 'ad94c81511': 'hurt',293 'adac9ee2e1': 'so good',294 '915c66ead8': 'I don`t want her dressed up though',295 'ad7be4d16e': 'nice',296 '26dfa4924b': 'Happy',297 '37e710afb0': 'almost better than',298 'e668df7ceb': 'nice',299 'cd5989172a': 'Sorry',300 '2225e0fa43': 'so sad',301 '09d0f8f088': 'wow',302 'ee5eb5337b': 'sad',303 '654d710fce': 'hate',304 '7972092a15': 'Eww',305 '7c1d73feef': 'Blessings and Joy',306 'c1c67d1a99': 'SMILE~life is good!',307 '89545ebb49': 'Have a very Happy Mother`s Day!',308 # '8d91c2e24a': 'Happy',309 # '8e4bd833da': 'Happy',310 # 'da48252d73': 'happy',311 # '271f782910': 'Happy',312 # '29afbab19c': 'Happy',313 # '9d786a1526': 'Happy',314 '2780126312': 'Thanks!',315 '1211a1d91f': 'too bad',316 '18180bb2ec': 'Its so pretty',317 '9df7f02404': 'FunForBunny',318}319class SentencePieceTokenizer:320 def __init__(self, model_path):321 self.sp = spm.SentencePieceProcessor()322 self.sp.load(os.path.join(model_path, "spiece.model"))323 def encode(self, sentence):324 spt = sentencepiece_pb2.SentencePieceText()325 spt.ParseFromString(self.sp.encode_as_serialized_proto(sentence))326 offsets = []327 tokens = []328 for piece in spt.pieces:329 tokens.append(piece.id)330 offsets.append((piece.begin, piece.end))...
test.py
Source:test.py
1import requests2from bs4 import BeautifulSoup3import json4import os5import time6"""7å
è·åç»å½çcookieåå
¥config.jsonæ件é8è¾å
¥å
³é®åï¼é¡µæ°ï¼å³å¯å¼å§ç¬åã9"""10class WeiSpider:11 def __init__(self, word="", page=10, config_filename="config.json"):12 self.headers = {}13 self.word = word14 self.page = page15 with open(config_filename, "r") as f:16 t = json.loads(f.read())17 self.headers["cookie"] = t["cookie"]18 if word != "":19 try:20 os.mkdir("select_text")21 except FileExistsError:22 pass23 with open("select_text/"+self.word+".txt", "w") as fp:24 pass25 def set_word(self, word):26 self.word = word27 with open("select_text/" + self.word + ".txt", "w") as fp:28 pass29 def set_page(self, num):30 self.page = num31 def get_soup(self, url):32 rq = requests.get(url, headers=self.headers)33 return BeautifulSoup(rq.text, "html.parser")34 def cookie(self):35 try:36 title = self.get_soup(("https://weibo.cn/search/mblog")).title.text37 if title !="æç´¢ç»æ":38 print("cookieè¿ææè
cookieé误")39 return False40 else:41 return True42 except:43 print("cookieé误")44 return False45 def run_spider(self):46 if self.cookie():47 try:48 os.mkdir("select_text")49 except FileExistsError:50 pass51 print("=" * 50 + "è·åç½é¡µä¸" + "=" * 50 + "\n\n\n")52 for i in range(1, self.page + 1):53 print("æ£å¨è·åå
³é®å:" + self.word + "第" + str(i) + "页微å\n\n")54 url = "https://weibo.cn/search/mblog?hideSearchFrame=&keyword=" + self.word + "&page=" + str(i) + "&sort=time"55 soup = self.get_soup(url)56 self.writer_soup(soup)57 time.sleep(1)58 print(str(self.page) + "个页é¢è·åå®æ")59 print("=" * 100 + "\n\n\n")60 def writer_soup(self, soup):61 with open("select_text/" + self.word + ".txt", "ab") as fp:62 list_soup = soup.find_all(name='div', attrs={"class": "c"})63 for i in list_soup[6::]:64 try:65 print("-" * 100 + "\n")66 name = i.a.text67 txt = i.find(name="span", attrs={"class": "ctt"}).text68 fp.write(("ç¨æ·åï¼" + name + "\n").encode("utf-8"))69 fp.write((txt + "\n\n\n").encode("utf-8"))70 print("ç¨æ·åï¼" + name + "\n")71 print(txt)72 print("-" * 100 + "\n\n")73 except AttributeError:74 pass75def main():76 word = input("请è¾å
¥æ¥è¯¢å
³é®åï¼")77 page = input("请è¾å
¥é¡µæ°ï¼")78 spider = WeiSpider(word, int(page))79 spider.run_spider()80if __name__ == '__main__':...
findWords.py
Source:findWords.py
1# https://stackabuse.com/read-a-file-line-by-line-in-python/2# https://www.geeksforgeeks.org/python-count-occurrences-of-each-word-in-given-text-file-using-dictionary/3import sys4import os5def select_text(user_input):6 # global files7 # global filepath8 if(isinstance(user_input, int)):9 if user_input in range(1, 7):10 # Element exists fill list, return true11 # print(files[user_input-1])12 # select_text.filepath = files[user_input-1]13 return True14 # else:15 print("Select a text or exit")16 return False17 # else:18 return False19def add_words(user_words):20 if(isinstance(user_words, int)):21 if(int(user_words) <= 0):22 print("Enter positive numbers or exit")23 return False24 # else:25 # i = 026 # elements e.g.["example", "hello", "less"]27 # while(i < int(user_words)):28 # print(i+1, end="- ")29 # find_words.append(str(input()))30 # i+=131 return True32 # else:33 return False34# def dic_filling():35# print(select_text.filepath)36# if not os.path.isfile(select_text.filepath):37def dic_filling(os_path):38 if not os.path.isfile(os_path):39 print("File path {} does not exist. Exiting...".format(os_path))40 sys.exit()41 # print("File path {} does not exist. Exiting...".42 # format(select_text.filepath))43 dic_filling.elements = {}44 # with open(select_text.filepath) as file:45 with open(os_path) as file:46 for line in file:47 record_word_cnt(line.strip().replace(",", "").replace(".", "").split(' '), dic_filling.elements)48 # file.close()49def record_word_cnt(words, elements):50 for word in words:51 if word != '':52 if word in elements:53 dic_filling.elements[word] += 154 else:55 dic_filling.elements[word] = 156def get_value(dict, element):57 if element in dict:58 print("Found, ", end="")59 return (element, "appeared ", dict[element], " times")60 #else:61 print(element, " not found")62def main():63 # Welcome to program64 print("\r\nWelcome to Find Words. \r\n")65 print(" Intructions: \r\n\t-Enter words you want to find in a desired text \n")66 # files = ["pg-dorian_gray.txt", "pg-les_miserables.txt", "pg-moby_dick.txt", "pg-sherlock_holmes.txt", "pg-aura.txt", "pg-pedro_paramo.txt"]67 # find_words = []68 # filepath = 069 print("\t1- Dorian Gray \t 2- Les Miserables \t 3- Moby Dick \t 4- Sherlock Holmes \t 5- Aura \t 6- Pedro Paramo\r\n")70 select_text(1)71 add_words(3)72if __name__ == '__main__':...
services.py
Source:services.py
1# -*- coding: utf-8 -*-2from contextlib import closing3from collections import OrderedDict4from django.db import connection5from django.utils.translation import to_locale, get_language6from ...base.utils.sqlpaginator import SqlPaginator7from ...base.utils.db import dictfetchall8def get_region_list(request):9 rows = _get_regions(239)10 result = []11 for data in rows:12 result.append(OrderedDict([13 ('id', data['id']),14 ('name', data['name'])15 ]))16 paginator = SqlPaginator(request, page=1, per_page=len(result), count=len(result))17 pagging = paginator.get_paginated_response()18 return OrderedDict([19 ('items', result),20 ('meta', pagging)21 ])22def get_district_list(request, id=None):23 rows = _get_districts(id)24 result = []25 for data in rows:26 result.append(OrderedDict([27 ('id', data['id']),28 ('region_id', data['region_id']),29 ('name', data['name'])30 ]))31 paginator = SqlPaginator(request, page=1, per_page=len(result), count=len(result))32 pagging = paginator.get_paginated_response()33 return OrderedDict([34 ('items', result),35 ('meta', pagging)36 ])37def _get_regions(country_id):38 current_language = to_locale(get_language())39 if current_language == 'uz':40 select_text = 'name_uz as name'41 else:42 select_text = 'name_ru as name'43 extra_sql = """select id, {select_text}44 FROM geo_region45 ORDER BY ordering46 LIMIT 10047 """.format(select_text=select_text)48 with closing(connection.cursor()) as cursor:49 cursor.execute(extra_sql)50 rows = dictfetchall(cursor)51 return rows52def _get_districts(region_id=None):53 current_language = to_locale(get_language())54 if current_language == 'uz':55 select_text = 'name_uz as name'56 else:57 select_text = 'name_ru as name'58 if region_id:59 extra_sql = """select id, region_id, {select_text}60 FROM geo_district61 WHERE region_id = %s62 ORDER BY ordering63 LIMIT 10064 """.format(select_text=select_text)65 params = [region_id]66 else:67 params = None68 extra_sql = """select id, region_id, {select_text}69 FROM geo_district 70 ORDER BY region_id71 LIMIT 20072 """.format(select_text=select_text)73 with closing(connection.cursor()) as cursor:74 cursor.execute(extra_sql, params)75 rows = dictfetchall(cursor)...
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!