# SPDX-FileCopyrightText: Copyright (c) 2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: LicenseRef-NvidiaProprietary # # NVIDIA CORPORATION, its affiliates and licensors retain all intellectual # property and proprietary rights in and to this material, related # documentation and any modifications thereto. Any use, reproduction, # disclosure or distribution of this material and related documentation # without an express license agreement from NVIDIA CORPORATION or # its affiliates is strictly prohibited. """This package is responsible for log-related operations.""" from csv import DictReader from os.path import exists from ..common import str_to_val class DataLogger(): def __init__(self): self.supported_file_types = [("comma-separated values file", ".csv"), ("text file", ".txt"), ] def get_delimiter(self, file_path): assert(exists(file_path)) delimiter = None if file_path.lower().endswith(".csv"): delimiter = "," elif file_path.lower().endswith(".txt"): delimiter = " " else: assert(False) return delimiter def read_one_column(self, file_path, target_key): assert(file_path and exists(file_path)) result = [] with open(file_path, "r") as f: dict_reader = DictReader(f, delimiter=self.get_delimiter(file_path)) for lines in dict_reader: val = str_to_val(lines[target_key]) if val != None: # Do not use `if(val)` since 0 is valid value result.append(val) return result def dump_heading(self, data, file_path): # Creare a file and dump data.keys() as heading to the file with open(file_path, "w") as f: file_heading = [f"{key[0]}-{key[1]}" for key in data.keys()] f.write(f"{self.get_delimiter(file_path).join(file_heading)}\n") def append_data(self, data, file_path): assert(file_path and exists(file_path)) with open(file_path, "a") as f: file_content = [str(val) for val in data.values()] f.write(f"{self.get_delimiter(file_path).join(file_content)}\n") # Instantiate a singleton data_logger = DataLogger()