def test_smart_tables(self): """Test :func:`humanfriendly.tables.format_smart_table()`.""" column_names = ['One', 'Two', 'Three'] data = [['1', '2', '3'], ['a', 'b', 'c']] assert ansi_strip(format_smart_table(data, column_names)) == dedent(""" --------------------- | One | Two | Three | --------------------- | 1 | 2 | 3 | | a | b | c | --------------------- """).strip() column_names = ['One', 'Two', 'Three'] data = [['1', '2', '3'], ['a', 'b', 'Here comes a\nmulti line column!']] assert ansi_strip(format_smart_table(data, column_names)) == dedent(""" ------------------ One: 1 Two: 2 Three: 3 ------------------ One: a Two: b Three: Here comes a multi line column! ------------------ """).strip()
def model_info(magpie): """ Returns the options available for modelling and predicting the performance of an Apache Heron topology.""" LOG.debug("Requesting topology performance model information") try: response: requests.Response = requests.get( magpie.url + "/model/topology/heron/model_info") except requests.exceptions.ConnectionError: LOG.error( "Unable to connect to Magpie server at: %s, is the server active?", magpie.url, ) else: models: List[Dict[str, str]] = response.json() data: List[List[str]] = [] headings = ["Name", "Description"] for model in models: data.append([model["name"], model["description"]]) click.echo( "\nAvailable performance models for Apache Heron topologies:\n") click.echo(format_smart_table(data, headings))
def report_available_mirrors(updater): """Print the available mirrors to the terminal (in a human friendly format).""" if connected_to_terminal(): have_bandwidth = any(c.bandwidth for c in updater.ranked_mirrors) have_last_updated = any(c.last_updated is not None for c in updater.ranked_mirrors) column_names = ["Rank", "Mirror URL", "Available?", "Updating?"] if have_last_updated: column_names.append("Last updated") if have_bandwidth: column_names.append("Bandwidth") data = [] for i, candidate in enumerate(updater.ranked_mirrors, start=1): row = [ i, candidate.mirror_url, "Yes" if candidate.is_available else "No", "Yes" if candidate.is_updating else "No" ] if have_last_updated: row.append("Up to date" if candidate.last_updated == 0 else ( "%s behind" % format_timespan(candidate.last_updated) if candidate. last_updated else "Unknown")) if have_bandwidth: row.append("%s/s" % format_size(round(candidate.bandwidth, 2)) if candidate.bandwidth else "Unknown") data.append(row) output(format_smart_table(data, column_names=column_names)) else: output(u"\n".join( candidate.mirror_url for candidate in updater.ranked_mirrors if candidate.is_available and not candidate.is_updating))
def handle(self, *args, **options): """List all the entries from a catalog name.""" if not options.get('catalog'): raise CommandError( "listexperimentsbycatalog wants at least one named argument") else: catalog_name = options['catalog'] column_names = ['Experiment Name'] experiments = (cat.xperiments.values('name') for cat in Catalog.objects.filter(name=catalog_name)) explist = list() for xperiment in experiments: for exp_kws in xperiment: explist.append([ exp_kws['name'], ]) self.stdout.write(format_smart_table(explist, column_names)) logging.info('Command executed successfully!')
def demonstrate_ansi_formatting(): """Demonstrate the use of ANSI escape sequences.""" # First we demonstrate the supported text styles. output('%s', ansi_wrap('Text styles:', bold=True)) styles = ['normal', 'bright'] styles.extend(ANSI_TEXT_STYLES.keys()) for style_name in sorted(styles): options = dict(color=HIGHLIGHT_COLOR) if style_name != 'normal': options[style_name] = True style_label = style_name.replace('_', ' ').capitalize() output(' - %s', ansi_wrap(style_label, **options)) # Now we demonstrate named foreground and background colors. for color_type, color_label in (('color', 'Foreground colors'), ('background', 'Background colors')): intensities = [ ('normal', dict()), ('bright', dict(bright=True)), ] if color_type != 'background': intensities.insert(0, ('faint', dict(faint=True))) output('\n%s' % ansi_wrap('%s:' % color_label, bold=True)) output( format_smart_table( [[color_name] + [ ansi_wrap( 'XXXXXX' if color_type != 'background' else (' ' * 6), **dict(list(kw.items()) + [(color_type, color_name)])) for label, kw in intensities ] for color_name in sorted(ANSI_COLOR_CODES.keys())], column_names=['Color'] + [label.capitalize() for label, kw in intensities])) # Demonstrate support for 256 colors as well. demonstrate_256_colors(0, 7, 'standard colors') demonstrate_256_colors(8, 15, 'high-intensity colors') demonstrate_256_colors(16, 231, '216 colors') demonstrate_256_colors(232, 255, 'gray scale colors')
def demonstrate_ansi_formatting(): """Demonstrate the use of ANSI escape sequences.""" # First we demonstrate the supported text styles. output('%s', ansi_wrap('Text styles:', bold=True)) styles = ['normal', 'bright'] styles.extend(ANSI_TEXT_STYLES.keys()) for style_name in sorted(styles): options = dict(color=HIGHLIGHT_COLOR) if style_name != 'normal': options[style_name] = True style_label = style_name.replace('_', ' ').capitalize() output(' - %s', ansi_wrap(style_label, **options)) # Now we demonstrate named foreground and background colors. for color_type, color_label in (('color', 'Foreground colors'), ('background', 'Background colors')): intensities = [ ('normal', dict()), ('bright', dict(bright=True)), ] if color_type != 'background': intensities.insert(0, ('faint', dict(faint=True))) output('\n%s' % ansi_wrap('%s:' % color_label, bold=True)) output(format_smart_table([ [color_name] + [ ansi_wrap( 'XXXXXX' if color_type != 'background' else (' ' * 6), **dict(list(kw.items()) + [(color_type, color_name)]) ) for label, kw in intensities ] for color_name in sorted(ANSI_COLOR_CODES.keys()) ], column_names=['Color'] + [ label.capitalize() for label, kw in intensities ])) # Demonstrate support for 256 colors as well. demonstrate_256_colors(0, 7, 'standard colors') demonstrate_256_colors(8, 15, 'high-intensity colors') demonstrate_256_colors(16, 231, '216 colors') demonstrate_256_colors(232, 255, 'gray scale colors')
def run(args): people = Person.select().where(Person.profile == args.profile) rows = [(person.name, person.email, encode_key(person.public_key)) for person in people] table = format_smart_table(rows, ('Name', 'Email', 'Public Key')) print(table)