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.fromSize(s: str) int[source]

Grabs size from string representation. Example:

fromSize("31.5k") # returns 31500
fromSize("31.5kB") # also returns 31500
k1lib.fmt.dollar(_dollar=0)[source]

Formats dollar. Example:

# returns '50.0 $'
fmt.dollar(50)
# returns '12.0 M$'
fmt.dollar(1.2e7)

I know this format looks kinda terrible, instead of “million” or “billion” written out loud, but that’s long, so let’s still do the short metric prefix

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]
k1lib.fmt.js(s: str) str[source]

Makes javascript code pretty, returns html

k1lib.fmt.py(s: str) str[source]

Makes python code pretty, returns html

k1lib.fmt.html(s: str) str[source]

Makes html code pretty, returns html

k1lib.fmt.sql(s: str) str[source]

Makes sql code pretty, returns html

k1lib.fmt.cpp(s: str) str[source]

Makes cpp code pretty, returns html

k1lib.fmt.java(s: str) str[source]

Makes java code pretty, returns html

k1lib.fmt.php(s: str) str[source]

Makes php code pretty, returns html

k1lib.fmt.ruby(s: str) str[source]

Makes ruby code pretty, returns 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, extras: str = '') str[source]

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

fmt.pre("abc")
k1lib.fmt.row(args, margin=10)[source]

Creates a html row of all the elements. Example:

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

Creates a html col of all the elements. Example:

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

Returns an infinite iterator that cycles through 12 colors. Example:

fmt.colors() | head(3) | deref()

Color scheme taken from https://colorbrewer2.org/#type=qualitative&scheme=Set3&n=12

k1lib.fmt.rmAnsi(text)[source]

Removes ansi escape characters, courtesy of https://stackoverflow.com/questions/14693701/how-can-i-remove-the-ansi-escape-sequences-from-a-string-in-python.