def _print_table(table_data: list, wrap_column: int = None) -> None: """ Helper function to print a table to the console. INPUTS table_data: A list where each entry is a list representing the columns in a table wrap_column: The 0-indexed column to wrap OUTPUTS None """ table = terminaltables.SingleTable(table_data) table.inner_heading_row_border = False table.inner_row_border = True table.justify_columns[0] = "center" # Calculate newlines if wrap_column is not None: max_width = table.column_max_width(wrap_column) for row in table_data: row[wrap_column] = "\n".join(wrap(row[wrap_column], max_width)) print(table.table)
def printBackups(backupsDict): """ Prints table of the backups on the media and in the database. Args: backupsDict (dict): list of backups Returns: None """ # create table tableData = [['Datetime', 'Folder', 'HDD', 'DB']] backups = list(backupsDict.keys()) backups.sort(reverse=True) for backup in backups: items = list(backupsDict[backup].keys()) items.sort() if len(items) > 0: for item, i in zip(items, range(len(items))): if i == 0: tableData.append([backup, item, '', '']) else: tableData.append(['', item, '', '']) if backupsDict[backup][item]['HDD']: tableData[-1][2] = 'X' if backupsDict[backup][item]['DB']: tableData[-1][3] = 'X' else: tableData.append([backup, '', '', '']) table = terminaltables.SingleTable(tableData) table.justify_columns[2] = 'center' table.justify_columns[3] = 'center' print(table.table)
def build(options, tables, title=None): """ Helper for building terminal table """ data = [['Option', 'Value', 'Type', 'Allowed', 'Description']] for key in options.keys(): opt = options.raw(key) if isinstance(opt.vtype, tuple): vtype = repr([v.__name__ for v in opt.vtype]) else: vtype = opt.vtype.__name__ if not isinstance(opt.get(), Options): data.append([opt.name, repr(opt.get()), vtype, repr(opt.allow), opt.doc]) else: data.append([opt.name, 'Options', vtype, repr(opt.allow), opt.doc]) build(opt.get(), tables, title=opt.name) table = terminaltables.SingleTable(data, title=title) n = sum(table.column_widths[:-2]) for i in range(len(table.table_data)): table.table_data[i][-2] = '\n'.join(textwrap.wrap(table.table_data[i][-2], 24)) table.table_data[i][-1] = '\n'.join(textwrap.wrap(table.table_data[i][-1], width-(n+24))) tables.append(table.table)
def MessengerBox(message, title): messbox = terminaltables.SingleTable(message, title) messbox.inner_heading_row_border = False messbox.inner_row_border = False messbox.justify_columns = {0: 'center'} print(messbox.table)
def table(rows, title='', prefix='', alignment=(), wrap_width=-1, indent=wrap_indent): """Format the list of rows as a table. - Each row is a sequence of column cells. - The first row defines the column headers. When terminaltables package is accessible, use it to get nice pretty tables, otherwise use some home-made primitive replacement - see https://pypi.org/project/terminaltables - see https://github.com/Robpol86/terminaltables >>> table_data = [ ... ( 'Name' , 'Occupation' , 'Note' ) , ... ( 'Alice' , '?' , '---' ) , ... ( 'Bob' , 'unemployed' , '' ) ] >>> t = table ( table_data , 'Title' ) >>> print (t) """ from ostap.utils.basic import isatty title = allright(decolorize(title)) if rows: rows = list(rows) header_row = rows[0] header_row = [infostr(decolorize(c)) for c in header_row] rows[0] = header_row rows = tuple(rows) rows = [list(row) for row in rows] if not terminaltables: ## use the local replacement return the_table(rows, title, prefix, alignment=alignment) if isatty(): title = allright(title) table_instance = terminaltables.SingleTable(rows, title) else: title = allright(title) table_instance = terminaltables.AsciiTable(rows, title) cw = table_instance.column_widths nc = len(cw) wraps = [i for (i, a) in enumerate(alignment) if a in wrapped] if wraps: from terminaltables.width_and_alignment import max_dimensions widths = max_dimensions(table_instance.table_data, table_instance.padding_left, table_instance.padding_right)[2] widths = sum(l for (i, l) in enumerate(widths) if not i in wraps) widths += nc + 1 + len(prefix) + 4 + 2 * len(wraps) _, w = terminal_size() ww = w - widths ww, _ = divmod(ww, len(wraps)) if 12 < ww and ww < wrap_width: wrap_width = ww elif 12 < ww and wrap_width <= 0: wrap_width = ww if wrap_width < 12: wrap_width = max_width nw = len(wraps) for i, a in zip(range(nc), alignment): if a and isinstance(a, str): al = a.lower() if al in left: table_instance.justify_columns[i] = 'left' elif al in right: table_instance.justify_columns[i] = 'right' elif al in center: table_instance.justify_columns[i] = 'center' elif al in wrapped: maxw = table_instance.column_max_width(i) if 15 < wrap_width * nw < maxw: maxw = (wrap_width - 3) * nw if 1 < nw else wrap_width if maxw < 15: maxw = (wrap_width - 3) * nw if 1 < nw else wrap_width if maxw < 15: maxw = (max_width - 3) * nw if 1 < nw else max_width width = maxw / nw if 1 < nw else maxw for l, line in enumerate(table_instance.table_data): if width < len(line[i]): table_instance.table_data[l][i] = textwrap.fill( indent + line[i], wrap_width) return add_prefix(table_instance.table, prefix)
numpy.sqrt(solve([[50]], [1000])), solve([[1]], [numpy.log2(1000)]) ], [ 60, solve([[1000]], [60000]), numpy.sqrt(solve([[50]], [60000])), solve([[1]], [numpy.log2(60000)]) ], [ 3600, solve([[1000]], [3600000]), numpy.sqrt(solve([[50]], [3600000])), solve([[1]], [numpy.log2(3600000)]) ]] tableR1 = terminaltables.SingleTable(timesR1, title="Rechner 1") print(tableR1.table) print("\n\nAufgabe b:\n") timesR2 = [[ 'Time in secconds', 'Operations Algorithm 1000*n', 'Operations Algorithm 50*n**2', 'Operations Algorithm 2**n' ], [ 1, solve([[1000]], [10000]), numpy.sqrt(solve([[50]], [10000])), solve([[1]], [numpy.log2(10000)]) ], [ 60,
def _render_pretty(self, data): rows = self._compute_rows(RenderTarget.pretty, data) rows.insert(0, [column.title for column in self._flatten_columns( self.visible_columns)]) return terminaltables.SingleTable(rows).table
]) buy_orders.sort(key=lambda order: order[0]) buy_orders.insert(0, [ colorclass.Color('Item'), colorclass.Color('Rank'), colorclass.Color('Max'), colorclass.Color('Qty'), colorclass.Color('90 day avg'), colorclass.Color('48 hr avg'), colorclass.Color('Price'), colorclass.Color('Highest') ]) output = terminaltables.SingleTable( buy_orders, colorclass.Color(' {hicyan}' + user + '\'s bids{/cyan} ')) output.inner_heading_row_border = True output.inner_row_border = True output.justify_columns = { 0: 'left', 1: 'right', 4: 'right', 5: 'right', 6: 'right', 7: 'right' } print(output.table)
def generate_via_luigi(p): logger.info('Generating') all_tasks = {} all_regions = get_regions() products_by_region = {} version_pipelines_to_build = [] for portfolio_file_name in os.listdir(p): if '.yaml' in portfolio_file_name: p_name = portfolio_file_name.split(".")[0] output_path = os.path.sep.join([constants.OUTPUT, p_name]) portfolios_file_path = os.path.sep.join([p, portfolio_file_name]) portfolios = generate_portfolios(portfolios_file_path) for region in all_regions: for portfolio in portfolios.get('Portfolios', []): create_portfolio_task_args = { "region": region, "portfolio_group_name": p_name, "display_name": portfolio.get('DisplayName'), "description": portfolio.get('Description'), "provider_name": portfolio.get('ProviderName'), "associations": portfolio.get('Associations'), "tags": portfolio.get('Tags'), } create_portfolio_task = luigi_tasks_and_targets.CreatePortfolioTask( **create_portfolio_task_args ) all_tasks[f"portfolio_{p_name}_{portfolio.get('DisplayName')}-{region}"] = create_portfolio_task nested_products = portfolio.get('Products', []) + portfolio.get('Components', []) for product in nested_products: product_uid = f"{product.get('Name')}" if products_by_region.get(product_uid) is None: products_by_region[product_uid] = {} create_product_task_args = { "region": region, "name": product.get('Name'), "owner": product.get('Owner'), "description": product.get('Description'), "distributor": product.get('Distributor'), "support_description": product.get('SupportDescription'), "support_email": product.get('SupportEmail'), "support_url": product.get('SupportUrl'), "tags": product.get('Tags'), "uid": "-".join([ create_portfolio_task_args.get('portfolio_group_name'), create_portfolio_task_args.get('display_name'), product.get('Name'), ]) } products_by_region[product_uid][region] = create_product_task_args create_product_task = luigi_tasks_and_targets.CreateProductTask( **create_product_task_args ) all_tasks[ f"product_{p_name}_{portfolio.get('DisplayName')}_{product.get('Name')}-{region}" ] = create_product_task associate_product_with_portfolio_task = luigi_tasks_and_targets.AssociateProductWithPortfolioTask( region=region, portfolio_args=create_portfolio_task_args, product_args=create_product_task_args, ) all_tasks[ f"association_{p_name}_{portfolio.get('DisplayName')}_{product.get('Name')}-{region}" ] = associate_product_with_portfolio_task for version in product.get('Versions', []): ensure_product_version_details_correct_task = luigi_tasks_and_targets.EnsureProductVersionDetailsCorrect( region=region, version=version, product_args=create_product_task_args, ) version_pipelines_to_build.append({ 'create_product_task_args': create_product_task_args, 'product':product, 'version':version, }) all_tasks[ f"version_{p_name}_{portfolio.get('Name')}_{product.get('Name')}_{version.get('Name')}-{region}" ] = ensure_product_version_details_correct_task for product in portfolios.get('Products', []): product_uid = f"{product.get('Name')}" if products_by_region.get(product_uid) is None: products_by_region[product_uid] = {} create_product_task_args = { "region": region, "name": product.get('Name'), "owner": product.get('Owner'), "description": product.get('Description'), "distributor": product.get('Distributor'), "support_description": product.get('SupportDescription'), "support_email": product.get('SupportEmail'), "support_url": product.get('SupportUrl'), "tags": product.get('Tags'), "uid": product.get('Name'), } products_by_region[product_uid][region] = create_product_task_args create_product_task = luigi_tasks_and_targets.CreateProductTask( **create_product_task_args ) for portfolio in product.get('Portfolios', []): create_portfolio_task_args = all_tasks[f"portfolio_{p_name}_{portfolio}-{region}"].param_kwargs associate_product_with_portfolio_task = luigi_tasks_and_targets.AssociateProductWithPortfolioTask( region=region, portfolio_args=create_portfolio_task_args, product_args=create_product_task_args, ) all_tasks[ f"association_{portfolio}_{product.get('Name')}-{region}" ] = associate_product_with_portfolio_task for version in product.get('Versions', []): version_pipelines_to_build.append({ 'create_product_task_args': create_product_task_args, 'product': product, 'version': version, }) ensure_product_version_details_correct_task = luigi_tasks_and_targets.EnsureProductVersionDetailsCorrect( region=region, version=version, product_args=create_product_task_args, ) all_tasks[ f"version_{product.get('Name')}_{version.get('Name')}-{region}" ] = ensure_product_version_details_correct_task all_tasks[f"product_{p_name}-{region}"] = create_product_task logger.info("Going to create pipeline tasks") for version_pipeline_to_build in version_pipelines_to_build: product_name = version_pipeline_to_build.get('product').get('Name') create_args = { "all_regions": all_regions, "version": version_pipeline_to_build.get('version'), "product": version_pipeline_to_build.get('product'), "products_args_by_region": products_by_region.get(product_name), } t = luigi_tasks_and_targets.CreateVersionPipelineTemplateTask( **create_args ) logger.info(f"created pipeline_template_{product_name}-{version_pipeline_to_build.get('version').get('Name')}") all_tasks[f"pipeline_template_{product_name}-{version_pipeline_to_build.get('version').get('Name')}"] = t t = luigi_tasks_and_targets.CreateVersionPipelineTask( **create_args ) logger.info(f"created pipeline_{product_name}-{version_pipeline_to_build.get('version').get('Name')}") all_tasks[f"pipeline_{product_name}-{version_pipeline_to_build.get('version').get('Name')}"] = t for type in ["failure", "success", "timeout", "process_failure", "processing_time", "broken_task", ]: os.makedirs(Path(constants.RESULTS_DIRECTORY) / type) run_result = luigi.build( all_tasks.values(), local_scheduler=True, detailed_summary=True, workers=10, log_level='INFO', ) table_data = [ ['Result', 'Task', 'Significant Parameters', 'Duration'], ] table = terminaltables.SingleTable(table_data) for filename in glob('results/processing_time/*.json'): result = json.loads(open(filename, 'r').read()) table_data.append([ colorclass.Color("{green}Success{/green}"), result.get('task_type'), yaml.safe_dump(result.get('params_for_results')), result.get('duration'), ]) click.echo(table.table) for filename in glob('results/failure/*.json'): result = json.loads(open(filename, 'r').read()) click.echo(colorclass.Color("{red}"+result.get('task_type')+" failed{/red}")) click.echo(f"{yaml.safe_dump({'parameters':result.get('task_params')})}") click.echo("\n".join(result.get('exception_stack_trace'))) click.echo('')
def _render_pretty(self, columns, rows): rows.insert(0, [column.title for column in columns]) return terminaltables.SingleTable(rows).table
def print_diff_table(self): # Print diff table by calling SingleTable function of terminaltables module diff_table = terminaltables.SingleTable(self.diff_table) diff_table.title = 'Backward Difference Table' print(diff_table.table)
elif fixture['difficulty'] <= 3: opponent = colorama.Fore.YELLOW + fixture['opponent_name'] + colorama.Style.RESET_ALL else: opponent = colorama.Fore.RED + fixture['opponent_name'] + colorama.Style.RESET_ALL if fixture['event_name'] not in temp_player: temp_player[fixture['event_name']] = opponent else: temp_player[fixture['event_name']] = temp_player[fixture['event_name']] + '\n' + opponent result.append(temp_player) headers = ['Player'] # Get the upcoming gameweek gw = my_team['entry']['current_event'] + 1 max_gw = gw + 5 while (gw <= 38) and (gw <= max_gw): headers.append('Gameweek ' + str(gw)) gw += 1 data = [] data.append(headers) for player in result: row = [] row.append(player['name']) for gameweek in headers[1:]: row.append(player[gameweek]) data.append(row) table = terminaltables.SingleTable(data, title=colorama.Back.WHITE + colorama.Fore.BLACK + 'Upcoming Fixtures' + colorama.Style.RESET_ALL) table.inner_row_border = True for i in range(len(headers)): table.justify_columns[i] = 'center' print(table.table)
def get_pretty_table_from_dict(attributes_titles, dictionary, title, attributes_to_print_order, table_type='AsciiTable'): """Function convert dictionary to pretty table. :param attributes_titles: list, headers of table, must be bigger by one then attributes_to_print :param dictionary: dict, dictionary in next format: (for example legendary boxers table :) ) { "Mike Tyson": { "Total_fights": 58, "Wins": 50, ... }, "Muhammad Ali": { "Total fights": 61, "Wins": 56, ... }, ... } In this way dictionary has string as keys and dict as values. Every value as dict must have similar keys which must be written in keys rules which set order of table columns :param title: title of table :param attributes_to_print_order: list, list of attributes which set order of cols in tables :param table_type: type of table from terminaltables in terminaltables==3.1.0 have 3 variants with title: AsciiTable, DoubleTable, SingleTable For the more information please read terminaltables documentation :return: str, pretty table for print, for example is: # table_type == 'AsciiTable' # attributes_titles == ["Boxer name", "Total fights", "Wins"] # attributes_to_print_order == ["Total_fights", "Wins"] # title == "Legendary Boxers" +Legendary Boxers-------+----------------------+----------------------+----------------------+ | Boxer name | Total fights | Wins | ... | +-----------------------+----------------------+----------------------+----------------------+ | Muhammad Ali | 61 | 56 | ... | | Mike Tyson | 58 | 50 | ... | | ... | ... | ... | ... | +-----------------------+----------------------+----------------------+----------------------+ """ if not (len(attributes_titles) - len(attributes_to_print_order) == 1): raise AttributeError( 'lenght of header must be bigger for one then len keys_rules') for key, value in dictionary.items(): subdict_keys = set(value.keys()) if subdict_keys.difference(set(attributes_to_print_order)): raise AttributeError( 'keys_rules must be equal subdicts in dictionary') valid_table_names = ['AsciiTable', 'DoubleTable', 'SingleTable'] if table_type not in valid_table_names: raise AttributeError('unknown table type, set one of: {}'.format( ', '.join(valid_table_names))) table = [attributes_titles] for d_key, d_value in dictionary.items(): raw = [d_key] for key in attributes_to_print_order: # user can use list or other structures raw.append(str(d_value[key])) table.append(raw) if table_type == 'AsciiTable': table_instance = terminaltables.AsciiTable(table, title) if table_type == 'DoubleTable': table_instance = terminaltables.DoubleTable(table, title) if table_type == 'SingleTable': table_instance = terminaltables.SingleTable(table, title) return table_instance.table