def write_benchmarking_results(results_directory, output_file, baseline_module, include_call): results, python_versions_by_modernity, calling_code = load_benchmarking_results( results_directory) modules_by_modern_speed = [ module for module, results in sorted( [*results.items()], key=lambda kvp: kvp[1].most_modern_result().timing) ] writer = pytablewriter.RstGridTableWriter() formatted_python_versions = [ f"Python {major}.{minor}" for major, minor in python_versions_by_modernity ] writer.header_list = ["Module"] + ( ["Call"] if include_call else []) + formatted_python_versions + [ f"Relative Slowdown (versus {baseline_module}, latest Python)" ] writer.type_hint_list = [pytablewriter.String] * len(writer.header_list) calling_codes = [ calling_code[module] for module in modules_by_modern_speed ] performance_results = [[ results[module].get(python_version, NOT_APPLICABLE) for python_version in python_versions_by_modernity ] for module in modules_by_modern_speed] relative_slowdowns = [ relative_slowdown(results[module], results[baseline_module]) if module != baseline_module else NOT_APPLICABLE for module in modules_by_modern_speed ] writer.value_matrix = [ [module] + ([calling_code[module]] if include_call else []) + performance_by_version + [relative_slowdown] for module, calling_code, performance_by_version, relative_slowdown in zip(modules_by_modern_speed, calling_codes, performance_results, relative_slowdowns) ] with open(output_file, "w") as fout: writer.stream = fout writer.write_table() fout.write("\n") if len(modules_by_modern_speed) > 1: baseline_module_timing = results[ baseline_module].most_modern_result().formatted_timing() fastest_module, next_fastest_module = modules_by_modern_speed[0:2] if fastest_module == baseline_module: fout.write( f"{baseline_module} takes {baseline_module_timing}, which is **{relative_slowdown(results[next_fastest_module], results[baseline_module])} faster than {next_fastest_module}**, the next fastest ISO 8601 parser in this comparison.\n" ) else: fout.write( f"{baseline_module} takes {baseline_module_timing}, which is **{relative_slowdown(results[baseline_module], results[fastest_module])} slower than {fastest_module}**, the fastest ISO 8601 parser in this comparison.\n" )
def dump_to_rst(self, rst): if self.input_file is not None: rst.file.write('Input file: %s\n' % self.input_file) rst.dump_title('Register map', 5) rst.dump_title('Overview', 6) writer = pytablewriter.RstGridTableWriter() writer.header_list = ['Name', 'Offset', 'Width', 'Description'] table = [] for name, register in self.registers.items(): register.dump_to_reglist_rst(table) writer.value_matrix = table writer.stream = rst.get_file() writer.write_table() rst.dump_title('Generated headers', 6) self.dump_regs_to_rst(rst=rst) rst.file.write('|\n') for name, register in self.registers.items(): rst.dump_title(register.name, 6, link=register.name) register.dump_to_rst(rst)
def dump_to_rst(self, rst): writer = pytablewriter.RstGridTableWriter() writer.header_list = ['Bit #', 'Value', 'Description'] table = [] for name, field in self.fields.items(): field.dump_to_rst(table) writer.value_matrix = table writer.stream = rst.get_file() writer.write_table()
def convert(tab): writer = pytablewriter.RstGridTableWriter() writer.table_name = "table_rst" file = open(tab, "r") num = 0 mylist = [] for line in file: line = line.strip() temp = line.split("\t") mylist.append(temp) writer.value_matrix = mylist writer.write_table()
def get_tabla(self, nombre_tabla, lista_cabeceras, lista_de_listas_con_datos): """ Devuelve una tabla en formato RestructuredText. :param nombre_tabla: nombre con el que la tabla aparece en el documento :param lista_cabeceras: lista de textos que aparecerán en las cabeceras de la tabla :param lista_de_lista_con_datos: lista de listas en las que aparecen las filas de datos """ writer = pytablewriter.RstGridTableWriter( headers=lista_cabeceras, value_matrix=lista_de_listas_con_datos, table_name=nombre_tabla) return writer.dumps()
def dump_to_cmdlist_rst(self, rst, writer): writer = pytablewriter.RstGridTableWriter() writer.header_list = [ 'Command name', 'Width', 'Command code', 'Description' ] table = [] for name, cmd in self.commands.items(): cmd.dump_to_cmdlist_rst(self, table) writer.value_matrix = table writer.stream = rst.get_file() writer.write_table() for name, cmd in self.commands.items(): rst.dump_title(cmd.name, 6, link='%s_%s' % (self.name, cmd.name)) cmd.dump_to_rst(rst)
def dump_to_rst(self, rst, pretty_name): if self.input_file is not None: rst.file.write('.. \n') rst.file.write(' Input file: %s\n' % self.input_file) if pretty_name is not None: rst.dump_title('Register map for %s' % pretty_name, 5) else: rst.dump_title('Register map', 5) rst.dump_title('Overview', 6) writer = pytablewriter.RstGridTableWriter() writer.header_list = ['Name', 'Offset', 'Width', 'Description'] table = [] for name, register in self.registers.items(): register.dump_to_reglist_rst(self, table) writer.value_matrix = table writer.stream = rst.get_file() writer.write_table() if os.environ.get('DUMP_C_HEADERS_TO_RST'): rst.dump_title('Generated headers', 6) self.dump_regs_to_rst(rst=rst) rst.file.write('|\n') for name, register in self.registers.items(): rst.dump_title(register.name, 6, link='%s_%s' % (self.name, register.name)) register.dump_to_rst(rst) if len(self.cmdmaps) != 0: for name, cmdmap in self.cmdmaps.items(): rst.dump_title(name, 5) cmdmap.dump_to_cmdlist_rst(rst, writer)
def create_pandas_rst(acc_tbl, runtime_tbl, minimizers, colour_scale, rst_links, table_names): """ Generates html page from pandas dataframes. :param acc_tbl : DataFrame of the accuracy results :type acc_tbl : pandas DataFrame :param runtime_tbl : DataFrame of the timing results :type runtime_tbl : pandas DataFrame :param minimizers : list of minimizers (column headers) :type minimizers : list :param colour_scale : user defined colour scale :type colour_scale : list :param rst_links : rst links used in pandas rendering :type rst_links : list :param table_names : list of table names :type table_names : list """ colour_bounds = [colour[0] for colour in colour_scale] rst_colours = [colour[1] for colour in colour_scale] acc_tbl.index = rst_links runtime_tbl.index = rst_links def colour_highlight(data): ''' Colour mapping for visualisation of table ''' data_list = check_normalised(data, rst_colours, colour_bounds) for i, x in enumerate(data): data[i] = ':{}:`{}`'.format(data_list[i], x) return data results = [acc_tbl, runtime_tbl] for table, name in zip(results, table_names): table.apply(colour_highlight, axis=1) writer = pytablewriter.RstGridTableWriter() writer.from_dataframe(table, add_index_column=True) writer.dump(name + "rst")
def dump_to_rst(self, rst): if self.desc is not None: rst.file.write(self.desc) rst.file.write('\n') rst.file.write('\n') writer = pytablewriter.RstGridTableWriter() writer.header_list = ['Bit #', 'R/W', 'Name', 'Description'] table = [] for name, field in self.fields.items(): field.dump_to_rst(table) writer.value_matrix = table writer.stream = rst.get_file() writer.write_table() rst.dump_title('Generated headers', 6) self.dump_c_headers_to_rst(rst=rst) rst.file.write('|\n')
import pytablewriter writer = pytablewriter.RstGridTableWriter() # writer.table_name = "params list" writer.headers = ["name", "type", "e.g.", "is_necessary"] writer.value_matrix = [ ["mobile", "string", "15522229999", True], ["password", "string", "password123*", True], ["username", "string", "user_name", False], ["nick_name", "string", "nick_name", False], ["avatar_url", "string", "15522229999", False], ["whats_up", "string", "whats_up", False], ] with open("./docs/table/user.rst", "w") as f: writer.stream = f writer.write_table()
def _get_tablewriter(): return pytablewriter.RstGridTableWriter()
def main(results_directory, output_file, compare_to, include_call, module_version_output): calling_code = {} timestamps = set() all_results = defaultdict(dict) timing_results = defaultdict(dict) for parent, _dirs, files in os.walk(results_directory): files_to_process = [f for f in files if FILENAME_REGEX.match(f)] for csv_file in files_to_process: try: with open(os.path.join(parent, csv_file), 'r') as fin: reader = csv.reader(fin, delimiter=",", quotechar='"') major, minor, timestamp = next(reader) timestamps.add(timestamp) for module, _setup, stmt, parse_result, count, time_taken, matched, exception in reader: all_results[(major, minor)][module] = Result(float(time_taken) / int(count), parse_result, exception, True if matched == "True" else False ) timing_results[(major, minor)][module] = all_results[(major, minor)][module].timing calling_code[module] = f"``{stmt.format(timestamp=timestamp)}``" except: print(f"Problem while parsing `{os.path.join(parent, csv_file)}`") raise if len(timestamps) > 1: raise NotImplementedError(f"Found a mix of files in the results directory. Found files that represent the parsing of {timestamps}. Support for handling multiple timestamps is not implemented.") all_modules = set([module for value in timing_results.values() for module in value.keys()]) python_versions_by_modernity = sorted(timing_results.keys(), reverse=True) most_modern_python = python_versions_by_modernity[0] modules_by_modern_speed = sorted(all_modules, key=lambda module: timing_results[most_modern_python][module]) writer = pytablewriter.RstGridTableWriter() formatted_python_versions = ["Python {}".format(".".join(key)) for key in python_versions_by_modernity] writer.header_list = ["Module"] + (["Call"] if include_call else []) + formatted_python_versions + [f"Relative Slowdown (versus {compare_to}, {formatted_python_versions[0]})"] writer.type_hint_list = [pytablewriter.String] * len(writer.header_list) calling_codes = [calling_code[module] for module in modules_by_modern_speed] performance_results = [[format_result(all_results[python_version].get(module, NOT_APPLICABLE)) for python_version in python_versions_by_modernity] for module in modules_by_modern_speed] relative_slowdowns = [format_relative(timing_results[most_modern_python].get(module), timing_results[most_modern_python].get(compare_to)) if module != compare_to else NOT_APPLICABLE for module in modules_by_modern_speed] writer.value_matrix = [ [module] + ([calling_code[module]] if include_call else []) + performance_by_version + [relative_slowdown] for module, calling_code, performance_by_version, relative_slowdown in zip(modules_by_modern_speed, calling_codes, performance_results, relative_slowdowns) ] with open(output_file, 'w') as fout: writer.stream = fout writer.write_table() fout.write('\n') if modules_by_modern_speed[0] == compare_to: fout.write(f"{compare_to} takes {format_duration(timing_results[most_modern_python][compare_to])}, which is **{format_relative(timing_results[most_modern_python][modules_by_modern_speed[1]], timing_results[most_modern_python][compare_to])} faster than {modules_by_modern_speed[1]}**, the next fastest ISO 8601 parser in this comparison.\n") else: fout.write(f"{compare_to} takes {format_duration(timing_results[most_modern_python][compare_to])}, which is **{format_relative(timing_results[most_modern_python][compare_to], timing_results[most_modern_python][modules_by_modern_speed[0]])} slower than {modules_by_modern_speed[0]}**, the fastest ISO 8601 parser in this comparison.\n") with open(os.path.join(os.path.dirname(output_file), module_version_output), 'w') as fout: fout.write(f"Tested on {platform.system()} {platform.release()} using the following modules:\n") fout.write('\n') fout.write(".. code:: python\n") fout.write('\n') for module_version_line in format_used_module_versions(determine_used_module_versions(results_directory)): fout.write(f" {module_version_line}\n")
if property_name not in property_name_csv_to_rst: raise Exception("Unknown property " + property_name + " in CSV file") row[0] = property_name_csv_to_rst[property_name] units = row[2] if units not in units_csv_to_rst: raise Exception("Unknown units " + units + " in CSV file") row[2] = units_csv_to_rst[units] table_rows += [row] with open(rst_filename, 'w') as rst_file: rst_writer = ptw.RstGridTableWriter() #rst_writer.table_name = os.path.basename(rst_filename) rst_writer.headers = table_rows[0] rst_writer.value_matrix = table_rows[1:] # Make sure precision of original numbers is not changed rst_writer.type_hints = [ptw.String] * len(rst_writer.headers) rst_writer.stream = rst_file rst_writer.write_table()