k1lib.fmt module

This module is for color formats, units and whatnot. This is exposed automatically with:

from k1lib.imports import *
fmt.txt # exposed
k1lib.fmt.generic(x, units: Dict[int, str])[source]
k1lib.fmt.size(_bytes=0)[source]

Formats disk size. Example:

# returns "50.0 bytes"
fmt.size(50)
# returns "12.0 MB"
fmt.size(1.2e7)
k1lib.fmt.sizeOf(l: Iterator[float]) Tuple[str, Iterator[float]][source]

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
k1lib.fmt.comp(flop=0)[source]

Formats computation amount. Example:

# returns "50.0 FLOPs"
fmt.computation(50)
# returns "50.0 MFLOPs"
fmt.computation(5e7)
k1lib.fmt.compRate(flops=0)[source]

Formats computation rate. Example:

# returns "50.0 FLOPS"
fmt.computationRate(50)
# returns "50.0 MFLOPS"
fmt.computationRate(5e7)
k1lib.fmt.time(seconds=0)[source]

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"
k1lib.fmt.item(n=0)[source]

Formats generic item. Example:

# returns "50.0"
fmt.item(50)
# returns "500.0 k"
fmt.item(5e5)
k1lib.fmt.throughput(n, unit='')[source]

Formats item throughput. Example:

# returns "3.16/year"
fmt.throughput(1e-7)
# returns "2.63/month"
fmt.throughput(1e-6)
# returns "3.6/hour"
fmt.throughput(1e-3)
# returns "100.0 k/s"
throughput(1e5)

# returns "100.0 k epochs/s"
throughput(1e5, " epochs")
Parameters
  • n – items per second

  • unit – optional item unit

class k1lib.fmt.txt[source]

Bases: object

Text formatting. Example:

# will print out red text
print(fmt.txt.red("some text"))
static darkcyan(s: str)[source]
static red(s: str)[source]
static green(s: str)[source]
static yellow(s: str)[source]
static blue(s: str)[source]
static purple(s: str)[source]
static cyan(s: str)[source]
static bold(s: str)[source]
static grey(s: str)[source]
static darkgrey(s: str)[source]
static underline(s: str)[source]
static identity(s: str)[source]
class k1lib.fmt.code[source]

Bases: object

python()[source]

Formats Python code. Example:

fmt.code.python("""
def f(x:int):
    return x + 3
""") | aS(IPython.display.HTML)
k1lib.fmt.h(code: str, level: int = 1) str[source]

Wraps content inside a ‘h’ html tag. Example:

fmt.h("abc", 2) # returns "<h2>abc</h2>"
Parameters

level – what’s the header level?

k1lib.fmt.pre(code: str) str[source]

Wraps content inside a ‘pre’ html tag. Example:

fmt.pre("abc")
k1lib.fmt.row(args)[source]

Creates a html row of all the elements. Example:

fmt.row(["abc", "def"]) | aS(IPython.display.HTML)
k1lib.fmt.col(args)[source]

Creates a html col of all the elements. Example:

fmt.col(["abc", "def"]) | aS(IPython.display.HTML)