Best Python code snippet using autotest_python
querylib.py
Source:querylib.py
1"""2 querylib - holds functions used for querying3"""4import searchtool.settings as sets5import re6import itertools7from googleapiclient.discovery import build8import datetime9import time10import json11def validateOptionalArgs(indicators, start_date, end_date, number_of_results):12 """13 returns true if all option arguments are valid14 """15 if indicators != "off":16 17 # check indicators contains valid codes18 db = sets.getDB()19 db_indicator_types = db.indicators.find()20 db_codes = []21 for ind_type in db_indicator_types:22 db_codes.append(ind_type['code'])23 for code in indicators:24 if code not in db_codes:25 return False26 if start_date != "off":27 # check start date is valid28 isValidStartDate = re.match(r'^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$', start_date)29 30 if not isValidStartDate:31 return False32 if end_date != "off":33 # check start date is valid34 isValidEndDate = re.match(r'^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$', end_date)35 if not isValidEndDate:36 return False37 # check number of results is a valid int38 if type(number_of_results) is not int:39 return False40 # check number of results is multiple of ten41 if number_of_results % 10 != 0:42 return False43 return True44def generateQuery(query_string, indicators, query_mode):45 """46 Generate query(s) from base string and indicators.47 - SINGLE query mode returns a string48 - MULTI query mode returns a list of strings49 """50 db = sets.getDB()51 # if indicators off, just return the query52 if indicators == "off":53 return query_string54 55 # indicator list provided56 # handle based on query mode57 if query_mode == 'single':58 # query mode is SINGLE, create a single string59 # get the indicators from the database60 indcator_types = []61 for ind_code in indicators:62 indicator_types.append(db.indicators.find({"code": ind_code}))63 generated_query_string = query_string64 65 for ind_type in indcator_types:66 generated_query_string += " AND ("67 68 ind_words = []69 for iword in ind_type['words']:70 ind_words += iword71 for word in ind_words:72 generated_query_string += + "'" + word + "' OR "73 generated_query_string += ")"74 return generated_query_string75 else:76 # query mode is MULTI, create a list of queries77 # get the indicators from the database78 indicator_types = []79 80 for ind_code in indicators:81 db_ind = db.indicators.find_one({"code": ind_code})82 indicator_types.append(db_ind)83 # make ind_words a nested list of indicators by their indicator type84 ind_words = []85 for ind_type in indicator_types:86 ind_words.append(ind_type['words'])87 # get all combinations88 all_combos = list(itertools.product(*ind_words))89 query_list = []90 # have one query that is just the query string91 query_list.append(query_string)92 for combo in all_combos:93 combo = list(combo)94 temp_query = query_string95 for key_phrase in combo:96 temp_query += ' AND "' + key_phrase + '"'97 query_list.append(temp_query)98 return query_list99def get_date_string(start_date, end_date):100 """101 Turns the dates into a string for the API102 """103 # first convert dates104 start_date = start_date.replace('/', '')105 end_date = end_date.replace('/','')106 if start_date == 'off' and end_date == 'off':107 return ""108 elif start_date == 'off' and end_date != 'off':109 return "date:r::" + end_date110 elif start_date != 'off' and end_date == 'off':111 return "date:r:" + start_date + ":"112 else:113 return "date:r:" + start_date + ":" + end_date114def queryAPI(query, start_date, end_date, number_of_results):115 """116 Query the API, return the results as a list of JSON object117 """118 # get config119 api_key = sets.getAPIKey()120 search_engine_id = sets.getSearchEngineID()121 service = build("customsearch", "v1", developerKey=api_key)122 result_list = []123 # make multiple api calls in multiples of 10 to get number of results124 for i in range(0, number_of_results, 10):125 i += 1126 api_call = service.cse().list(127 q=query,128 cx=search_engine_id,129 sort=get_date_string(start_date, end_date),130 start=i131 )132 result = api_call.execute()133 # get the results134 results = []135 total_results = result['queries']['request'][0]['totalResults']136 if int(total_results) > 0:137 for item in result['items']:138 splitMeta = item['snippet'].split(" ")139 if len(splitMeta) > 2:140 if splitMeta[3] == "...":141 date = splitMeta[0] + " " + splitMeta[1] + " " + splitMeta[2]142 meta = " ".join(splitMeta[4:len(splitMeta)])143 else:144 date = "No date available"145 meta = item['snippet']146 results.append({147 'title': item['title'],148 'url': item['link'],149 'date': date,150 'meta': meta151 })152 #build the JSON object153 json_data = {154 "query" : str(api_call.__dict__["uri"]),155 "query_string" : str(query),156 "search_engine_id" : str(search_engine_id),157 "start_date" : str(start_date),158 "end_date" : str(end_date),159 "query_mode" : str(sets.getQueryMode()),160 "number_of_runs" : str(sets.getNumberOfRuns()),161 "number_of_results" : number_of_results,162 "start_from_result" : i,163 "total_results" : total_results,164 "timestamp" : str(datetime.datetime.fromtimestamp(time.time())),165 "results" : results166 }167 result_list.append(json_data)168 return result_list169def calculateDuplications():170 """171 Drops and populates the query_duplications table with calculated duplications172 """173 db = sets.getDB()174 db.query_duplications.drop()175 # get all results (query history)176 archive_results = db.query_archive.find()177 # init duplication count list178 duplication_count = []179 for res in archive_results:180 query_string = res['query_string'] 181 date = res['timestamp'][0:13] #2017-08-21 16:34:31.622082182 results_list = res['results']183 for r in results_list:184 url = r['url']185 # Ignore query parameters186 url_split = url.split('?')187 base_url = url_split[0]188 flag = False189 for record in duplication_count:190 if base_url == record['url']:191 record['count'] = record['count'] + 1192 if query_string not in record['query_list']:193 record['query_list'].append(query_string)194 if date not in record['date_list']:195 record['date_list'].append(date)196 197 flag = True198 break199 if flag == False:200 duplication_count.append({"url": base_url, "count": 1, "query_list": [query_string], "date_list": [date]})201 for item in duplication_count:202 db.query_duplications.insert({203 "url": item['url'],204 "frequency": item['count'],205 "query_list": item['query_list'],206 "date_list": item['date_list']207 })...
translate.py
Source:translate.py
1import string2import sys3import os4import codecs5import unicodedata6class translate:7 def translateGIB(GIBstring): 8 9 moves = []10 11 body = False12 13 # I don't like gib files14 while(GIBstring != None):15 first = GIBstring.read(1)16 if(first != 'S'):17 next = GIBstring.read(2)18 if(next == 'GE'): # this is the eof19 GIBstring = None20 break21 metadata = GIBstring.readline()22 if(next == '[W'):23 splitmeta = metadata.replace(':',',').split(',')24 whitePlayer = splitmeta[5]25 if(next == '[B'):26 splitmeta = metadata.replace(':',',').split(',')27 blackPlayer = splitmeta[5]28 elif(first == 'S'): # read a move from file29 body = True30 line = GIBstring.readline()31 splitLine = line.split()32 if (splitLine[0] == 'TO'):33 color = splitLine[3]34 moveX = int(splitLine[4])35 moveY = int(splitLine[5])36 moves.append((color, moveX, moveY))37 # try again next line38 39 # PB[nathan9 ]BR[3D]PW[sidae777 ]WR[3D]40 SGFstring = '(;GM[1]FF[4]CA[UTF-8]SZ[19]PB[%s]PW[%s]' % (blackPlayer, whitePlayer)41 42 for move in moves:43 if (move[0] == '1'):44 SGFstring += ';B['45 elif (move[0] == '2'):46 SGFstring += ';W['47 else:48 return "Error"49 50 SGFstring += '%s%s]' % (chr(move[1] + 97),chr(move[2] + 97)) # go from number to letter representation with ascii offset for 'a'51 52 SGFstring += ')' 53 return SGFstring54 55 rootDir = sys.argv[1]56 targetDir = sys.argv[2]57 58 for dirName, subdirList, fileList in os.walk(rootDir):59 for fname in fileList:60 if fname.endswith('.gib'):61 print("Cpnverting %s" %fname)62 path = os.path.join(dirName, fname)63 with open(path, mode='rt', encoding="utf-8", errors="ignore") as infile:64 sgfBody = translateGIB(infile)65 writePath = targetDir + dirName[len(rootDir):]66 if not os.path.exists(writePath):67 os.makedirs(writePath)68 writefname = fname[:-4] + '.sgf'69 with open(writePath + '/' + writefname, 'w') as f:70 f.write(sgfBody)71 else:72 continue...
highlight.py
Source:highlight.py
...11 global _TEMPLATE12 if not _TEMPLATE:13 _TEMPLATE = Template(14 '<span style="color: {color|htmltag};">{token|html}</span>')15 meta_left, meta_right = SplitMeta(meta)16 token_re = MakeTokenRegex(meta_left, meta_right)17 tokens = token_re.split(template_str)18 html = []19 for i, token in enumerate(tokens):20 # By the definition of re.split, even tokens are literal strings, and odd21 # tokens are directives.22 if i % 2 == 0:23 html.append(cgi.escape(token))24 else:25 # Because of the regex, the token should be at least 2 characters long26 c = token[1]27 if c == '#':28 token_type = COMMENT29 elif c == '.':...
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!!