22import sys
33from datetime import datetime
44import logging
5+ from enum import StrEnum
6+ from typing import Type
57
68## CHECKS
79def assert_path (filepath : str ):
@@ -17,12 +19,42 @@ def assert_path(filepath: str):
1719 - raises assertion error if filepath is not a string or doesn't exist
1820 """
1921
20- assert isinstance (filepath , str ), f"filepath must be a string: { filepath } "
22+ assert isinstance (filepath , str ), f"Filepath must be a string: { filepath } "
2123 assert os .path .exists (
2224 os .path .abspath (filepath )
23- ), f"filepath does not exist: { os .path .abspath (filepath )} "
25+ ), f"Filepath does not exist: { os .path .abspath (filepath )} "
2426
2527
28+ def assert_enum_value (enum_class : Type [StrEnum ], value : str , logger : logging .Logger ) -> StrEnum :
29+ """
30+ Validate that the given value is a valid member of the specified enumeration class.
31+
32+ Parameters
33+ ----------
34+ enum_class : Type[StrEnum]
35+ The enumeration class to validate against.
36+ value : str
37+ The value to be validated.
38+ logger : logging.Logger
39+ A logger object to track warnings, errors, and info messages.
40+
41+ Returns
42+ -------
43+ StrEnum
44+ The corresponding member of the enumeration if valid.
45+
46+ Raises
47+ ------
48+ ValueError
49+ If the value is not a valid member of the enumeration class.
50+ """
51+ try :
52+ return enum_class [value .upper ()]
53+ except KeyError :
54+ expected_values = ", " .join ([str (e .value ) for e in enum_class ])
55+ logger .error (f"Invalid value for { enum_class .__name__ } : '{ value } '. Expected values are: { expected_values } " )
56+ raise ValueError (f"Invalid { enum_class .__name__ } : { value } . Expected values are: { expected_values } " )
57+
2658## LOGGING
2759def get_basename (fname : None | str = None ) -> str :
2860 """
@@ -168,7 +200,7 @@ def init_log(filename: str, display: bool = False, logger_id: str | None = None)
168200 [2023-10-20 10:38:03,074] root: INFO - Loading things
169201 """
170202 # PRECONDITIONALS
171- assert isinstance (filename , str ), "filename must be a string"
203+ assert isinstance (filename , str ), "Filename must be a string"
172204 assert (
173205 isinstance (logger_id , str ) or logger_id is None
174206 ), "logger_id must be a string or None"
@@ -197,17 +229,27 @@ def init_log(filename: str, display: bool = False, logger_id: str | None = None)
197229 return logger
198230
199231
200- def get_logger ():
232+ def get_logger (log_suffix ):
201233 """
202- Putting at all together to init the log file.
234+ Initialize the logger with a log file name that includes an optional suffix.
235+
236+ Parameters
237+ ----------
238+ log_suffix : str
239+ A string to append to the log file name.
240+
241+ Returns
242+ -------
243+ logging.Logger
244+ An initialized logger instance.
203245 """
204- # get log suffix, which will be the current script's base file name
205- log_suffix = get_basename ()
206- # generate log file name
246+ # Generate log file name
207247 log_file = generate_log_filename (suffix = log_suffix )
208- # init logger
248+
249+ # Initialize logger
209250 logger = init_log (log_file , display = True )
210- # log it
251+
252+ # Log the path to the log file
211253 logger .info (f"Path to log file: { log_file } " )
212254
213255 return logger
0 commit comments