Best Python code snippet using avocado_python
astring.py
Source:astring.py
...78 command = command.replace("$", r'\$')79 command = command.replace('"', r'\"')80 command = command.replace('`', r'\`')81 return command82def strip_console_codes(output, custom_codes=None):83 """84 Remove the Linux console escape and control sequences from the console85 output. Make the output readable and can be used for result check. Now86 only remove some basic console codes using during boot up.87 :param output: The output from Linux console88 :type output: string89 :param custom_codes: The codes added to the console codes which is not90 covered in the default codes91 :type output: string92 :return: the string without any special codes93 :rtype: string94 """95 if "\x1b" not in output:96 return output97 old_word = ""98 return_str = ""99 index = 0100 output = "\x1b[m%s" % output101 console_codes = "%[G@8]|\\[[@A-HJ-MPXa-hl-nqrsu\\`]"102 console_codes += "|\\[[\\d;]+[HJKgqnrm]|#8|\\([B0UK]|\\)"103 if custom_codes is not None and custom_codes not in console_codes:104 console_codes += "|%s" % custom_codes105 while index < len(output):106 tmp_index = 0107 tmp_word = ""108 while (len(re.findall("\x1b", tmp_word)) < 2 and109 index + tmp_index < len(output)):110 tmp_word += output[index + tmp_index]111 tmp_index += 1112 tmp_word = re.sub("\x1b", "", tmp_word)113 index += len(tmp_word) + 1114 if tmp_word == old_word:115 continue116 try:117 special_code = re.findall(console_codes, tmp_word)[0]118 except IndexError:119 if index + tmp_index < len(output):120 raise ValueError("%s is not included in the known console "121 "codes list %s" % (tmp_word, console_codes))122 continue123 if special_code == tmp_word:124 continue125 old_word = tmp_word126 return_str += tmp_word[len(special_code):]127 return return_str128def iter_tabular_output(matrix, header=None, strip=False):129 """130 Generator for a pretty, aligned string representation of a nxm matrix.131 This representation can be used to print any tabular data, such as132 database results. It works by scanning the lengths of each element133 in each column, and determining the format string dynamically.134 :param matrix: Matrix representation (list with n rows of m elements).135 :param header: Optional tuple or list with header elements to be displayed.136 :param strip: Optionally remove trailing whitespace from each row.137 """138 def _get_matrix_with_header():139 return itertools.chain([header], matrix)140 def _get_matrix_no_header():141 return matrix142 if header is None:143 header = []144 if header:145 get_matrix = _get_matrix_with_header146 else:147 get_matrix = _get_matrix_no_header148 lengths = []149 len_matrix = []150 str_matrix = []151 for row in get_matrix():152 len_matrix.append([])153 str_matrix.append([string_safe_encode(column) for column in row])154 for i, column in enumerate(str_matrix[-1]):155 col_len = len(strip_console_codes(column))156 len_matrix[-1].append(col_len)157 try:158 max_len = lengths[i]159 if col_len > max_len:160 lengths[i] = col_len161 except IndexError:162 lengths.append(col_len)163 # For different no cols we need to calculate `lengths` of the last item164 # but later in `yield` we don't want it in `len_matrix`165 len_matrix[-1] = len_matrix[-1][:-1]166 if strip:167 def str_out(x): return " ".join(x).rstrip()168 else:169 def str_out(x): return " ".join(x)...
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!!