Best Python code snippet using autotest_python
csv_encoder.py
Source:csv_encoder.py
...6 def __init__(self, request, response):7 self._request = request8 self._response = response9 self._output_rows = []10 def _append_output_row(self, row):11 self._output_rows.append(row)12 def _build_response(self):13 response = django.http.HttpResponse(mimetype='text/csv')14 response['Content-Disposition'] = (15 'attachment; filename=tko_query.csv')16 writer = csv.writer(response)17 writer.writerows(self._output_rows)18 return response19 def encode(self):20 raise NotImplementedError21class UnhandledMethodEncoder(CsvEncoder):22 def encode(self):23 return rpc_utils.raw_http_response(24 'Unhandled method %s (this indicates a bug)\r\n' %25 self._request['method'])26class SpreadsheetCsvEncoder(CsvEncoder):27 def _total_index(self, group, num_columns):28 row_index, column_index = group['header_indices']29 return row_index * num_columns + column_index30 def _group_string(self, group):31 result = '%s / %s' % (group['pass_count'], group['complete_count'])32 if group['incomplete_count'] > 0:33 result += ' (%s incomplete)' % group['incomplete_count']34 if 'extra_info' in group:35 result = '\n'.join([result] + group['extra_info'])36 return result37 def _build_value_table(self):38 value_table = [''] * self._num_rows * self._num_columns39 for group in self._response['groups']:40 total_index = self._total_index(group, self._num_columns)41 value_table[total_index] = self._group_string(group)42 return value_table43 def _header_string(self, header_value):44 return '/'.join(header_value)45 def _process_value_table(self, value_table, row_headers):46 total_index = 047 for row_index in xrange(self._num_rows):48 row_header = self._header_string(row_headers[row_index])49 row_end_index = total_index + self._num_columns50 row_values = value_table[total_index:row_end_index]51 self._append_output_row([row_header] + row_values)52 total_index += self._num_columns53 def encode(self):54 header_values = self._response['header_values']55 assert len(header_values) == 256 row_headers, column_headers = header_values57 self._num_rows, self._num_columns = (len(row_headers),58 len(column_headers))59 value_table = self._build_value_table()60 first_line = [''] + [self._header_string(header_value)61 for header_value in column_headers]62 self._append_output_row(first_line)63 self._process_value_table(value_table, row_headers)64 return self._build_response()65class TableCsvEncoder(CsvEncoder):66 def __init__(self, request, response):67 super(TableCsvEncoder, self).__init__(request, response)68 self._column_specs = request['columns']69 def _format_row(self, row_object):70 """Extract data from a row object into a list of strings"""71 return [row_object.get(field) for field, name in self._column_specs]72 def _encode_table(self, row_objects):73 self._append_output_row([column_spec[1] # header row74 for column_spec in self._column_specs])75 for row_object in row_objects:76 self._append_output_row(self._format_row(row_object))77 return self._build_response()78 def encode(self):79 return self._encode_table(self._response)80class GroupedTableCsvEncoder(TableCsvEncoder):81 def encode(self):82 return self._encode_table(self._response['groups'])83class StatusCountTableCsvEncoder(GroupedTableCsvEncoder):84 _PASS_RATE_FIELD = '_test_pass_rate'85 def __init__(self, request, response):86 super(StatusCountTableCsvEncoder, self).__init__(request, response)87 # inject a more sensible field name for test pass rate88 for column_spec in self._column_specs:89 field, name = column_spec90 if name == 'Test pass rate':...
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!!