Skip to content

report

resource_tracker.report #

Classes:

Name Description
Report

A string subclass representing an HTML report with methods to view and save it.

Functions:

Name Description
round_memory

Round a number to the nearest meaningful memory amount.

Report #

Bases: str

A string subclass representing an HTML report with methods to view and save it.

Methods:

Name Description
browse

Open the report in the default web browser.

save

Save the report to a file.

Source code in resource_tracker/report.py
class Report(str):
    """A string subclass representing an HTML report with methods to view and save it."""

    def browse(self):
        """Open the report in the default web browser.

        Creates a temporary HTML file and opens it in the default web browser.

        Returns:
            self: Returns the Report object for method chaining
        """
        with tempfile.NamedTemporaryFile(suffix=".html", delete=False) as f:
            f.write(self.encode("utf-8"))
            temp_path = f.name

        webbrowser.open("file://" + temp_path)
        return self

    def save(self, filepath):
        """Save the report to a file.

        Args:
            filepath: The path where to save the HTML report

        Returns:
            self: Returns the Report object for method chaining
        """
        with open(filepath, "w", encoding="utf-8") as f:
            f.write(self)
        return self

browse #

browse()

Open the report in the default web browser.

Creates a temporary HTML file and opens it in the default web browser.

Returns:

Name Type Description
self

Returns the Report object for method chaining

Source code in resource_tracker/report.py
def browse(self):
    """Open the report in the default web browser.

    Creates a temporary HTML file and opens it in the default web browser.

    Returns:
        self: Returns the Report object for method chaining
    """
    with tempfile.NamedTemporaryFile(suffix=".html", delete=False) as f:
        f.write(self.encode("utf-8"))
        temp_path = f.name

    webbrowser.open("file://" + temp_path)
    return self

save #

save(filepath)

Save the report to a file.

Parameters:

Name Type Description Default
filepath

The path where to save the HTML report

required

Returns:

Name Type Description
self

Returns the Report object for method chaining

Source code in resource_tracker/report.py
def save(self, filepath):
    """Save the report to a file.

    Args:
        filepath: The path where to save the HTML report

    Returns:
        self: Returns the Report object for method chaining
    """
    with open(filepath, "w", encoding="utf-8") as f:
        f.write(self)
    return self

round_memory #

round_memory(mb)

Round a number to the nearest meaningful memory amount.

Parameters:

Name Type Description Default
mb Union[int, float]

The value in MB to round.

required

Returns:

Type Description
int

The rounded value in MB as an integer.

Example:

>>> round_memory(68)
128
>>> round_memory(896)
1024
>>> round_memory(3863)
4096
Source code in resource_tracker/report.py
def round_memory(mb: Union[int, float]) -> int:
    """Round a number to the nearest meaningful memory amount.

    Args:
        mb: The value in MB to round.

    Returns:
        The rounded value in MB as an integer.

    Example:

        >>> round_memory(68)
        128
        >>> round_memory(896)
        1024
        >>> round_memory(3863)
        4096
    """
    if mb <= 128:
        rounded = 128
    elif mb <= 256:
        rounded = 256
    elif mb <= 512:
        rounded = 512
    elif mb <= 1024:
        rounded = 1024
    elif mb <= 2048:
        rounded = 2048
    else:
        # round up to the next GB
        rounded_gb = mb / 1024
        rounded = int(1024 * (rounded_gb // 1 + (1 if rounded_gb % 1 > 0 else 0)))
    return rounded