github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/python/aistore/sdk/dataset/data_attribute.py (about)

     1  #
     2  # Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved.
     3  #
     4  
     5  from typing import Tuple
     6  from pathlib import Path
     7  from aistore.sdk.dataset.config_attribute import ConfigAttribute
     8  from aistore.sdk.utils import read_file_bytes
     9  
    10  
    11  # pylint: disable=too-few-public-methods
    12  class DataAttribute(ConfigAttribute):
    13      """
    14      Handles data attributes for datasets, managing the retrieval of attribute data from files
    15  
    16      Args:
    17          path (Path): The file path where the attribute data is stored
    18          name (str): The name of the attribute, used as a reference and identifier in the dataset
    19          file_type (str): The type of file that stores the attribute data (e.g., 'jpg', 'png', 'csv')
    20      """
    21  
    22      def __init__(self, path: Path, name: str, file_type: str):
    23          self.path = path
    24          self.name = name
    25          self.file_type = file_type
    26  
    27      def get_data_for_entry(self, filename: str) -> Tuple[str, any]:
    28          """
    29          Get the data for a given filename
    30  
    31          Args:
    32              filename (str): The name of the file to retrieve data for
    33  
    34          Returns:
    35              Tuple[str, any]: A tuple containing the sample key and the data for the given filename
    36          """
    37          key = self.name + "." + self.file_type
    38          try:
    39              data = read_file_bytes(self.path.joinpath(filename + "." + self.file_type))
    40          except FileNotFoundError as err:
    41              print(f"File not found: {err}")
    42              data = None
    43          return key, data