|
| 1 | +import os |
| 2 | +import sys |
| 3 | +from datetime import datetime |
| 4 | +import logging |
| 5 | + |
| 6 | +## CHECKS |
| 7 | +def assert_path(filepath: str): |
| 8 | + """ |
| 9 | + Checks that fpath is a string and that it exists |
| 10 | +
|
| 11 | + PARAMS |
| 12 | + ----- |
| 13 | + - filepath (str): the filepath or folderpath |
| 14 | +
|
| 15 | + OUTPUTS |
| 16 | + ----- |
| 17 | + - raises assertion error if filepath is not a string or doesn't exist |
| 18 | + """ |
| 19 | + |
| 20 | + assert isinstance(filepath, str), f"filepath must be a string: {filepath}" |
| 21 | + assert os.path.exists( |
| 22 | + os.path.abspath(filepath) |
| 23 | + ), f"filepath does not exist: {os.path.abspath(filepath)}" |
| 24 | + |
| 25 | + |
| 26 | +## LOGGING |
| 27 | +def get_basename(fname: None | str = None) -> str: |
| 28 | + """ |
| 29 | + - For a given filename, returns basename WITHOUT file extension |
| 30 | + - If no fname given (i.e., None) then return basename that the function is called in |
| 31 | +
|
| 32 | + PARAMS |
| 33 | + ----- |
| 34 | + - fname (None or str): the filename to get basename of, or None |
| 35 | +
|
| 36 | + OUTPUTS |
| 37 | + ----- |
| 38 | + - basename of given filepath or the current file the function is executed |
| 39 | +
|
| 40 | + EXAMPLES |
| 41 | + ----- |
| 42 | + 1) |
| 43 | + >>> get_basename() |
| 44 | + utils |
| 45 | +
|
| 46 | + 2) |
| 47 | + >>> get_basename('this/is-a-filepath.csv') |
| 48 | + is-a-filepath |
| 49 | + """ |
| 50 | + if fname is not None: |
| 51 | + # PRECONDITION |
| 52 | + assert_path(fname) |
| 53 | + # MAIN FUNCTIONS |
| 54 | + return os.path.splitext(os.path.basename(fname))[0] |
| 55 | + else: |
| 56 | + return os.path.splitext(os.path.basename(sys.argv[0]))[0] |
| 57 | + |
| 58 | + |
| 59 | +def get_time(incl_time: bool = True, incl_timezone: bool = True) -> str: |
| 60 | + """ |
| 61 | + Gets current date, time (optional) and timezone (optional) for file naming |
| 62 | +
|
| 63 | + PARAMETERS |
| 64 | + ----- |
| 65 | + - incl_time (bool): whether to include timestamp in the string |
| 66 | + - incl_timezone (bool): whether to include the timezone in the string |
| 67 | +
|
| 68 | + RETURNS |
| 69 | + ----- |
| 70 | + - fname (str): includes date, timestamp and/or timezone |
| 71 | + connected by '_' in one string e.g. yyyyMMdd_hhmm_timezone |
| 72 | +
|
| 73 | + EXAMPLES |
| 74 | + ----- |
| 75 | + 1) |
| 76 | + >>> get_time() |
| 77 | + '20231019_101758_CEST' |
| 78 | +
|
| 79 | + 2) |
| 80 | + >>> get_time(incl_time=False) |
| 81 | + '20231019_CEST' |
| 82 | +
|
| 83 | + """ |
| 84 | + |
| 85 | + # PRECONDITIONALS |
| 86 | + assert isinstance(incl_time, bool), "incl_time must be True or False" |
| 87 | + assert isinstance(incl_timezone, bool), "incl_timezone must be True or False" |
| 88 | + |
| 89 | + # MAIN FUNCTION |
| 90 | + # getting current time and timezone |
| 91 | + the_time = datetime.now() |
| 92 | + timezone = datetime.now().astimezone().tzname() |
| 93 | + # convert date parts to string |
| 94 | + y = str(the_time.year) |
| 95 | + M = str(the_time.month) |
| 96 | + d = str(the_time.day) |
| 97 | + h = str(the_time.hour) |
| 98 | + m = str(the_time.minute) |
| 99 | + s = str(the_time.second) |
| 100 | + # putting date parts into one string |
| 101 | + if incl_time and incl_timezone: |
| 102 | + fname = "_".join([y + M + d, h + m + s, timezone]) |
| 103 | + elif incl_time: |
| 104 | + fname = "_".join([y + M + d, h + m + s]) |
| 105 | + elif incl_timezone: |
| 106 | + fname = "_".join([y + M + d, timezone]) |
| 107 | + else: |
| 108 | + fname = y + M + d |
| 109 | + |
| 110 | + # POSTCONDITIONALS |
| 111 | + parts = fname.split("_") |
| 112 | + if incl_time and incl_timezone: |
| 113 | + assert len(parts) == 3, f"time and/or timezone inclusion issue: {fname}" |
| 114 | + elif incl_time or incl_timezone: |
| 115 | + assert len(parts) == 2, f"time/timezone inclusion issue: {fname}" |
| 116 | + else: |
| 117 | + assert len(parts) == 1, f"time/timezone inclusion issue: {fname}" |
| 118 | + |
| 119 | + return fname |
| 120 | + |
| 121 | + |
| 122 | +def generate_log_filename(folder: str = "logs", suffix: str = "") -> str: |
| 123 | + """ |
| 124 | + Creates log file name and path |
| 125 | +
|
| 126 | + PARAMETERS |
| 127 | + ----- |
| 128 | + folder (str): name of the folder to put the log file in |
| 129 | + suffix (str): anything else you want to add to the log file name |
| 130 | +
|
| 131 | + RETURNS |
| 132 | + ----- |
| 133 | + log_filepath (str): the file path to the log file |
| 134 | + """ |
| 135 | + # PRECONDITIONS |
| 136 | + assert_path(folder) |
| 137 | + |
| 138 | + # MAIN FUNCTION |
| 139 | + log_filename = get_time(incl_timezone=False) + "_" + suffix + ".log" |
| 140 | + log_filepath = os.path.join(folder, log_filename) |
| 141 | + |
| 142 | + return log_filepath |
| 143 | + |
| 144 | + |
| 145 | +def init_log(filename: str, display: bool = False, logger_id: str | None = None): |
| 146 | + """ |
| 147 | + - Custom python logger configuration (basicConfig()) |
| 148 | + with two handlers (for stdout and for file) |
| 149 | + - from: https://stackoverflow.com/a/44760039 |
| 150 | + - Keeps a log record file of the python application, with option to |
| 151 | + display in stdout |
| 152 | +
|
| 153 | + PARAMETERS |
| 154 | + ----- |
| 155 | + - filename (str): filepath to log record file |
| 156 | + - display (bool): whether to print the logs to whatever standard output |
| 157 | + - logger_id (str): an optional identifier for yourself, |
| 158 | + if None then defaults to 'root' |
| 159 | +
|
| 160 | + RETURNS |
| 161 | + ----- |
| 162 | + - logger object |
| 163 | +
|
| 164 | + EXAMPLE |
| 165 | + ----- |
| 166 | + >>> logger = init_log('logs/tmp.log', display=True) |
| 167 | + >>> logger.info('Loading things') |
| 168 | + [2023-10-20 10:38:03,074] root: INFO - Loading things |
| 169 | + """ |
| 170 | + # PRECONDITIONALS |
| 171 | + assert isinstance(filename, str), "filename must be a string" |
| 172 | + assert ( |
| 173 | + isinstance(logger_id, str) or logger_id is None |
| 174 | + ), "logger_id must be a string or None" |
| 175 | + |
| 176 | + # MAIN FUNCTION |
| 177 | + # init handlers |
| 178 | + file_handler = logging.FileHandler(filename=filename) |
| 179 | + stdout_handler = logging.StreamHandler(stream=sys.stdout) |
| 180 | + if display: |
| 181 | + handlers = [file_handler, stdout_handler] |
| 182 | + else: |
| 183 | + handlers = [file_handler] |
| 184 | + |
| 185 | + # logger configuration |
| 186 | + logging.basicConfig( |
| 187 | + # level=logging.DEBUG, |
| 188 | + format="[%(asctime)s] %(name)s: %(levelname)s - %(message)s", |
| 189 | + handlers=handlers, |
| 190 | + ) |
| 191 | + logging.getLogger("matplotlib.font_manager").disabled = True |
| 192 | + |
| 193 | + # instantiate the logger |
| 194 | + logger = logging.getLogger(logger_id) |
| 195 | + logger.setLevel(logging.DEBUG) |
| 196 | + |
| 197 | + return logger |
| 198 | + |
| 199 | + |
| 200 | +def get_logger(): |
| 201 | + """ |
| 202 | + Putting at all together to init the log file. |
| 203 | + """ |
| 204 | + # get log suffix, which will be the current script's base file name |
| 205 | + log_suffix = get_basename() |
| 206 | + # generate log file name |
| 207 | + log_file = generate_log_filename(suffix=log_suffix) |
| 208 | + # init logger |
| 209 | + logger = init_log(log_file, display=True) |
| 210 | + # log it |
| 211 | + logger.info(f"Path to log file: {log_file}") |
| 212 | + |
| 213 | + return logger |
0 commit comments