def ptable(self, rows, columns, grid_args, row_stylist): """Format tabular data for pretty-printing as a fixed-width table and then display it using a pager. :param rows: required argument - can be a list-of-lists (or another iterable of iterables), a two-dimensional NumPy array, or an Iterable of non-iterable objects :param columns: column headers and formatting options per column :param grid_args: argparse arguments for formatting the grid :param row_stylist: function to determine how each row gets styled """ if grid_args.color: grid = tf.AlternatingRowGrid(BACK_PRI, BACK_ALT) elif grid_args.fancy: grid = tf.FancyGrid() elif grid_args.sparse: grid = tf.SparseGrid() else: grid = None transpose = False if grid_args.transpose: transpose = True formatted_table = tf.generate_table(rows=rows, columns=columns, grid_style=grid, row_tagger=row_stylist, transpose=transpose) self.ppaged(formatted_table, chop=True)
def _commands_plugin_show_all(self, limit: int) -> None: """ Shows all commands """ max_cmd_w, max_out_w = available_max_width_on_screen_for_commands( self.selected_client.max_commands_width, self.selected_client.max_outputs_width) table = tf.generate_table( grid_style=tf.FancyGrid(), columns=['ID', 'Status', 'Created On', 'Command', 'Output'], rows=[ cmd.to_table_list(max_cmd_w, max_out_w) for cmd in self.selected_client.reversed_commands[:limit] ]) self.ppaged(table, chop=True)
def test_basic_fancy_grid(rows): expected = ''' ╔════╤════╤════╤════╗ ║ A1 │ A2 │ A3 │ A4 ║ ╟────┼────┼────┼────╢ ║ B1 │ B2 │ B3 │ B4 ║ ║ │ B2 │ │ ║ ║ │ B2 │ │ ║ ╟────┼────┼────┼────╢ ║ C1 │ C2 │ C3 │ C4 ║ ╟────┼────┼────┼────╢ ║ D1 │ D2 │ D3 │ D4 ║ ╚════╧════╧════╧════╝ '''.lstrip('\n') table = tf.generate_table(rows, grid_style=tf.FancyGrid()) assert table == expected
def test_table_with_header_transposed_fancy(rows, cols): expected = ''' ╔══════╦════╤════╤════╤════╗ ║ Col1 ║ A1 │ B1 │ C1 │ D1 ║ ╟──────╫────┼────┼────┼────╢ ║ Col2 ║ A2 │ B2 │ C2 │ D2 ║ ║ ║ │ B2 │ │ ║ ║ ║ │ B2 │ │ ║ ╟──────╫────┼────┼────┼────╢ ║ Col3 ║ A3 │ B3 │ C3 │ D3 ║ ╟──────╫────┼────┼────┼────╢ ║ Col4 ║ A4 │ B4 │ C4 │ D4 ║ ╚══════╩════╧════╧════╧════╝ '''.lstrip('\n') table = tf.generate_table(rows, cols, grid_style=tf.FancyGrid(), transpose=True) assert table == expected
def test_object_table_fancy_grid(obj_rows, obj_cols): expected = ''' ╔══════╤══════╤══════╤══════╗ ║ Col1 │ Col2 │ Col3 │ Col4 ║ ╠══════╪══════╪══════╪══════╣ ║ A1 │ A2 │ A3 │ A4 ║ ╟──────┼──────┼──────┼──────╢ ║ B1 │ B2 │ B3 │ B4 ║ ║ │ B2 │ │ ║ ║ │ B2 │ │ ║ ╟──────┼──────┼──────┼──────╢ ║ C1 │ C2 │ C3 │ C4 ║ ╟──────┼──────┼──────┼──────╢ ║ D1 │ D2 │ D3 │ D4 ║ ╚══════╧══════╧══════╧══════╝ '''.lstrip('\n') table = tf.generate_table(obj_rows, obj_cols, grid_style=tf.FancyGrid()) assert table == expected
def _client_plugin_handle_show_all(self, limit: int) -> None: """ Shows all clients in the database """ clients = list(Client.objects.order_by('-registered_on')[:limit]) max_last_seen = 0 if not clients else max([ len(str(client.humanized_last_seen)) for client in clients ]) max_user_agent_width = available_max_width_on_screen_for_clients(max_last_seen) table = tf.generate_table( grid_style=tf.FancyGrid(), columns=['ID', 'User Agent', 'Registered On', 'Last Seen'], rows=[ client.to_table_list(max_user_agent_width) for client in clients ] ) self.ppaged(table, chop=True)
#!/usr/bin/env python3 # coding=utf-8 import tableformatter as tf from tableformatter import generate_table rows = [('A1', 'A2', 'A3', 'A4'), ('B1', 'B2\nB2\nB2', 'B3', 'B4'), ('C1', 'C2', 'C3', 'C4'), ('D1', 'D2', 'D3', 'D4')] columns = ('Col1', 'Col2', 'Col3', 'Col4') tf.TableColors.set_color_library('None') with open('table_none.txt', mode='w') as outfile: print(generate_table(rows, columns, grid_style=tf.FancyGrid()), file=outfile) tf.TableColors.set_color_library('colorama') with open('table_colorama.txt', mode='w') as outfile: print(generate_table(rows, columns, grid_style=tf.FancyGrid()), file=outfile) tf.TableColors.set_color_library('colored') with open('table_colored.txt', mode='w') as outfile: print(generate_table(rows, columns, grid_style=tf.FancyGrid()), file=outfile)
question_column = find_column_with_first_row_equal_to(ws, question_name) if question_column <= 0 or question_column is None: print("Invalid question name: " + question_name + ". Is there a column named " + question_name + " in the Excel file with the students' answers?") quit() # the path to the file containing the text of the proposed answer proposed_answer_location = str(sys.argv[5]) summary_rows = [("Date", time.strftime("%a, %d %b %Y %H:%M:%S")), ("SQLite Version", runcommand("sqlite3 --version")), ("Sample database script", database_script_path_sample), ("Complete database script", database_script_path)] print(tf.generate_table(summary_rows, grid_style=tf.FancyGrid())) StatesPriority = {} currentRow = 2 with open(proposed_answer_location, 'r') as f: proposed_answer = f.read() for row in ws.iter_rows(min_row=start_row): firstname = ws.cell(row=currentRow, column=firstname_column).value surname = ws.cell(row=currentRow, column=surname_column).value email = ws.cell(row=currentRow, column=email_column).value # remove non breaking spaces student_answer = ws.cell(row=currentRow, column=question_column).value if student_answer is not None:
import tableformatter as tf from tableformatter import generate_table rows = [('A1', 'A2', 'A3', 'A4'), ('B1', 'B2\nB2\nB2', 'B3', 'B4'), ('C1', 'C2', 'C3', 'C4'), ('D1', 'D2', 'D3', 'D4')] columns = ('Col1', 'Col2', 'Col3', 'Col4') print("Basic Table, default style (AlternatingRowGrid):") print(generate_table(rows)) print("Basic Table, transposed, default style (AlternatingRowGrid):") print(generate_table(rows, transpose=True)) print("Basic Table, FancyGrid:") print(generate_table(rows, grid_style=tf.FancyGrid())) print("Basic Table, SparseGrid:") print(generate_table(rows, grid_style=tf.SparseGrid())) print("Table with header, AlteratingRowGrid:") print(generate_table(rows, columns)) print("Table with header, transposed, AlteratingRowGrid:") print(generate_table(rows, columns, transpose=True)) print("Table with header, transposed, FancyGrid:") print(generate_table(rows, columns, grid_style=tf.FancyGrid(), transpose=True)) print("Table with header, transposed, SparseGrid:") print(generate_table(rows, columns, grid_style=tf.SparseGrid(),
try: imp.find_module('befe_config') import befe_config as befe_config except ImportError: print(Colors.RED + "befe_config.py not found" + Colors.ENDC) print( Colors.RED + "Please make a copy of the befe_config_example.py and name it befe_config.py, and edit it as needed to reflect the configuration of your setup" + Colors.ENDC) print( Colors.RED + "In most cases the example config without modifications will work as a starting point" + Colors.ENDC) exit(1) FULL_TABLE_GRID_STYLE = tf.FancyGrid() DEFAULT_TABLE_GRID_STYLE = tf.AlternatingRowGrid() def get_befe_scripts_dir(): scripts_dir = os.environ.get('BEFE_SCRIPT_DIR') return scripts_dir def get_config(config_name): return eval("befe_config." + config_name) def config_exists(config_name): return hasattr(befe_config, config_name)