# AUTOGENERATED FILE! PLEASE DON'T EDIT
"""
This module is for color formats, units and whatnot. This is exposed
automatically with::
   from k1lib.imports import *
   fmt.txt # exposed
"""
import k1lib, math; from k1lib import cli
from typing import Dict, Iterator, Tuple
__all__ = ["generic", "metricPrefixes", "size", "sizeOf",
           "comp", "compRate", "time", "item", "txt"]
metricPrefixes = {-8:"y",-7:"z",-6:"a",-5:"f",-4:"p",-3:"n",-2:"u",-1:"m",0:"",1:"k",2:"M",3:"G",4:"T",5:"P",6:"E",7:"Z",8:"Y"}
#metricPrefixes = ["", "k", "M", "G", "T", "P", "E", "Z", "Y"]
[docs]def generic(x, units:Dict[int, str]):
    for i, unit in units.items():
        upperBound = 1000 * 1000**i
        if abs(x) < upperBound:
            return f"{round(1e3*x/upperBound, 2)} {unit}"
    return (f"{round(1e3*x/upperBound, 2)} {unit}").strip() 
sizes = {i: f"{p}B" for i, p in metricPrefixes.items() if i >= 0}; sizes[0] = "bytes"
[docs]def size(_bytes=0):
    """Formats disk size.
Example::
    # returns "50.0 bytes"
    fmt.size(50)
    # returns "12.0 MB"
    fmt.size(1.2e7)
"""
    return generic(_bytes, sizes) 
[docs]def sizeOf(l:Iterator[float]) -> Tuple[str, Iterator[float]]:
    """Figures out appropriate scale, scales back the Iterator, and return both.
Example::
    x = torch.abs(torch.randn(2)) * 1e4 + 1e5
    label, t = fmt.sizeOf(x) # label is "kB"
    (t | toTensor()).min() # min value should be close to 100"""
    l = list(l | cli.apply(lambda n: abs(n)))
    v = l | cli.toMax()
    v = math.log10(v) if v > 0 else -math.log10(-v)
    idx = math.floor(v/3)
    coef = 1.0/1000**idx
    return sizes[idx], l | cli.apply(lambda x: x * coef) | cli.deref() 
computations = {i: f"{p}FLOPs" for i, p in metricPrefixes.items() if i >= 0}
[docs]def comp(flop=0):
    """Formats computation amount.
Example::
    # returns "50.0 FLOPs"
    fmt.computation(50)
    # returns "50.0 MFLOPs"
    fmt.computation(5e7)
"""
    return generic(flop, computations) 
computationRates = {i: f"{p}FLOPS" for i, p in metricPrefixes.items() if i >= 0}
[docs]def compRate(flops=0):
    """Formats computation rate.
Example::
    # returns "50.0 FLOPS"
    fmt.computationRate(50)
    # returns "50.0 MFLOPS"
    fmt.computationRate(5e7)
"""
    return generic(flops, computationRates) 
times = {i:f"{p}s" for i, p in metricPrefixes.items() if i <= 0}
[docs]def time(seconds=0):
    """Formats small times.
Example::
    fmt.time(50) # returns "50.0 s"
    fmt.time(4000) # returns "4000.0 s"
    fmt.time(0.02) # returns "20.0 ms"
    fmt.time(1e-5) # returns "10.0 us"
"""
    return generic(seconds, times) 
items = {0: "", 1: "k", 2: "M", 3: "B", 4: "T"}
[docs]def item(n=0):
    """Formats generic item.
Example::
    # returns "50.0"
    fmt.item(50)
    # returns "500.0 k"
    fmt.item(5e5)
"""
    return generic(n, items).strip() 
_esc = '\033['
_end = f'{_esc}0m'
[docs]class txt:
    """Text formatting.
Example::
    # will print out red text
    print(fmt.txt.red("some text"))"""
[docs]    @staticmethod
    def darkcyan(s:str):  return f"{_esc}36m{s}{_end}" 
[docs]    @staticmethod
    def red(s:str):       return f"{_esc}91m{s}{_end}" 
[docs]    @staticmethod
    def green(s:str):     return f"{_esc}92m{s}{_end}" 
[docs]    @staticmethod
    def yellow(s:str):    return f"{_esc}93m{s}{_end}" 
[docs]    @staticmethod
    def blue(s:str):      return f"{_esc}94m{s}{_end}" 
[docs]    @staticmethod
    def purple(s:str):    return f"{_esc}95m{s}{_end}" 
[docs]    @staticmethod
    def cyan(s:str):      return f"{_esc}96m{s}{_end}" 
[docs]    @staticmethod
    def bold(s:str):      return f"{_esc}1m{s}{_end}" 
[docs]    @staticmethod
    def grey(s:str):      return f"{_esc}38;2;150;150;150m{s}{_end}" 
[docs]    @staticmethod
    def darkgrey(s:str):  return f"{_esc}38;2;100;100;100m{s}{_end}" 
[docs]    @staticmethod
    def underline(s:str): return f"{_esc}4m{s}{_end}" 
[docs]    @staticmethod
    def identity(s:str):  return f"{s}"