Skip to content

local_object_size

Storage capacity measurement for locally-stored objects.

LocalObjectSize

Bases: Measurement

Measure the size of a locally-stored object. Calculates the results by default in bytes.

Source code in mlte/measurement/storage/local_object_size.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class LocalObjectSize(Measurement):
    """Measure the size of a locally-stored object. Calculates the results by default in bytes."""

    def __init__(self, identifier: Optional[str] = None):
        """
        Initialize a new LocalObjectSize measurement.

        :param identifier: A unique identifier for the measurement
        """
        super().__init__(identifier)

    def __call__(self, path: str, unit: Unit = Units.byte) -> Real:
        """
        Compute the size of the object at `path`.

        :param path: The path to the object
        :param unit: The unit to return the size in, defaults to byte (Units.byte).

        :return: The size of the object, in bytes
        """
        if not os.path.isfile(path) and not os.path.isdir(path):
            raise RuntimeError(f"Invalid path: {path}")

        # If the object is just a file, just get its size.
        if os.path.isfile(path):
            total_size = os.path.getsize(path)
        else:
            # Otherwise, the object must be directory, get accumulated size.
            assert os.path.isdir(
                path
            ), f"Path {path} is not a file nor a folder."
            total_size = 0
            for dirpath, _, filenames in os.walk(path):
                for name in filenames:
                    path = os.path.join(dirpath, name)
                    if not os.path.islink(path):
                        total_size += os.path.getsize(path)

        # Since getsize measures in bytes, first set up quantity in that unit.
        size_w_unit = total_size * Units.byte

        # Convert to requested unit.
        converted_size = size_w_unit.to(unit)
        return Real(float(converted_size.magnitude), unit=unit)

    # Overriden.
    @classmethod
    def get_output_type(cls) -> type[Real]:
        return Real

__call__(path, unit=Units.byte)

Compute the size of the object at path.

Parameters:

Name Type Description Default
path str

The path to the object

required
unit Unit

The unit to return the size in, defaults to byte (Units.byte).

byte

Returns:

Type Description
Real

The size of the object, in bytes

Source code in mlte/measurement/storage/local_object_size.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def __call__(self, path: str, unit: Unit = Units.byte) -> Real:
    """
    Compute the size of the object at `path`.

    :param path: The path to the object
    :param unit: The unit to return the size in, defaults to byte (Units.byte).

    :return: The size of the object, in bytes
    """
    if not os.path.isfile(path) and not os.path.isdir(path):
        raise RuntimeError(f"Invalid path: {path}")

    # If the object is just a file, just get its size.
    if os.path.isfile(path):
        total_size = os.path.getsize(path)
    else:
        # Otherwise, the object must be directory, get accumulated size.
        assert os.path.isdir(
            path
        ), f"Path {path} is not a file nor a folder."
        total_size = 0
        for dirpath, _, filenames in os.walk(path):
            for name in filenames:
                path = os.path.join(dirpath, name)
                if not os.path.islink(path):
                    total_size += os.path.getsize(path)

    # Since getsize measures in bytes, first set up quantity in that unit.
    size_w_unit = total_size * Units.byte

    # Convert to requested unit.
    converted_size = size_w_unit.to(unit)
    return Real(float(converted_size.magnitude), unit=unit)

__init__(identifier=None)

Initialize a new LocalObjectSize measurement.

Parameters:

Name Type Description Default
identifier Optional[str]

A unique identifier for the measurement

None
Source code in mlte/measurement/storage/local_object_size.py
14
15
16
17
18
19
20
def __init__(self, identifier: Optional[str] = None):
    """
    Initialize a new LocalObjectSize measurement.

    :param identifier: A unique identifier for the measurement
    """
    super().__init__(identifier)