本文整理汇总了Python中tabulate.PRESERVE_WHITESPACE属性的典型用法代码示例。如果您正苦于以下问题:Python tabulate.PRESERVE_WHITESPACE属性的具体用法?Python tabulate.PRESERVE_WHITESPACE怎么用?Python tabulate.PRESERVE_WHITESPACE使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类tabulate
的用法示例。
在下文中一共展示了tabulate.PRESERVE_WHITESPACE属性的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_preserve_whitespace
# 需要导入模块: import tabulate [as 别名]
# 或者: from tabulate import PRESERVE_WHITESPACE [as 别名]
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)
示例2: serialise
# 需要导入模块: import tabulate [as 别名]
# 或者: from tabulate import PRESERVE_WHITESPACE [as 别名]
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
示例3: adapter
# 需要导入模块: import tabulate [as 别名]
# 或者: from tabulate import PRESERVE_WHITESPACE [as 别名]
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'))
示例4: parameter_count_table
# 需要导入模块: import tabulate [as 别名]
# 或者: from tabulate import PRESERVE_WHITESPACE [as 别名]
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