diff options
author | Théo de la Hogue | 2024-01-24 10:38:19 +0100 |
---|---|---|
committer | Théo de la Hogue | 2024-01-24 10:38:19 +0100 |
commit | 6fec7b7a2a9f8b8a0e624abdc0fdba84294f4178 (patch) | |
tree | b5f0b9103347bab3a529df06178c0f94e8845f45 /src/argaze/utils/UtilsFeatures.py | |
parent | 75e2e040edee78139ea5e60e9dcf8962fa54cb7c (diff) | |
download | argaze-6fec7b7a2a9f8b8a0e624abdc0fdba84294f4178.zip argaze-6fec7b7a2a9f8b8a0e624abdc0fdba84294f4178.tar.gz argaze-6fec7b7a2a9f8b8a0e624abdc0fdba84294f4178.tar.bz2 argaze-6fec7b7a2a9f8b8a0e624abdc0fdba84294f4178.tar.xz |
Defining FileWriter as utils class. Removing DataLog folder. Adding flags to ArFrame an And ArLyers to know when analysis are available. Look methods are not iterator anymore.
Diffstat (limited to 'src/argaze/utils/UtilsFeatures.py')
-rw-r--r-- | src/argaze/utils/UtilsFeatures.py | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/argaze/utils/UtilsFeatures.py b/src/argaze/utils/UtilsFeatures.py index 7971167..0cb6d77 100644 --- a/src/argaze/utils/UtilsFeatures.py +++ b/src/argaze/utils/UtilsFeatures.py @@ -146,3 +146,61 @@ class TimeProbe(): self.start() +def tuple_to_string(t: tuple, separator: str = ", ") -> str: + """Convert tuple elements into quoted strings separated by a separator string.""" + + return separator.join(f'\"{e}\"' for e in t) + +class FileWriter(): + """Write into a file line by line. + + Parameters: + path: File path where to write data. + header: String or tuple to write first. + separator: String used to separate elements during tuple to string conversion. + """ + + def __init__(self, path: str, header: str|tuple, separator: str = ", "): + """Check that folder structure exist and create file then, write header line.""" + + import os + import pathlib + + self.path = pathlib.Path(path) + self.separator = separator + + if not os.path.exists(self.path.parent.absolute()): + os.makedirs(self.path.parent.absolute()) + + # Open file + self._file = open(self.path, 'w', encoding='utf-8', buffering=1) + + # Write header if required + if header is not None: + + # Format list or tuple element into quoted strings + if not isinstance(header, str): + + header = tuple_to_string(header, self.separator) + + print(header, file=self._file, flush=True) + + def __del__(self): + """Close file.""" + + self._file.close() + + def write(self, log: str|tuple): + """Write log as a new line into file. + + !!! note + Tuple elements are converted into quoted strings separated by separator string. + """ + + # Format list or tuple element into quoted strings + if not isinstance(log, str): + + log = tuple_to_string(log, self.separator) + + # Write into file + print(log, file=self._file, flush=True)
\ No newline at end of file |