# AUTOGENERATED FILE! PLEASE DON'T EDIT
"""
For operations that feel like the termination
"""
from collections import defaultdict
from typing import Iterator, Any
from k1lib.bioinfo.cli.init import BaseCli, Table
import k1lib.bioinfo.cli as cli
__all__ = ["stdout", "file", "pretty", "display", "headOut"]
[docs]class stdout(BaseCli):
"""Prints out all lines. If not iterable, then print out the input raw"""
[docs] def __ror__(self, it:Iterator[str]):
try:
it = iter(it)
for line in it: print(line)
except TypeError: print(it)
[docs]class file(BaseCli):
def __init__(self, fileName:str):
super().__init__(); self.fileName = fileName
[docs] def __ror__(self, it:Iterator[str]) -> None:
super().__ror__(it)
with open(self.fileName, "w") as f:
for line in it: f.write(f"{line}\n")
[docs]class pretty(BaseCli):
"""Pretty prints a table"""
[docs] def __ror__(self, it:Table[Any]) -> Iterator[str]:
table = []; widths = defaultdict(lambda: 0)
for row in it:
_row = []
for i, e in enumerate(row):
_row.append(e := f"{e}")
widths[i] = max(len(e), widths[i])
table.append(_row)
for row in table:
s = ""
for w, e in zip(widths.values(), row):
s += e.rstrip(" ").ljust(w+3)
yield s
[docs]def display(lines:int=10):
"""Convenience method for displaying a table"""
f = pretty() | stdout()
if lines is None: return f
else: return cli.head(lines) | f
[docs]def headOut(lines:int=10):
"""Convenience method for head() | stdout()"""
if lines is None: return stdout()
else: return cli.head(lines) | stdout()