# 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"]
class _Stdout(BaseCli):
"""Prints out all lines"""
def __ror__(self, it:Iterator[str]):
for line in it: print(line)
def __call__(self): return self
stdout = _Stdout()
[docs]class file(BaseCli):
def __init__(self, fileName:str): self.fileName = fileName
[docs] def __ror__(self, it:Iterator[str]) -> None:
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