nvitop.utils module

Utilities of nvitop APIs.

nvitop.utils.UINT_MAX: int = 4294967295

The maximum value of ctypes.c_uint.

nvitop.utils.ULONGLONG_MAX: int = 18446744073709551615

The maximum value of ctypes.c_ulonglong.

nvitop.utils.KiB: int = 1024

Kibibyte (1024)

nvitop.utils.MiB: int = 1048576

Mebibyte (1024 * 1024)

nvitop.utils.GiB: int = 1073741824

Gibibyte (1024 * 1024 * 1024)

nvitop.utils.TiB: int = 1099511627776

Tebibyte (1024 * 1024 * 1024 * 1024)

nvitop.utils.PiB: int = 1125899906842624

Pebibyte (1024 * 1024 * 1024 * 1024 * 1024)

nvitop.utils.SIZE_UNITS: dict[str | None, int] = {None: 1, '': 1, 'B': 1, 'KiB': 1024, 'MiB': 1048576, 'GiB': 1073741824, 'TiB': 1099511627776, 'PiB': 1125899906842624, 'KB': 1000, 'MB': 1000000, 'GB': 1000000000, 'TB': 1000000000000, 'PB': 1000000000000000}

Units of storage and memory measurements.

nvitop.utils.bytes2human(b: int | float | NaType, *, min_unit: int = 1) str[source]

Convert bytes to a human readable string.

nvitop.utils.human2bytes(s: int | str) int[source]

Convert a human readable size string (case insensitive) to bytes.

Raises:

ValueError – If cannot convert the given size string.

Examples

>>> human2bytes('500B')
500
>>> human2bytes('10k')
10000
>>> human2bytes('10ki')
10240
>>> human2bytes('1M')
1000000
>>> human2bytes('1MiB')
1048576
>>> human2bytes('1.5GiB')
1610612736
nvitop.utils.timedelta2human(dt: int | float | datetime.timedelta | NaType, *, round: bool = False) str[source]

Convert a number in seconds or a datetime.timedelta instance to a human readable string.

nvitop.utils.utilization2string(utilization: int | float | NaType) str[source]

Convert a utilization rate to string.

nvitop.utils.colored(text: str, color: str | None = None, on_color: str | None = None, attrs: Iterable[str] | None = None) str[source]

Colorize text with ANSI color escape codes.

Available text colors:

red, green, yellow, blue, magenta, cyan, white.

Available text highlights:

on_red, on_green, on_yellow, on_blue, on_magenta, on_cyan, on_white.

Available attributes:

bold, dark, underline, blink, reverse, concealed.

Examples

>>> colored('Hello, World!', 'red', 'on_grey', ['blue', 'blink'])
>>> colored('Hello, World!', 'green')
nvitop.utils.set_color(value: bool) None[source]

Force enable text coloring.

nvitop.utils.boolify(string: str, default: Any | None = None) bool[source]

Convert the given value, usually a string, to boolean.

class nvitop.utils.Snapshot(real: Any, **items: Any)[source]

Bases: object

A dict-like object holds the snapshot values.

The value can be accessed by snapshot.name or snapshot['name'] syntax. The Snapshot can also be converted to a dictionary by dict(snapshot) or {**snapshot}.

Missing attributes will be automatically fetched from the original object.

__init__(real: Any, **items: Any) None[source]

Initialize a new Snapshot object with the given attributes.

__repr__() str[source]

Return a string representation of the snapshot.

__hash__() int[source]

Return a hash value of the snapshot.

__getattr__(name: str) Any[source]

Get a member from the instance.

If the attribute is not defined, fetches from the original object and makes a function call.

__getitem__(name: str) Any[source]

Support snapshot['name'] syntax.

__setitem__(name: str, value: Any) None[source]

Support snapshot['name'] = value syntax.

__iter__() Iterator[str][source]

Support for name in snapshot syntax and * tuple unpack [*snapshot] syntax.

keys() Iterable[str][source]

Support ** dictionary unpack {**snapshot} / dict(**snapshot) syntax and dict(snapshot) dictionary conversion.