Best Python code snippet using avocado_python
insta_poster.py
Source:insta_poster.py
...20 chrome_options.add_argument('--user-agent="Mozilla/5.0 (iPhone; CPU iPhone OS 13_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/83.0.4103.116 Mobile/15E148 Safari/604.1"')21 self.driver = webdriver.Chrome(ChromeDriverManager().install(), options=chrome_options)22 def login(self):23 self.driver.get("https://instagram.com")24 self.sleep_random()25 #Log in button26 self.driver.find_element_by_xpath("/html/body/div[1]/section/main/article/div/div/div/div[2]/button").click()27 self.sleep_random()28 self.driver.find_element_by_xpath("//input[@name=\"username\"]").send_keys(self.username)29 self.driver.find_element_by_xpath("//input[@name=\"password\"]").send_keys(self.pw)30 self.driver.find_element_by_xpath('//button[@type="submit"]').click()31 self.sleep_random()32 33 def close_popups(self):34 #Not Now Button35 if self.check_exists_by_xpath("/html/body/div[1]/section/main/div/div/div/button"):36 self.driver.find_element_by_xpath("/html/body/div[1]/section/main/div/div/div/button").click()37 self.sleep_random()38 39 #Cancel on adding to home screen40 if self.check_exists_by_xpath("/html/body/div[4]/div/div/div/div[3]/button[2]"):41 self.driver.find_element_by_xpath("/html/body/div[4]/div/div/div/div[3]/button[2]").click()42 self.sleep_random()43 44 def post(self):45 #Post button46 self.driver.find_element_by_xpath("/html/body/div[1]/section/nav[2]/div/div/div[2]/div/div/div[3]").click()47 self.sleep_random()48 write('/')49 self.sleep_random()50 write('Users/nishant/git/reddit-ig-poster', interval=0.05)51 press('return')52 self.sleep_random()53 write(self.subreddit, interval=0.1)54 press('return')55 self.sleep_random()56 # Scroll to bottom57 self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")58 self.sleep_random()59 # Resize image Button60 self.driver.find_element_by_xpath("/html/body/div[1]/section/div[2]/div[2]/div/div/div/button[1]").click()61 self.sleep_random()62 # Scroll to top63 self.driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.HOME)64 self.sleep_random()65 66 # Next Button67 self.driver.find_element_by_xpath("/html/body/div[1]/section/div[1]/header/div/div[2]/button").click()68 self.sleep_random()69 c = Captions()70 caption = c.generate_caption(self.subreddit, self.post_rating)71 # Type caption72 self.driver.find_element_by_xpath("/html/body/div[1]/section/div[2]/section[1]/div[1]/textarea").send_keys(caption)73 self.sleep_random()74 # Post 75 self.driver.find_element_by_xpath("/html/body/div[1]/section/div[1]/header/div/div[2]/button").click()76 sleep(5)77 def successfully_made_post(self):78 try:79 self.login()80 self.close_popups()81 self.post()82 self.driver.close()83 except:84 print("failed to post")85 self.driver.close()86 return False87 else:88 print("sucessfully posted")89 return True90 def sleep_random(self):91 num = 1+random.randint(1,3)92 sleep(num)93 def check_exists_by_xpath(self, xpath):94 try:95 self.driver.find_element_by_xpath(xpath)96 except NoSuchElementException:97 return False...
decorators.py
Source:decorators.py
1import time2import random3def sleep_random():4 time.sleep(random.random())5 return "Done!"6print(sleep_random())7####################################8def add(a, b):9 return a + b10def sub(a, b):11 return a - b12def apply(func, a, b):13 return func(a, b)14print(apply(add, 1, 2))15print(apply(sub, 1, 2))16###############################################17def power(n):18 def func(number):19 return number ** n20 return func21pow2 = power(2)22pow3 = power(3)23print(pow2(3))24print(pow3(3))25####################################################26import random27def stopwatch(f):28 def func(*args, **kwargs):29 tic = time.time()30 result = f(*args, **kwargs)31 print(f"this function took: {time.time() - tic}")32 return result33 return func34def sleep_random():35 time.sleep(random.random())36 return "Done!"37timed_sleep = stopwatch(sleep_random)38print(timed_sleep())39###################################################40from functools import wraps41def stopwatch(f):42 @wraps(f)43 def func(*args, **kwargs):44 tic = time.time()45 result = f(*args, **kwargs)46 print(f"this function took: {time.time() - tic}")47 return result48 return func49@stopwatch50def sleep_random():51 '''This string will be propagated by the wraps decorator.'''52 time.sleep(random.random())53 return "Done!"54###############################55def print_call1(f):56 @wraps(f)57 def func(*args, **kwargs):58 tic = time.time()59 result = f(*args, **kwargs)60 print(f"print 1: {time.time() - tic}")61 return result62 return func63def print_call2(f):64 @wraps(f)65 def func(*args, **kwargs):66 tic = time.time()67 result = f(*args, **kwargs)68 print(f"print 2: {time.time() - tic}")69 return result70 return func71@print_call272@print_call173def sleep_random():74 '''This string will be propagated by the wraps decorator.'''75 time.sleep(random.random())76 return "Done!"77# we ca have more then one decorator in a stack78######################################################79from retry import retry80import logging81logging.basicConfig()82@retry(ValueError, tries = 5, delay= 0.5)83def randomly_fails(p=0.5):84 if random.random() < p:85 raise ValueError("no bueno!")86 return "Done!"87randomly_fails
worker.py
Source:worker.py
1import random2import time3def sleep_random(min_s, max_s, unit=1):4 seconds = random.randint(min_s, max_s)5 time.sleep(seconds * unit)6def find_or_steal_money(initial, target):7 current = initial8 loop = 09 while current < target:10 sleep_random(1, 3, 0.1)11 current += 112 loop += 113 if loop > 50:14 return current15 return current16def build_boat():17 sleep_random(4, 14)18def steal_boat():19 sleep_random(8, 19)20def find_crew():21 sleep_random(4, 10)22def find_parrot():23 if random.randint(1, 2) == 1:24 raise ValueError("Failed!")...
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!!