Skip to content

Commit cb8e367

Browse files
committed
Modernize code
1 parent 6b4a20c commit cb8e367

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

source-code/logging/log_it_all.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,30 @@
33
from argparse import ArgumentParser
44
import logging
55
import math
6-
import os
6+
from pathlib import Path
77
import sys
88

99

1010
def do_stuff(n):
1111
if n < 0:
12-
logging.error('can not do stuff for {0}'.format(n))
12+
logging.error(f'can not do stuff for {n}')
1313
return 1
1414
for i in range(n):
15-
logging.info('doing stuff {0}'.format(str(i)))
16-
print('doing {0}: {1:.4f}'.format(i, math.sqrt(i)))
17-
logging.info('done stuff {0}'.format(str(i)))
15+
logging.info(f'doing stuff {i}')
16+
print(f'doing {i}: {math.sqrt(i):.4f}')
17+
logging.info(f'done stuff {i}')
1818
return 0
1919

2020

2121
def main():
2222
arg_parser = ArgumentParser(description='example for logging facility')
23-
arg_parser.add_argument('-log', dest='log_file',
23+
arg_parser.add_argument('--log', dest='log_file',
2424
help='name of log file')
25-
arg_parser.add_argument('-info', action='store_true',
25+
arg_parser.add_argument('--info', action='store_true',
2626
help='set log level to info')
27-
arg_parser.add_argument('-new_log', action='store_true',
27+
arg_parser.add_argument('--new_log', action='store_true',
2828
help='overwrite existing log file')
29-
arg_parser.add_argument('-n', type=int, default=1,
29+
arg_parser.add_argument('--n', type=int, default=1,
3030
help='number of times to do stuff')
3131
options = arg_parser.parse_args()
3232
format_str = '%(asctime)s:%(levelname)s:%(message)s'
@@ -39,14 +39,15 @@ def main():
3939
else:
4040
filemode = 'a'
4141
if options.log_file:
42-
exists = os.path.exists(options.log_file)
42+
log_file = Path(options.log_file)
43+
exists = log_file.exists()
4344
logging.basicConfig(level=level, filename=options.log_file,
4445
filemode=filemode, format=format_str)
4546
else:
4647
exists = False
4748
logging.basicConfig(level=level, format=format_str)
4849
if exists:
49-
logging.warn('overwriting existing log file')
50+
logging.warning('overwriting existing log file')
5051
logging.info('application started')
5152
logging.info('logger initialized')
5253
status = do_stuff(options.n)

0 commit comments

Comments
 (0)