Best Python code snippet using avocado_python
concordance.py
Source:concordance.py
1from hash_quad import *2import string3class Concordance:4 def __init__(self):5 self.stop_table = None # hash table for stop words6 self.concordance_table = None # hash table for concordance7 def load_stop_table(self, filename):8 """ Read stop words from input file (filename) and insert each word as a key into the stop words hash table.9 Starting size of hash table should be 191: self.stop_table = HashTable(191)10 If file does not exist, raise FileNotFoundError"""11 self.stop_table = HashTable(191)12 try:13 file = open(filename, "r")14 except FileNotFoundError:15 raise FileNotFoundError16 read_all_lines = False17 while not read_all_lines:18 line = file.readline()19 if line == "":20 read_all_lines = True21 if not read_all_lines:22 stop_word = line.strip()23 self.stop_table.insert(stop_word)24 file.close()25 def load_concordance_table(self, filename):26 """ Read words from input text file (filename) and insert them into the concordance hash table,27 after processing for punctuation, numbers and filtering out words that are in the stop words hash table.28 Do not include duplicate line numbers (word appearing on same line more than once, just one entry for that line)29 Starting size of hash table should be 191: self.concordance_table = HashTable(191)30 If file does not exist, raise FileNotFoundError"""31 self.concordance_table = HashTable(191)32 try:33 file = open(filename, "r")34 except FileNotFoundError:35 raise FileNotFoundError36 read_all_lines = False37 line_number = 038 while not read_all_lines:39 line = file.readline().lower()40 if line == "":41 read_all_lines = True42 line_number += 143 if not read_all_lines:44 new_line = self.punctuation_removal(line)45 for i in new_line:46 if not(self.stop_table.in_table(i)) and not(self.is_int_or_float(i)):47 index = self.concordance_table.get_index(i)48 if index is not None:49 value = self.concordance_table.hash_table[index][1]50 length_of_value = len(value)51 if value[length_of_value - 1] != line_number:52 value.append(line_number)53 new_value = (self.concordance_table.hash_table[index][0], value)54 self.concordance_table.hash_table[index] = new_value55 else:56 self.concordance_table.insert(i, [line_number])57 file.close()58 def write_concordance(self, filename):59 """ Write the concordance entries to the output file(filename)60 See sample output files for format."""61 file = open(filename, "w")62 keys = self.concordance_table.get_all_keys()63 keys.sort()64 for i in range(len(keys)):65 index = self.concordance_table.get_index(keys[i])66 key = self.concordance_table.hash_table[index][0]67 values = self.concordance_table.hash_table[index][1]68 string_of_values = ""69 for j in values:70 string_of_values += " " + str(j)71 content = "{0}:{1}".format(key, string_of_values)72 if i == len(keys) - 1:73 file.write(content)74 else:75 file.write(content + "\n")76 file.close()77 def punctuation_removal(self, line):78 """This functions takes in a line and creates a new line without any punctuation in it. It makes sure to79 view dashes as spaces."""80 punctuation = string.punctuation81 res = ""82 for i in line:83 if i == "-":84 res += " "85 elif i in punctuation:86 res += ""87 else:88 res += i89 return res.split()90 def is_int_or_float(self, word):91 """This function checks if the given string is a number (float or integer) and returns True if it92 is a number and False if it is not a number."""93 try:94 integer = float(word)95 return True96 except ValueError:...
fileread.py
Source:fileread.py
1def read_first_ten_lines():2 ln_number = lines_number()3 if ln_number < 10:4 return read_all_lines()[0:ln_number]5 else:6 return read_all_lines()[0:10]7def read_last_ten_lines():8 ln_number = lines_number()9 if ln_number < 10:10 return read_all_lines()[-ln_number:]11 else:12 return read_all_lines()[-10:]13def read_all_lines():14 lines = file.readlines()15 file.seek(0)16 return lines17def exit_func():18 file.close()19 exit()20def lines_number():21 return len(read_all_lines())22def words_frequency():23 words = dict()24 lines = read_all_lines()25 for line in lines:26 for word in line.split():27 word = clean_word(word).lower()28 if word in words:29 words[word] = words[word] + 130 else:31 words[word] = 132 return words33def find_longest_word():34 maxlen = 035 longword = ""36 lines = read_all_lines()37 for line in lines:38 for word in line.split(" "):39 word = clean_word(word)40 if len(word) > maxlen:41 maxlen = len(word)42 longword = word43 return longword44def print_lines(lines):45 for line in lines:46 print(line, end="")47def print_dict(lines):48 for key in lines:49 print(key, " - ", lines[key])50def clean_word(word):51 return word.replace(' ', '').replace('.', '').replace(',', '')52path = input("ÐведиÑе Ð¸Ð¼Ñ Ñайла:")53try:54 file = open(path)55except Exception as e:56 print("Ðе ÑдалоÑÑ Ð¿ÑоÑиÑаÑÑ Ñайл")57 print(e)58 exit()59else:60 print_lines(read_all_lines())61 while 1:62 print("ÐÑбеÑиÑе опÑиÑ:")63 print("1 - ÐÑоÑиÑаÑÑ Ð¿ÐµÑвÑе 10 ÑÑÑок")64 print("2 - ÐÑоÑиÑаÑÑ Ð¿Ð¾Ñледние 10 ÑÑÑок")65 print("3 - ÐÑоÑиÑаÑÑ Ð²ÐµÑÑ ÑекÑÑ")66 print("4 - ÐÑвеÑÑи колиÑеÑÑво ÑÑÑок")67 print("5 - ÐÑвеÑÑи длиннейÑее Ñлово")68 print("6 - ÐÑвеÑÑи ÑиÑло повÑоÑений")69 print("7 - ÐÑÑ
од")70 a = input("ÐÐ°Ñ Ð²ÑбоÑ:")71 if a.isnumeric() and 8 > int(a) > 0:72 choise = int(a)73 if choise == 1:74 print_lines(read_first_ten_lines())75 elif choise == 2:76 print_lines(read_last_ten_lines())77 elif choise == 3:78 print_lines(read_all_lines())79 elif choise == 4:80 print(lines_number())81 elif choise == 5:82 print(find_longest_word())83 elif choise == 6:84 print_dict(words_frequency())85 else:86 exit_func()87 else:...
file_reader.py
Source:file_reader.py
...33 def read_first_line(self):34 read_line = lambda: self.file.readline()35 return self.__execute_function(read_line)36 37 def read_all_lines(self):38 read_all_lines = lambda: self.file.readlines()...
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!!