Best Python code snippet using robotframework
callie_mcgraw_scraper.py
Source:callie_mcgraw_scraper.py
1# -*- coding: utf-8 -*-2'''3Created on 30-Jan-20184@author: Administrator5'''6import csv,sys7from selenium import webdriver8import time9from urllib.parse import urlparse, parse_qs10# URL = 'https://treasurer.yumacountyaz.gov/treasurer/treasurerweb/search.jsp'11# file_name = 'treasurer'12URL = sys.argv[1]13file_name = sys.argv[2]14start_keyword = ''15try:16 start_keyword = sys.argv[3]17except Exception as e:18 pass19# options = Options()20# options.add_argument('--headless')21# options.add_argument('--disable-gpu')22# options.add_experimental_option("excludeSwitches",["ignore-certificate-errors"])23# driver = webdriver.Chrome(executable_path ='C:\\Users\\Administrator\\workspace\\Project\\Projects\\chromedriver\\chromedriver.exe', chrome_options=options)24# driver1 = webdriver.Chrome(executable_path ='C:\\Users\\Administrator\\workspace\\Project\\Projects\\chromedriver\\chromedriver.exe', chrome_options=options)25driver = webdriver.Chrome('./chromedriver')26driver1 = webdriver.Chrome('./chromedriver')27output_f = open(file_name+'.csv','w',encoding='utf-16', newline='')28WR = csv.writer(output_f, quoting=csv.QUOTE_ALL)29WR.writerow(['Account Number','Parcel Number','Owners','Address Line 1','Address Line 2','Situs Address','Legal','Property Type','Actual','Assessed','Total Due'])30products_url = ''31def login(driver,url):32 driver.get(url)33 driver.find_element_by_css_selector('form[method="GET"] [type="submit"]').click()34 35def input_keywords(driver, keyword, url):36 driver.get(url)37 driver.find_element_by_css_selector('#TaxAOwnerIDSearchString').send_keys(keyword+'*')38 driver.find_element_by_css_selector('[value="Search"]').click()39 40def get_next_page(driver):41 try:42 page_links = driver.find_elements_by_css_selector('[class="pagelinks"] a')43 44 for p in page_links:45 if p.text.lower() == 'next':46 return (p.get_attribute('href'))47 48 break49 except Exception as e:50 return 051def get_details(driver, url,wr):52 try:53 details = []54 driver.get(url)55 time.sleep(0.5)56 tr_count = len(driver.find_elements_by_css_selector('#taxAccountSummary tr'))57 account = driver.find_element_by_css_selector('#taxAccountSummary tr:nth-of-type(1) td:nth-of-type(2)').text58 parcel = driver.find_element_by_css_selector('#taxAccountSummary tr:nth-of-type(2) td:nth-of-type(2)').text59 legal = driver.find_element_by_css_selector('#taxAccountSummary tr:nth-of-type({}) td:nth-of-type(2)'.format(tr_count)).text60 situs = driver.find_element_by_css_selector('#taxAccountSummary tr:nth-of-type({}) td:nth-of-type(2)'.format(tr_count-1)).text61 address= driver.find_element_by_css_selector('#taxAccountSummary tr:nth-of-type({}) td:nth-of-type(2)'.format(tr_count-2)).text62 total_due = ''63 owners = []64 for i in range(3,4+(tr_count-6)):65 owners.append(driver.find_element_by_css_selector('#taxAccountSummary tr:nth-of-type({}) td:nth-of-type(2)'.format(i)).text)66 67 owners = ', '.join(owners)68 details.append(account)69 details.append(parcel)70 details.append(owners)71 add1 = ''72 add2 = ''73 try:74 add1 = address.split('\n')[0]75 except Exception as e:76 pass77 try:78 add2 = address.split('\n')[1]79 except Exception as e:80 pass81 details.append(add1)82 details.append(add2)83 84 details.append(situs.replace('\n',' ').replace('\r',' ').strip())85 details.append(legal.replace('\n',' ').replace('\r',' ').strip())86 for t in driver.find_elements_by_css_selector('#totals tr'):87 if t.find_element_by_css_selector('td:nth-of-type(1)').text == 'Total Due':88 total_due = (t.find_element_by_css_selector('td:nth-of-type(2)').text)89 break90 property_type = ''91 actual = ''92 assessed = ''93 for p in driver.find_elements_by_css_selector('#taxAccountValueSummary tr'):94 try:95 prop = p.find_element_by_css_selector('td:nth-of-type(1)').text.strip().replace('\n','').replace('\r','')96 actu = p.find_element_by_css_selector('td:nth-of-type(2)').text.strip().replace(',','')97 asse = p.find_element_by_css_selector('td:nth-of-type(3)').text.strip().replace(',','')98 if actu.isdigit() and asse.isdigit():99 property_type = prop100 actual = actu101 assessed = asse102 break103 except Exception as e:104 #print (e)105 pass106 107 details.append(property_type)108 details.append(actual)109 details.append(assessed)110 details.append(total_due)111 wr.writerow(details)112 except Exception as e:113 print (e)114 #print (details)115 116login(driver, URL)117login(driver1, URL)118do_start = 0119for i in ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']:120 for j in ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']:121 122 if start_keyword == '':123 do_start = 1124 125 if not start_keyword == '' and start_keyword[0] == i and start_keyword[1] == j:126 do_start = 1127 128 if do_start == 1:129 print ('Getting accounts for keyword : ' +i+j)130 input_keywords(driver, i+j, URL)131 while True:132 account_links = driver.find_elements_by_css_selector('#searchResultsTable td strong a')133 for al in account_links:134 lk = str(al.get_attribute('href'))135 o = urlparse(lk)136 query = parse_qs(o.query)137 if 'account' in query:138 if query['account'][0][:1].lower() == 'r':139 get_details(driver1, lk, WR)140 141 next_page = str(get_next_page(driver))142 143 if not next_page == 'None':144 print ('\t >> Next page : '+next_page)145 driver.get(next_page)146 else:147 break148 149 try:150 driver.find_element_by_css_selector('[href*="logout.jsp"]').click()151 login(driver, URL)152 except Exception as e:153 login(driver, URL)154 try:155 driver1.find_element_by_css_selector('[href*="logout.jsp"]').click()156 login(driver1, URL)157 except Exception as e:158 login(driver1, URL)159driver.quit()160driver1.quit()...
test_str_format_function.py
Source:test_str_format_function.py
1"""Test formatting of a string value within a processed property.2Attributes3----------4LOWER_KEYWORD : :py:obj:`str`5 String identifier for lower case formatting. This is the same keyword that6 is seen in :py:mod:`pyproprop/utils`.7UPPER_KEYWORD : :py:obj:`str`8 String identifier for upper case formatting. This is the same keyword that9 is seen in :py:mod:`pyproprop/utils`.10TITLE_KEYWORD : :py:obj:`str`11 String identifier for title case formatting. This is the same keyword that12 is seen in :py:mod:`pyproprop/utils`.13START_KEYWORD : :py:obj:`str`14 String identifier for start case formatting. This is the same keyword that15 is seen in :py:mod:`pyproprop/utils`.16SNAKE_KEYWORD : :py:obj:`str`17 String identifier for snake case formatting. This is the same keyword that18 is seen in :py:mod:`pyproprop/utils`.19PASCAL_KEYWORD : :py:obj:`str`20 String identifier for pascal case formatting. This is the same keyword that21 is seen in :py:mod:`pyproprop/utils`.22HYPHEN_KEYWORD : :py:obj:`str`23 String identifier for hyphen case formatting. This is the same keyword that24 is seen in :py:mod:`pyproprop/utils`.25EXAMPLE_STR_1 : :py:obj:`str`26 Very basic test example.27EXAMPLE_STR_1_FORMATTED : :py:obj:`dict`28 Expected formatted output strings for :py:const:`EXAMPLE_STR_1`.29EXAMPLE_STR_2 : :py:obj:`str`30 Test example involving multiple spaces and an abbreviation.31EXAMPLE_STR_2_FORMATTED : :py:obj:`dict`32 Expected formatted output strings for :py:const:`EXAMPLE_STR_2`.33EXAMPLE_STR_3 : :py:obj:`str`34 Test example involving invalid identifier punctuation and underscores35 between words.36EXAMPLE_STR_3_FORMATTED : :py:obj:`dict`37 Expected formatted output strings for :py:const:`EXAMPLE_STR_3`.38EXAMPLE_STR_4 : :py:obj:`str`39 Test example involving punctuation, hyphenation between words and40 apostrophies.41EXAMPLE_STR_4_FORMATTED : :py:obj:`dict`42 Expected formatted output strings for :py:const:`EXAMPLE_STR_4`.43EXAMPLE_STR_5 : :py:obj:`str`44 Test example involving different uses of underscores.45EXAMPLE_STR_5_FORMATTED : :py:obj:`dict`46 Expected formatted output strings for :py:const:`EXAMPLE_STR_5`.47"""48import pytest49from pyproprop import format_str_case50# TODO - make example strings fixtures using pytes-cases (see issue #36)51LOWER_KEYWORD = "lower"52UPPER_KEYWORD = "upper"53TITLE_KEYWORD = "title"54START_KEYWORD = "start"55SNAKE_KEYWORD = "snake"56PASCAL_KEYWORD = "pascal"57HYPHEN_KEYWORD = "hyphen"58EXAMPLE_STR_1 = "this is a string"59EXAMPLE_STR_1_FORMATTED = {60 LOWER_KEYWORD: "this is a string",61 UPPER_KEYWORD: "THIS IS A STRING",62 TITLE_KEYWORD: "This Is a String",63 START_KEYWORD: "This is a string",64 SNAKE_KEYWORD: "this_is_a_string",65 PASCAL_KEYWORD: "ThisIsAString",66 HYPHEN_KEYWORD: "this-is-a-string",67}68EXAMPLE_STR_2 = "string with an ABRV"69EXAMPLE_STR_2_FORMATTED = {70 LOWER_KEYWORD: "string with an abrv",71 UPPER_KEYWORD: "STRING WITH AN ABRV",72 TITLE_KEYWORD: "String With an ABRV",73 START_KEYWORD: "String with an ABRV",74 SNAKE_KEYWORD: "string_with_an_abrv",75 PASCAL_KEYWORD: "StringWithAnABRV",76 HYPHEN_KEYWORD: "string-with-an-abrv",77}78EXAMPLE_STR_3 = "string_with %_£+"79EXAMPLE_STR_3_FORMATTED = {80 LOWER_KEYWORD: "string_with %_£+",81 UPPER_KEYWORD: "STRING_WITH %_£+",82 TITLE_KEYWORD: "String_with %_£+",83 START_KEYWORD: "String_with %_£+",84 SNAKE_KEYWORD: "string_with",85 PASCAL_KEYWORD: "StringWith",86 HYPHEN_KEYWORD: "string-with",87}88EXAMPLE_STR_4 = "it's an example-with punctuation!"89EXAMPLE_STR_4_FORMATTED = {90 LOWER_KEYWORD: "it's an example-with punctuation!",91 UPPER_KEYWORD: "IT'S AN EXAMPLE-WITH PUNCTUATION!",92 TITLE_KEYWORD: "It's an Example-With Punctuation!",93 START_KEYWORD: "It's an example-with punctuation!",94 SNAKE_KEYWORD: "its_an_example_with_punctuation",95 PASCAL_KEYWORD: "ItsAnExampleWithPunctuation",96 HYPHEN_KEYWORD: "its-an-example-with-punctuation",97}98EXAMPLE_STR_5 = "string _with__lots___of_underscores_"99EXAMPLE_STR_5_FORMATTED = {100 LOWER_KEYWORD: "string _with__lots___of_underscores_",101 UPPER_KEYWORD: "STRING _WITH__LOTS___OF_UNDERSCORES_",102 TITLE_KEYWORD: "String _with__lots___of_underscores_",103 START_KEYWORD: "String _with__lots___of_underscores_",104 SNAKE_KEYWORD: "string_with_lots_of_underscores",105 PASCAL_KEYWORD: "StringWithLotsOfUnderscores",106 HYPHEN_KEYWORD: "string-with-lots-of-underscores",107}108@pytest.mark.parametrize(109 "input_str, expected",110 [111 (EXAMPLE_STR_1, EXAMPLE_STR_1_FORMATTED),112 (EXAMPLE_STR_2, EXAMPLE_STR_2_FORMATTED),113 (EXAMPLE_STR_3, EXAMPLE_STR_3_FORMATTED),114 (EXAMPLE_STR_4, EXAMPLE_STR_4_FORMATTED),115 (EXAMPLE_STR_5, EXAMPLE_STR_5_FORMATTED),116 ],117)118def test_formatted_string_method_expected_result_with_processing(input_str, expected):119 """Assert strings examples formatted exactly as expected, with `process`120 flag set to `True`.121 Additional example strings should be added in future to this test to ensure122 that a number of wider use-cases are supported.123 """124 assert (125 format_str_case(input_str, case=LOWER_KEYWORD, process=True)126 == expected[LOWER_KEYWORD]127 )128 assert (129 format_str_case(input_str, case=UPPER_KEYWORD, process=True)130 == expected[UPPER_KEYWORD]131 )132 assert (133 format_str_case(input_str, case=TITLE_KEYWORD, process=True)134 == expected[TITLE_KEYWORD]135 )136 assert (137 format_str_case(input_str, case=START_KEYWORD, process=True)138 == expected[START_KEYWORD]139 )140 assert (141 format_str_case(input_str, case=SNAKE_KEYWORD, process=True)142 == expected[SNAKE_KEYWORD]143 )144 assert (145 format_str_case(input_str, case=PASCAL_KEYWORD, process=True)146 == expected[PASCAL_KEYWORD]147 )148 assert (149 format_str_case(input_str, case=HYPHEN_KEYWORD, process=True)150 == expected[HYPHEN_KEYWORD]...
manual.py
Source:manual.py
1from enum import Enum, auto2import discord3import re4class State(Enum):5 REPORT_START = auto()6 AWAITING_MESSAGE = auto()7 MESSAGE_IDENTIFIED = auto()8 AWAITING_SE_RESPONSE = auto()9 MINOR_INVOLVED = auto()10 GROOMING = auto()11 CSAM = auto()12 DELETE_MESSAGE = auto()13 SUSPEND_USER = auto()14 FORWARD_MESSAGE = auto()15 REPORT_COMPLETE = auto()16class Manual:17 START_KEYWORD = ['yes', 'y', 'Yes']18 CANCEL_KEYWORD = ['cancel']19 HELP_KEYWORD = "help"20 def __init__(self, client):21 self.state = State.REPORT_START22 self.client = client23 self.message = None24 async def handle_message(self, message):25 '''26 This function makes up the meat of the manual-side reporting flow.27 '''28 if message.content in self.CANCEL_KEYWORD:29 self.state = State.REPORT_COMPLETE30 return ["Report cancelled."]31 #REPORT START32 if self.state == State.REPORT_START and message.content in self.START_KEYWORD:33 reply = "Report Started: please answer the following questions to identify further action.\n"34 reply += "Use the `cancel` command at anytime to cancel the report process.\n\n"35 reply += "Does this message contain sexually explicit content?"36 self.state = State.AWAITING_SE_RESPONSE37 return [reply]38 if self.state == State.REPORT_START and message.content not in self.START_KEYWORD:39 self.state = State.REPORT_COMPLETE40 return ["Report cancelled."]41 #SE CONTENET42 if self.state == State.AWAITING_SE_RESPONSE and message.content in self.START_KEYWORD:43 reply = "Is a minor involved or referenced?"44 self.state = State.MINOR_INVOLVED45 return [reply]46 if self.state == State.AWAITING_SE_RESPONSE and message.content not in self.START_KEYWORD:47 reply = "Does this message show signs of grooming?"48 self.state = State.GROOMING49 return [reply]50 #MINOR INVOVLED51 if self.state == State.MINOR_INVOLVED and message.content in self.START_KEYWORD:52 reply = "Is CSAM referenced?"53 self.state = State.CSAM54 return [reply]55 if self.state == State.MINOR_INVOLVED and message.content not in self.START_KEYWORD:56 reply = "Would you like to forward this message to a different department?"57 self.state = State.FORWARD_MESSAGE58 return [reply]59 #GROOMING60 if self.state == State.GROOMING and message.content in self.START_KEYWORD:61 reply = "Is a minor involved or referenced?"62 self.state = State.MINOR_INVOLVED63 return [reply]64 if self.state == State.GROOMING and message.content not in self.START_KEYWORD:65 reply = "Would you like to forward this message to a different department?"66 self.state = State.FORWARD_MESSAGE67 return [reply]68 #CSAM69 if self.state == State.CSAM and message.content in self.START_KEYWORD:70 reply = "This message has been forwarded to the authorities.\n"71 reply += "The account will be suspended and message deleted. Report Completed."72 self.state = State.REPORT_COMPLETE73 return [reply]74 if self.state == State.CSAM and message.content not in self.START_KEYWORD:75 reply = "Would you like to delete this message?"76 self.state = State.DELETE_MESSAGE77 return [reply]78 #DELETE MESSAGE79 if self.state == State.DELETE_MESSAGE and message.content in self.START_KEYWORD:80 reply = "This message has been removed. Would you like to suspend this user?"81 self.state = State.SUSPEND_USER82 return [reply]83 if self.state == State.DELETE_MESSAGE and message.content not in self.START_KEYWORD:84 reply = "Would you like to suspend this user?"85 self.state = State.SUSPEND_USER86 return [reply]87 #SUSPEND USER88 if self.state == State.SUSPEND_USER and message.content in self.START_KEYWORD:89 reply = "This user has been suspended. Report Completed."90 self.state = State.REPORT_COMPLETE91 return [reply]92 if self.state == State.SUSPEND_USER and message.content not in self.START_KEYWORD:93 reply = "Report Completed."94 self.state = State.REPORT_COMPLETE95 return [reply]96 #FORWARD MESSAGE97 if self.state == State.FORWARD_MESSAGE and message.content in self.START_KEYWORD:98 reply = "This content has been forwarded. Would you like to delete this message?"99 self.state = State.DELETE_MESSAGE100 return [reply]101 if self.state == State.FORWARD_MESSAGE and message.content not in self.START_KEYWORD:102 reply = "Would you like to delete this message?"103 self.state = State.DELETE_MESSAGE104 return [reply]105 #REPORT COMPLETE106 if self.state == State.REPORT_COMPLETE:107 reply = "Please wait for a new flagged message. Thank you."108 self.state = State.REPORT_COMPLETE109 return [reply]110 def report_complete(self):...
log_timer_gta.py
Source:log_timer_gta.py
1import os, sys2import argparse3import logging, time4import datetime5from watchdog import events6from watchdog.observers import Observer7import codecs8start_keyword = '[INFO]OkToPuSz adminszolgálatba lépett.'9end_keyword = '[INFO]OkToPuSz kilépett az adminszolgálatból.'10opened_keyword = False11changed_keyword = False12start_time = None13class ChangeEvent(events.PatternMatchingEventHandler):14 def on_modified(self, event):15 super(ChangeEvent, self).on_modified(event)16 check_log_time()17def parse_arguments():18 parser = argparse.ArgumentParser(19 description='Monitor a file for changes and log them.',20 epilog='With love from Kvazhir <3',21 add_help=False22 )23 opt_arg = parser.add_argument_group('optional arguments')24 opt_arg.add_argument('-h', '--help', action='help', help='show this help message and exit')25 opt_arg.add_argument('-v', '--verbose', action='store_true', help='enable verbosity')26 opt_arg.add_argument('-ks', '--keyword_start', 27 action='store', 28 help='the keyword for starting the timer (default: [INFO]OkToPuSz adminszolgálatba lépett.)',29 default='[INFO]OkToPuSz adminszolgálatba lépett.'30 )31 opt_arg.add_argument('-ke', '--keyword_end', 32 action='store', 33 help='the keyword for ending the timer (default: [INFO]OkToPuSz kilépett az adminszolgálatból.)',34 default='[INFO]OkToPuSz kilépett az adminszolgálatból.'35 )36 opt_arg.add_argument('--version', action='version', version='%(prog)s 0.1')37 req_arg = parser.add_argument_group('required arguments')38 req_arg.add_argument('-i', '--input', 39 action='store', 40 help='path to input file',41 metavar='<input_path>',42 required=True43 )44 req_arg.add_argument('-o', '--output',45 action='store',46 help='path to log output',47 metavar='<output_path>',48 required=True49 )50 args = parser.parse_args()51 args.input = os.path.abspath(args.input)52 return args53def check_log_time():54 global opened_keyword55 global start_keyword56 global end_keyword57 global start_time58 global changed_keyword59 content = read_file.read()60 content.encode('utf-8')61 if args.verbose:62 print(content)63 logging.debug(content)64 if not opened_keyword:65 if start_keyword in content:66 start_time = datetime.datetime.now()67 if args.verbose:68 if not changed_keyword:69 print('OkToPuSz came ONLINE.')70 else:71 print('Timer started.')72 logging.debug('in %s', str(start_time))73 log_file.write('IN: ' + str(start_time) + '\n')74 opened_keyword = True75 else:76 if end_keyword in content:77 end_time = datetime.datetime.now()78 if args.verbose:79 if not changed_keyword:80 print('OkToPuSz went OFFLINE.')81 else:82 print('Timer stopped and logged.')83 logging.debug('out %s', str(end_time))84 log_file.write('OUT: ' + str(end_time) + '\n')85 log_file.write('SESSION: ' + str(end_time - start_time) + '\n\n')86 opened_keyword = False87 log_file.flush()88 os.fsync(log_file.fileno())89def main():90 global args91 global read_file92 global log_file93 global start_keyword94 global end_keyword95 global changed_keyword96 args = parse_arguments()97 if os.path.isfile('log_timer_debug.log'):98 if os.stat('log_timer_debug.log').st_size > 1000000000: # 1gb99 os.remove('log_timer_debug.log')100 if args.keyword_start != start_keyword:101 start_keyword = args.keyword_start102 changed_keyword = True103 if args.keyword_end != end_keyword:104 end_keyword = args.keyword_end105 changed_keyword = True106 if args.verbose:107 print('Starting keyword:', start_keyword)108 print('Ending keyword:', end_keyword)109 logging.basicConfig(filename='log_timer_debug.log',110 filemode='a',111 level=logging.DEBUG,112 format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',113 datefmt='%Y-%m-%d %H:%M:%S')114 event_handler = ChangeEvent([args.input])115 observer = Observer()116 observer.schedule(event_handler, os.path.dirname(args.input), recursive=False)117 try:118 read_file = codecs.open(args.input, encoding='utf-8', mode='r')119 log_file = codecs.open(args.output, encoding='utf-8', mode='a')120 read_file.seek(0, 2)121 except OSError as e:122 logging.error(str(e))123 sys.exit(1)124 try:125 observer.start()126 except FileNotFoundError as e:127 logging.error(str(e))128 sys.exit(1)129 try:130 while True:131 time.sleep(1)132 except KeyboardInterrupt:133 read_file.close()134 log_file.close()135 observer.stop()136 observer.join()137if __name__ == "__main__":138 main()...
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!!