Skip to content

Common data types

Common data types

Classes:

Name Description
MemoryUsed

Memory data class, handles the various string formats (K, M, G postfix) for uniform processing.

Number

Class for capturing certain number information in the JSON output.

PostFixUnit

Handle postfix units (K, M, G) mostly used for frequency.

Attributes:

Name Type Description
CPU_TIME_RE

Regex to parse time format D-HH:MM:SS

CPU_TIME_RE

CPU_TIME_RE = "^(?:(?P<days>\\d+)-)?(?P<hours>\\d+):(?P<minutes>\\d+):(?P<seconds>\\d+)$"

Regex to parse time format D-HH:MM:SS

MemoryUsed

MemoryUsed(value: str | int | float | None = None)

Memory data class, handles the various string formats (K, M, G postfix) for uniform processing. Value is stored as MiB and converted when needed.

Methods:

Name Description
from_mb

Create from MiB float value.

Attributes:

Name Type Description
GB float

Get the value in GiB

MB float

Get the value in MiB

Source code in src/slurm_viewer/data/common_types.py
def __init__(self, value: str | int | float | None = None) -> None:
    self.value: float | None = None
    if value is None:
        self.value = None
        return

    if isinstance(value, str):
        if value.endswith('K'):
            self.value = float(value[:-1]) / 1024
            return

        if value.endswith('M'):
            self.value = float(value[:-1])
            return

        if value.endswith('G'):
            self.value = float(value[:-1]) * 1024
            return

    try:
        self.value = float(value) / (1024 * 1024)
    except ValueError:
        self.value = None

GB

GB: float

Get the value in GiB

MB

MB: float

Get the value in MiB

from_mb

from_mb(value: float) -> MemoryUsed

Create from MiB float value.

Source code in src/slurm_viewer/data/common_types.py
@classmethod
def from_mb(cls, value: float) -> MemoryUsed:
    """ Create from MiB float value. """
    val = cls()
    val.value = value
    return val

Number

Bases: BaseModel

Class for capturing certain number information in the JSON output.

PostFixUnit

PostFixUnit(value: str)

Handle postfix units (K, M, G) mostly used for frequency.

Source code in src/slurm_viewer/data/common_types.py
def __init__(self, value: str) -> None:
    self.value: float | None = None
    if value.endswith('K'):
        self.value = float(value[:-1]) / 1024
        return

    if value.endswith('M'):
        self.value = float(value[:-1])
        return

    if value.endswith('G'):
        self.value = float(value[:-1]) * 1024
        return

    try:
        self.value = float(value) / (1024 * 1204)
    except ValueError:
        self.value = None