Python tabulate.PRESERVE_WHITESPACE Examples
The following are 4
code examples of tabulate.PRESERVE_WHITESPACE().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tabulate
, or try the search function
.
Example #1
Source File: test_output.py From python-tabulate with MIT License | 6 votes |
def test_preserve_whitespace(): "Output: Default table output, but with preserved leading whitespace." tabulate_module.PRESERVE_WHITESPACE = True table_headers = ["h1", "h2", "h3"] test_table = [[" foo", " bar ", "foo"]] expected = "\n".join( ["h1 h2 h3", "----- ------- ----", " foo bar foo"] ) result = tabulate(test_table, table_headers) assert_equal(expected, result) tabulate_module.PRESERVE_WHITESPACE = False table_headers = ["h1", "h2", "h3"] test_table = [[" foo", " bar ", "foo"]] expected = "\n".join(["h1 h2 h3", "---- ---- ----", "foo bar foo"]) result = tabulate(test_table, table_headers) assert_equal(expected, result)
Example #2
Source File: lib.py From johnnydep with MIT License | 5 votes |
def serialise(self, fields=("name", "summary"), recurse=True, format=None): if format == "pinned": # user-specified fields are ignored/invalid in this case fields = ("pinned",) data = [OrderedDict([(f, getattr(self, f, None)) for f in fields])] if format == "human": table = gen_table(self, extra_cols=fields) tabulate.PRESERVE_WHITESPACE = True return tabulate.tabulate(table, headers="keys") if recurse and self.requires: deps = flatten_deps(self) next(deps) # skip over root data += [d for dep in deps for d in dep.serialise(fields=fields, recurse=False)] if format is None or format == "python": result = data elif format == "json": result = json.dumps(data, indent=2, default=str, separators=(",", ": ")) elif format == "yaml": result = oyaml.dump(data) elif format == "toml": result = "\n".join([toml.dumps(d) for d in data]) elif format == "pinned": result = "\n".join([d["pinned"] for d in data]) else: raise Exception("Unsupported format") return result
Example #3
Source File: tabulate_adapter.py From cli_helpers with BSD 3-Clause "New" or "Revised" License | 5 votes |
def adapter(data, headers, table_format=None, preserve_whitespace=False, **kwargs): """Wrap tabulate inside a function for TabularOutputFormatter.""" keys = ('floatfmt', 'numalign', 'stralign', 'showindex', 'disable_numparse') tkwargs = {'tablefmt': table_format} tkwargs.update(filter_dict_by_key(kwargs, keys)) if table_format in supported_markup_formats: tkwargs.update(numalign=None, stralign=None) tabulate.PRESERVE_WHITESPACE = preserve_whitespace return iter(tabulate.tabulate(data, headers, **tkwargs).split('\n'))
Example #4
Source File: parameter_count.py From fvcore with Apache License 2.0 | 4 votes |
def parameter_count_table(model: nn.Module, max_depth: int = 3) -> str: """ Format the parameter count of the model (and its submodules or parameters) in a nice table. Args: model: a torch module max_depth (int): maximum depth to recursively print submodules or parameters Returns: str: the table to be printed """ count: typing.DefaultDict[str, int] = parameter_count(model) param_shape: typing.Dict[str, typing.Tuple] = { k: tuple(v.shape) for k, v in model.named_parameters() } table: typing.List[typing.Tuple] = [] def format_size(x: int) -> str: # pyre-fixme[6]: Expected `int` for 1st param but got `float`. # pyre-fixme[6]: Expected `int` for 1st param but got `float`. if x > 1e5: return "{:.1f}M".format(x / 1e6) # pyre-fixme[6]: Expected `int` for 1st param but got `float`. # pyre-fixme[6]: Expected `int` for 1st param but got `float`. if x > 1e2: return "{:.1f}K".format(x / 1e3) return str(x) def fill(lvl: int, prefix: str) -> None: if lvl >= max_depth: return for name, v in count.items(): if name.count(".") == lvl and name.startswith(prefix): indent = " " * (lvl + 1) if name in param_shape: table.append((indent + name, indent + str(param_shape[name]))) else: table.append((indent + name, indent + format_size(v))) fill(lvl + 1, name + ".") table.append(("model", format_size(count.pop("")))) fill(0, "") old_ws = tabulate.PRESERVE_WHITESPACE tabulate.PRESERVE_WHITESPACE = True tab = tabulate.tabulate( table, headers=["name", "#elements or shape"], tablefmt="pipe" ) tabulate.PRESERVE_WHITESPACE = old_ws return tab