Best Python code snippet using tempest_python
transientdisk_test.py
Source:transientdisk_test.py
...73 size=10 * MiB74 )75 transientdisk.remove_disk(owner_name, disk1_name)76 # owner dir should remain77 assert transientdisk.list_disks(owner_name) == [disk2_name]78 transientdisk.remove_disk(owner_name, disk2_name)79 # owner dir should be removed now80 assert transientdisk.list_disks(owner_name) == []81def test_add_existing_transient_disk(tmp_basedir):82 owner_name = 'vm-id'83 disk_name = 'sda'84 def create_disk():85 return transientdisk.create_disk(86 owner_name=owner_name,87 disk_name=disk_name,88 size=10 * MiB89 )90 create_disk()91 with pytest.raises(se.TransientDiskAlreadyExists):92 create_disk()93 assert transientdisk.list_disks(owner_name) == [disk_name]94def test_remove_disk_not_exists(tmp_basedir):95 owner_name = 'vm-id'96 disk1_name = 'sda'97 disk2_name = 'sdb'98 transientdisk.create_disk(99 owner_name=owner_name,100 disk_name=disk1_name,101 size=10 * MiB102 )103 # removal of disk that isn't exists should not104 # raise any exception105 transientdisk.remove_disk(owner_name, disk2_name)106 assert transientdisk.list_disks(owner_name) == [disk1_name]107def test_remove_disk_and_dir_not_exists(tmp_basedir):108 owner_name = 'vm-id'109 disk_name = 'sda'110 # removal of disk and directory that aren't exists111 # should not raise any exception112 transientdisk.remove_disk(owner_name, disk_name)113def test_list_disks(tmp_basedir):114 owner_name = 'vm-id'115 disk1_name = 'sda'116 disk2_name = 'sdb'117 for disk_name in [disk1_name, disk2_name]:118 res = transientdisk.create_disk(119 owner_name=owner_name,120 disk_name=disk_name,121 size=10 * MiB122 )123 actual_path = res['path']124 assert os.path.isfile(actual_path)125 disks_list = transientdisk.list_disks(owner_name)126 assert sorted(disks_list) == [disk1_name, disk2_name]127def test_list_disks_no_dir(tmp_basedir):128 owner_name = 'vm-id'129 disks_list = transientdisk.list_disks(owner_name)...
tower_disk07.py
Source:tower_disk07.py
1import re2from collections import namedtuple3Disk = namedtuple("Disk", "name weight subdisks")4# with open("day07_test.txt") as file:5with open("day07.txt") as file:6 test_data = file.readlines()7test_string = "fwft (72) -> ktlj, cntj, xhth\n"8p = re.compile(r"\W+")9print(p.split(test_string))10def process_call(input_string: str) -> Disk:11 """converts a call(a string) in to a disk data strucutre"""12 p = re.compile(r"\W+")13 disk_l = p.split(input_string[:-1]) # remove \n from end of string14 # print(disk_l)15 if len(disk_l) > 3:16 subdisk = disk_l[2:]17 else:18 subdisk = None19 disk_out = Disk(disk_l[0], disk_l[1], subdisk)20 # print(disk_out)21 return disk_out22process_call(test_string)23process_call("fbrdpr (28)") # have to make sure empty subnodes works24list_disks = [process_call(disk) for disk in test_data]25# for disk in list_disks:26# print(disk)27class Node:28 def __init__(self, disk):29 self.name = disk.name30 self.weigth = int(disk.weight)31 self.subdisks = []32 def __repr__(self):33 return str(self.name) + " " + str(self.weigth)34 def pprint(self):35 subs = " ".join(str(disk.name) for disk in self.subdisks)36 print(str(self.name) + " " + str(self.weigth) + "->" + subs)37 for disk in self.subdisks:38 disk.pprint()39 def sum_check(self):40 """ all disks towers must be equal weight, finds the weights that are not equal and prints them out so you can figure out the off blance node, "lnpuarm is too heavy by 8"41 """42 weigth = self.weigth43 if self.subdisks:44 l_weights = [disk.sum_check() for disk in self.subdisks]45 weigth += sum(l_weights)46 for w in l_weights:47 if w != l_weights[0]:48 print("weight of sub towers", l_weights)49 print("with the correspnding disk:")50 print(self.name, "->", self.subdisks, "\n")51 break52 return weigth53 def find_up(self):54 """Looks throught the list a builds the tree out from the node"""55 print("calling find_up")56 # find self.name in list_disks:57 flag = 058 for idx, disk in enumerate(list_disks):59 if disk.subdisks: # checks not none60 if self.name in disk.subdisks:61 thedisk = list_disks.pop(idx)62 flag = 163 break64 if flag == 0:65 print("stop")66 print(f"To insert:{thedisk}")67 leaf = self68 self = Node(thedisk)69 self.subdisks.append(leaf)70 print("leaf is now", self.subdisks)71 print("root is now", self.name)72 thedisk.subdisks.remove(leaf.name)73 for disk in thedisk.subdisks:74 self.find_down(disk)75 print("after insertion")76 return self77 def find_down(self, disk_name):78 """Finds the leafs from the list and rescursively inserts there weights and subdisks"""79 print("calling find_down")80 # now search the list for a match81 for idx, disk in enumerate(list_disks):82 if disk_name in disk.name:83 disk_in_list = list_disks.pop(idx)84 break85 # need to creat the subdisks now86 leaf = Node(disk_in_list)87 # insert the leafs recursively88 if disk_in_list.subdisks:89 # checking if there are subdisks90 for disk_name in disk_in_list.subdisks:91 leaf = leaf.find_down(disk_name)92 # finally add the leafs into the node(rescursively)93 self.subdisks.append(leaf)94 return self95print()96root = Node(list_disks.pop(2)) # need to start with a leaf97while list_disks:98 root = root.find_up()99print()100root.pprint()101assert len(list_disks) == 0102print("the root:", root.name, "->", root.subdisks)103# print(root.sum_towers())104print("part2: finding misblance")105print()...
CarsPrice.py
Source:CarsPrice.py
1import urllib.request2import urllib.parse3import csv4import time5from bs4 import BeautifulSoup6import http.client7http.client.HTTPConnection._http_vsn = 108http.client.HTTPConnection._http_vsn_str = 'HTTP/1.0'9#Insert your URL10# BASE_URL = 'http://moscow.drom.ru/toyota/camry/?fueltype=1&transmission=2&ph=1&pts=2&damaged=2&w=2&go_search=2'11BASE_URL = 'https://baza.drom.ru/user/Replika777'12#Insert your NAME File13NAME_FILE = "Cars"14def get_html(url):15 try:16 content_page = urllib.request.urlopen(url).read().decode('windows-1251')17 return content_page18 except http.client.IncompleteRead:19 return False20def remove_char(string, chars):21 string_new = ""22 for c in string:23 if c not in chars:24 string_new = string_new + c25 return string_new26def parse_html(html_page):27 list_object = BeautifulSoup(html_page, 'html.parser')28 # print(list_object.prettify())29 print(list_object.body.form.table.tbody.prettify())30 list_object = list_object.body.form.table.tbody31 list_disks = []32 33 for x in list_object.find_all('td', {'class' : 'descriptionCell'}):34 list_disks.append(x)35 print(list_disks)36 var = []37 38 for x in list_disks:39 print(x.td.div.div.span.content)40 # var.append({"URL" : x['href'],41 # "price" : x.find('div', {'class': 'priceCell'})42 # .replace("\n", "")43 # .replace("\xa0", "")44 # .strip(),45 #46 # })47 48 print(var)49 return var50def result_print(data):51 for x in data:52 print("{0}; {1}; {2}".format(x['date'], x['date']), x['value'])53# def save(namefile, data):54# with open("{0}.txt".format(namefile), "w") as f:55# f.write("Date ; UTC ; Value")56# for x in data:57# f.write("{0}; {1}; {2}".format(x['date'], convert_data(x['date']), x['value']))58 59def save_csv(namefile, data):60 with open("{0}.csv".format(namefile), "a", encoding='utf-8') as f:61 spamwriter = csv.writer(f, dialect='excel')62 for x in data:63 spamwriter.writerow((x['year'], x['price'], x['URL'], x['date'], x['city']))64def create_csv(namefile):65 with open("{0}.csv".format(namefile), "w", encoding='utf-8') as f:66 spamwriter = csv.writer(f, dialect='excel')67def count_page(current_link):68 69 count = 170 create_csv(NAME_FILE)71 72 while 1:73 time.sleep(1)74 print(count)75 if count % 5 == 0:76 time.sleep(15)77 if count % 10 == 0:78 time.sleep(30)79 content = get_html(current_link)80 81 82 content_page = BeautifulSoup(content, 'html.parser')83 #print(content_page)84 var = content_page.find("a", {"class" : "b-pagination__item_next"})85 if var is None:86 print(("Pages: {0}".format(count)))87 x = parse_html(content)88 # save_csv(NAME_FILE, x)89 break90 else:91 count +=192 current_link = var['href']93 content = parse_html(get_html(current_link))94 # save_csv(NAME_FILE, content)95# count_page(BASE_URL)96if __name__ == '__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!!