1
- import sys , os , re
1
+ import sys
2
+ import os
3
+ import re
2
4
from pathlib import Path
3
5
4
- import shlex
5
- import subprocess
6
6
import warnings
7
7
8
8
from datetime import datetime
9
9
import logging
10
10
import stat
11
11
12
- from hydrodiy import PYVERSION
13
-
14
- from io import StringIO ,BytesIO
15
-
16
- import requests
17
-
18
- import numpy as np
19
- import pandas as pd
20
-
21
12
22
13
def script_template (filename , comment ,
23
- type = "simple" ,
24
- author = None , \
25
- fout = None , fdata = None , fimg = None ):
14
+ type = "simple" ,
15
+ author = None ,
16
+ fout = None , fdata = None , fimg = None ):
26
17
""" Write a script template
27
18
28
19
Parameters
@@ -45,25 +36,23 @@ def script_template(filename, comment,
45
36
Images folder
46
37
Example
47
38
-----------
48
- >>> iutils.script_template("a_cool_script.py", "Testing", "plot", "Bob Marley")
49
-
39
+ >>> iutils.script_template("a_cool_script.py", "Testing",
40
+ "plot", "Bob Marley")
50
41
"""
51
-
52
- if not type in ["simple" , "plot" ]:
53
- raise ValueError ("Expected script type in [simple/plot], " + \
54
- f"got { type } ." )
42
+ if type not in ["simple" , "plot" ]:
43
+ errmess = f"Expected script type in [simple/plot], got { type } ."
44
+ raise ValueError (errmess )
55
45
56
46
# Open script template
57
- ftemplate = Path (__file__ ).resolve ().parent / \
58
- f"script_template_{ type } .py"
47
+ ftemplate = Path (__file__ ).resolve ().parent / f"script_template_{ type } .py"
59
48
with ftemplate .open ("r" ) as ft :
60
49
txt = ft .read ()
61
50
62
51
# Add comment header
63
52
if author is None :
64
53
try :
65
54
author = os .getlogin ()
66
- except :
55
+ except Exception :
67
56
author = "unknown"
68
57
69
58
meta = "## -- Script Meta Data --\n "
@@ -118,12 +107,12 @@ def script_template(filename, comment,
118
107
class StartedCompletedLogger ():
119
108
""" Add context to logging messages via the context attribute """
120
109
121
- def __init__ (self , logger , \
122
- separator_charac = "-" , \
123
- separator_length = 50 , \
124
- dictseparator_charac = "+" , \
125
- dictseparator_length = 30 , \
126
- tab_length = 4 ):
110
+ def __init__ (self , logger ,
111
+ separator_charac = "-" ,
112
+ separator_length = 50 ,
113
+ dictseparator_charac = "+" ,
114
+ dictseparator_length = 30 ,
115
+ tab_length = 4 ):
127
116
errmess = "Expected a logger object"
128
117
assert isinstance (logger , logging .Logger ), errmess
129
118
self ._logger = logger
@@ -140,7 +129,7 @@ def get_separator(self, nsep, sep):
140
129
return sep * nsep
141
130
142
131
def add_tab (self , msg , ntab ):
143
- if ntab == 0 :
132
+ if ntab == 0 :
144
133
return msg
145
134
146
135
tab_space = " " * self .tab_length * ntab
@@ -168,24 +157,26 @@ def handlers(self):
168
157
169
158
def started (self ):
170
159
self .info ("@@@ Process started @@@" )
171
- self .info (self .get_separator (self .separator_charac , \
172
- self .separator_length ))
160
+ self .info (self .get_separator (self .separator_charac ,
161
+ self .separator_length ))
173
162
self .info ("" )
174
163
175
164
def completed (self ):
176
165
self .info ("" )
177
- self .info (self .get_separator (self .separator_charac , \
178
- self .separator_length ))
166
+ self .info (self .get_separator (self .separator_charac ,
167
+ self .separator_length ))
179
168
self .info ("@@@ Process completed @@@" )
180
169
181
170
def log_dict (self , tolog , name = "" , level = "info" ):
182
- """ Add log entry for dictionnary (e.g. created from argparse using vars)"""
171
+ """ Add log entry for dictionnary
172
+ (e.g. created from argparse using vars)
173
+ """
183
174
assert level in ["info" , "warning" , "critical" , "error" ]
184
175
logfun = getattr (self , level )
185
- sep = self .get_separator (self .dictseparator_charac , \
186
- self .dictseparator_length )
176
+ sep = self .get_separator (self .dictseparator_charac ,
177
+ self .dictseparator_length )
187
178
logfun (sep )
188
- if name != "" :
179
+ if name != "" :
189
180
logfun (f"{ name } :" )
190
181
for k , v in tolog .items ():
191
182
msg = self .add_tab (f"{ k } = { v } " , 1 )
@@ -195,15 +186,14 @@ def log_dict(self, tolog, name="", level="info"):
195
186
logfun ("" )
196
187
197
188
198
-
199
189
class ContextualLogger (StartedCompletedLogger ):
200
190
""" Add context to logging messages via the context attribute """
201
191
202
- def __init__ (self , logger , \
203
- context_hasheader = False , \
204
- context_charac = "#" , \
205
- context_length = 3 , \
206
- * args , ** kwargs ):
192
+ def __init__ (self , logger ,
193
+ context_hasheader = False ,
194
+ context_charac = "#" ,
195
+ context_length = 3 ,
196
+ * args , ** kwargs ):
207
197
self ._context = ""
208
198
self .context_hasheader = context_hasheader
209
199
self .context_charac = context_charac
@@ -221,8 +211,8 @@ def context(self, value):
221
211
self .info ("" )
222
212
self ._context = str (value )
223
213
if self ._context != "" and self .context_hasheader :
224
- sep = self .get_separator (self .context_charac , \
225
- self .context_length )
214
+ sep = self .get_separator (self .context_charac ,
215
+ self .context_length )
226
216
mess = sep + " " + self ._context + " " + sep
227
217
self .info (mess )
228
218
@@ -232,7 +222,7 @@ def completed(self):
232
222
233
223
def get_message (self , msg , ntab ):
234
224
if self .context != "" :
235
- tab = " " * self .tab_length * ntab if ntab > 0 else ""
225
+ tab = " " * self .tab_length * ntab if ntab > 0 else ""
236
226
return "{{ {0} }} {1}{2}" .format (self .context , tab , msg )
237
227
return msg
238
228
@@ -253,15 +243,15 @@ def critical(self, msg, ntab=0, *args, **kwargs):
253
243
return self ._logger .critical (msg , * args , ** kwargs )
254
244
255
245
256
- def get_logger (name , level = "INFO" , \
257
- console = True , flog = None , \
258
- fmt = "%(asctime)s | %(levelname)s | %(message)s" , \
259
- overwrite = True ,
260
- excepthook = True ,
261
- no_duplicate_handler = True ,\
262
- contextual = False , \
263
- start_message = True , \
264
- date_fmt = "%y-%m-%d %H:%M" ):
246
+ def get_logger (name , level = "INFO" ,
247
+ console = True , flog = None ,
248
+ fmt = "%(asctime)s | %(levelname)s | %(message)s" ,
249
+ overwrite = True ,
250
+ excepthook = True ,
251
+ no_duplicate_handler = True ,
252
+ contextual = False ,
253
+ start_message = True ,
254
+ date_fmt = "%y-%m-%d %H:%M" ):
265
255
""" Get a logger object that can handle contextual info
266
256
267
257
Parameters
@@ -303,8 +293,8 @@ def get_logger(name, level="INFO", \
303
293
logger .handlers = []
304
294
305
295
# Set logging level
306
- if not level in ["DEBUG" , "INFO" , "WARNING" , "ERROR" , "CRITICAL" ]:
307
- raise ValueError ("{0 } not a valid level" . format ( level ) )
296
+ if level not in ["DEBUG" , "INFO" , "WARNING" , "ERROR" , "CRITICAL" ]:
297
+ raise ValueError (f" { level } not a valid level." )
308
298
309
299
logger .setLevel (getattr (logging , level ))
310
300
@@ -330,7 +320,7 @@ def get_logger(name, level="INFO", \
330
320
logger .addHandler (sh )
331
321
332
322
# log to file
333
- if not flog is None and not has_flog :
323
+ if flog is not None and not has_flog :
334
324
flog = Path (flog )
335
325
if overwrite :
336
326
try :
@@ -344,8 +334,8 @@ def get_logger(name, level="INFO", \
344
334
345
335
if excepthook :
346
336
def catcherr (exc_type , exc_value , exc_traceback ):
347
- logger .error ("Unexpected error" , exc_info = ( exc_type , \
348
- exc_value , exc_traceback ))
337
+ logger .error ("Unexpected error" ,
338
+ exc_info = ( exc_type , exc_value , exc_traceback ))
349
339
sys .__excepthook__ (exc_type , exc_value , exc_traceback )
350
340
351
341
sys .excepthook = catcherr
@@ -354,12 +344,12 @@ def catcherr(exc_type, exc_value, exc_traceback):
354
344
[h .close () for h in logger .handlers ]
355
345
356
346
# Create the extended logger
357
- elogger = ContextualLogger (logger ) if contextual else \
358
- StartedCompletedLogger (logger )
347
+ if contextual :
348
+ elogger = ContextualLogger (logger )
349
+ else :
350
+ elogger = StartedCompletedLogger (logger )
359
351
360
352
if start_message :
361
353
elogger .started ()
362
354
363
355
return elogger
364
-
365
-
0 commit comments