Skip to content

Converters

Collection of tools for converting data.

The module contains the following functions:

  • to_human_size(n_bytes) - Returns a humanized string: 200 bytes | 1 KB | 1.5 MB etc.
  • int_to_roman - Convert an integer to Roman.
  • roman_to_int - Convert to integer from Roman.

int_to_roman(number)

Convert an integer to Roman.

Examples:

>>> from xloft import int_to_roman
>>> int_to_roman(1994)
MCMXCIV

Parameters:

Name Type Description Default
number int

Integer.

required

Returns:

Type Description
str

Roman numeral string.

Source code in src/xloft/converters/roman.py
def int_to_roman(number: int) -> str:
    """Convert an integer to Roman.

    Examples:
        >>> from xloft import int_to_roman
        >>> int_to_roman(1994)
        MCMXCIV

    Args:
        number (int): Integer.

    Returns:
        Roman numeral string.
    """
    result = ""
    for arabic, roman in ROMAN:
        (factor, number) = divmod(number, arabic)
        result += roman * factor
    return result

roman_to_int(roman)

Convert to integer from Roman.

Examples:

>>> from xloft import roman_to_int
>>> roman_to_int("MCMXCIV")
1994

Parameters:

Name Type Description Default
roman str

Roman numeral string.

required

Returns:

Type Description
int

Integer.

Source code in src/xloft/converters/roman.py
def roman_to_int(roman: str) -> int:
    """Convert to integer from Roman.

    Examples:
        >>> from xloft import roman_to_int
        >>> roman_to_int("MCMXCIV")
        1994

    Args:
        roman (str): Roman numeral string.

    Returns:
        Integer.
    """
    i_count = roman.count("I")
    v_count = roman.count("V")
    x_count = roman.count("X")
    l_count = roman.count("L")
    c_count = roman.count("C")
    d_count = roman.count("D")
    m_count = roman.count("M")

    iv_count = roman.count("IV")
    i_count -= iv_count
    v_count -= iv_count

    ix_count = roman.count("IX")
    i_count -= ix_count
    x_count -= ix_count

    xl_count = roman.count("XL")
    x_count -= xl_count
    l_count -= xl_count

    xc_count = roman.count("XC")
    x_count -= xc_count
    c_count -= xc_count

    cd_count = roman.count("CD")
    c_count -= cd_count
    d_count -= cd_count

    cm_count = roman.count("CM")
    c_count -= cm_count
    m_count -= cm_count

    total = 0
    total += 1 * i_count
    total += 5 * v_count
    total += 10 * x_count
    total += 50 * l_count
    total += 100 * c_count
    total += 500 * d_count
    total += 1000 * m_count

    total += 4 * iv_count
    total += 9 * ix_count
    total += 40 * xl_count
    total += 90 * xc_count
    total += 400 * cd_count
    total += 900 * cm_count

    return total

to_human_size(n_bytes)

Converts the number of bytes into a human-readable format.

Examples:

>>> from xloft import to_human_size
>>> to_human_size(200)
200 bytes
>>> to_human_size(1048576)
1 MB
>>> to_human_size(1048575)
1023.999 KB

Parameters:

Name Type Description Default
n_bytes int

The number of bytes.

required

Returns:

Type Description
str

Returns a humanized string: 200 bytes | 1 KB | 1.5 MB etc.

Source code in src/xloft/converters/human_size.py
def to_human_size(n_bytes: int) -> str:
    """Converts the number of bytes into a human-readable format.

    Examples:
        >>> from xloft import to_human_size
        >>> to_human_size(200)
        200 bytes
        >>> to_human_size(1048576)
        1 MB
        >>> to_human_size(1048575)
        1023.999 KB

    Args:
        n_bytes: The number of bytes.

    Returns:
        Returns a humanized string: 200 bytes | 1 KB | 1.5 MB etc.
    """
    idx: int = math.floor(math.log(n_bytes) / math.log(1024))
    ndigits: int = [0, 3, 6, 9, 12][idx]
    human_size: int | float = n_bytes if n_bytes < 1024 else abs(round(n_bytes / pow(1024, idx), ndigits))
    order = ["bytes", "KB", "MB", "GB", "TB"][idx]
    if math.modf(human_size)[0] == 0.0:
        human_size = int(human_size)
    return f"{human_size} {order}"