Skip to content

GenericFile

GenericFile

A class containing basic metadata about a file.

Parameters:

Name Type Description Default
file_path str | Path

The path to the file.

required
experiment_type str

The type of experiment the file is associated with, by default "".

''

Attributes:

Name Type Description
local_path Path

The path to the file.

length int

The length of the file in bytes.

sha512 str

The SHA512 hash of the file.

date_created datetime

The date and time the file was created.

experiment_type str

The type of experiment the file is associated with.

Source code in magnetopy\data_files.py
class GenericFile:
    """A class containing basic metadata about a file.

    Parameters
    ----------
    file_path : str | Path
        The path to the file.
    experiment_type : str, optional
        The type of experiment the file is associated with, by default "".

    Attributes
    ----------
    local_path : Path
        The path to the file.
    length : int
        The length of the file in bytes.
    sha512 : str
        The SHA512 hash of the file.
    date_created : datetime
        The date and time the file was created.
    experiment_type : str
        The type of experiment the file is associated with.
    """

    def __init__(self, file_path: str | Path, experiment_type: str = "") -> None:
        self.local_path = Path(file_path)
        self.length = self.local_path.stat().st_size
        self.date_created = datetime.fromtimestamp(self.local_path.stat().st_ctime)
        self.sha512 = self._determine_sha512()
        self.experiment_type = experiment_type

    def __str__(self) -> str:
        return f"GenericFile({self.local_path.name})"

    def __repr__(self) -> str:
        return f"GenericFile({self.local_path.name})"

    def _determine_sha512(self) -> str:
        buf_size = 4 * 1024 * 1024  # 4MB chunks
        hasher = hashlib.sha512()
        with self.local_path.open("rb") as f:
            while data := f.read(buf_size):
                hasher.update(data)
        return hasher.hexdigest()

    def as_dict(self) -> dict[str, Any]:
        """Serializes the GenericFile object to a dictionary.

        Returns
        -------
        dict[str, Any]
            Contains the following keys: local_path, length, date_created, sha512
        """
        return {
            "_class_": self.__class__.__name__,
            "experiment_type": self.experiment_type,
            "local_path": str(self.local_path),
            "length": self.length,
            "date_created": self.date_created.isoformat(),
            "sha512": self.sha512,
        }

as_dict()

Serializes the GenericFile object to a dictionary.

Returns:

Type Description
dict[str, Any]

Contains the following keys: local_path, length, date_created, sha512

Source code in magnetopy\data_files.py
def as_dict(self) -> dict[str, Any]:
    """Serializes the GenericFile object to a dictionary.

    Returns
    -------
    dict[str, Any]
        Contains the following keys: local_path, length, date_created, sha512
    """
    return {
        "_class_": self.__class__.__name__,
        "experiment_type": self.experiment_type,
        "local_path": str(self.local_path),
        "length": self.length,
        "date_created": self.date_created.isoformat(),
        "sha512": self.sha512,
    }