Skip to content

local_object_size

mlte/measurement/storage/local_object_size.py

Storage capacity measurement for locally-stored objects.

LocalObjectSize

Bases: Measurement

Measure the size of a locally-stored object.

Source code in mlte/measurement/storage/local_object_size.py
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
class LocalObjectSize(Measurement):
    """Measure the size of a locally-stored object."""

    def __init__(self, identifier: str):
        """
        Initialize a new LocalObjectSize measurement.

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

    def __call__(self, path: str) -> Integer:
        """
        Compute the size of the object at `path`.

        :param path: The path to the object

        :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, return it immediately
        if os.path.isfile(path):
            return Integer(self.metadata, os.path.getsize(path))

        # Otherwise, the object must be directory
        assert os.path.isdir(path), "Broken invariant."

        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)

        return Integer(self.metadata, total_size)

    @classmethod
    def value(self) -> Type[Integer]:
        """Returns the class type object for the Value produced by the Measurement."""
        return Integer

__call__(path)

Compute the size of the object at path.

Parameters:

Name Type Description Default
path str

The path to the object

required

Returns:

Type Description
Integer

The size of the object, in bytes

Source code in mlte/measurement/storage/local_object_size.py
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
def __call__(self, path: str) -> Integer:
    """
    Compute the size of the object at `path`.

    :param path: The path to the object

    :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, return it immediately
    if os.path.isfile(path):
        return Integer(self.metadata, os.path.getsize(path))

    # Otherwise, the object must be directory
    assert os.path.isdir(path), "Broken invariant."

    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)

    return Integer(self.metadata, total_size)

__init__(identifier)

Initialize a new LocalObjectSize measurement.

Parameters:

Name Type Description Default
identifier str

A unique identifier for the measurement

required
Source code in mlte/measurement/storage/local_object_size.py
17
18
19
20
21
22
23
def __init__(self, identifier: str):
    """
    Initialize a new LocalObjectSize measurement.

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

value() classmethod

Returns the class type object for the Value produced by the Measurement.

Source code in mlte/measurement/storage/local_object_size.py
52
53
54
55
@classmethod
def value(self) -> Type[Integer]:
    """Returns the class type object for the Value produced by the Measurement."""
    return Integer