Put custom classes in classes/
CustomFormatter is now in external classe
This commit is contained in:
parent
2fdcd831b2
commit
42809eecd1
3 changed files with 26 additions and 24 deletions
24
classes/CustomFormater.py
Normal file
24
classes/CustomFormater.py
Normal file
|
@ -0,0 +1,24 @@
|
|||
import logging
|
||||
|
||||
class CustomFormatter(logging.Formatter):
|
||||
|
||||
grey = "\x1b[0;35m"
|
||||
blue = "\x1b[34;20m"
|
||||
yellow = "\x1b[33;20m"
|
||||
red = "\x1b[31;20m"
|
||||
bold_red = "\x1b[31;1m"
|
||||
reset = "\x1b[0m"
|
||||
format = "%(levelname)s: %(message)s (%(filename)s:%(lineno)d)"
|
||||
|
||||
FORMATS = {
|
||||
logging.DEBUG: blue + format + reset,
|
||||
logging.INFO: grey + format + reset,
|
||||
logging.WARNING: yellow + format + reset,
|
||||
logging.ERROR: red + format + reset,
|
||||
logging.CRITICAL: bold_red + format + reset
|
||||
}
|
||||
|
||||
def format(self, record):
|
||||
log_fmt = self.FORMATS.get(record.levelno)
|
||||
formatter = logging.Formatter(log_fmt)
|
||||
return formatter.format(record)
|
66
classes/Timer.py
Normal file
66
classes/Timer.py
Normal file
|
@ -0,0 +1,66 @@
|
|||
import time
|
||||
|
||||
|
||||
class TimerError(Exception):
|
||||
""" Manage timer errors """
|
||||
|
||||
class Timer:
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
text_out = '{}: Elapsed time: {:0.4f} seconds',
|
||||
text_in = 'Start {}',
|
||||
text_tag = 'Time elapsed for {}: {:0.4f} seconds',
|
||||
text_total = 'Total recorded time: {:0.4f} seconds',
|
||||
logger=print,
|
||||
):
|
||||
self._start_time = None
|
||||
self.name = None
|
||||
self.text_in = text_in
|
||||
self.text_out = text_out
|
||||
self.text_tag = text_tag
|
||||
self.text_total = text_total
|
||||
self.logger = logger
|
||||
self.timers = {}
|
||||
|
||||
def start(self, name='General'):
|
||||
"""Start a new timer"""
|
||||
|
||||
self.name = name
|
||||
# if timer does not exists, initiate it
|
||||
if not name in self.timers:
|
||||
self.timers[self.name] = 0
|
||||
|
||||
if self._start_time is not None:
|
||||
raise TimerError('Timer is running. Use .stop() to stop it')
|
||||
|
||||
self.logger(self.text_in.format(self.name))
|
||||
self._start_time = time.perf_counter()
|
||||
|
||||
def stop(self):
|
||||
"""Stop the timer, and report the elapsed time"""
|
||||
if self._start_time is None:
|
||||
raise TimerError('Timer is not running. Use .start() to start it')
|
||||
|
||||
elapsed_time = time.perf_counter() - self._start_time
|
||||
self._start_time = None
|
||||
|
||||
if self.logger:
|
||||
self.logger(self.text_out.format(self.name, elapsed_time))
|
||||
|
||||
if self.name:
|
||||
self.timers[self.name] += elapsed_time
|
||||
|
||||
def get_time_by_tag(self, tag):
|
||||
if tag in self.timers:
|
||||
self.logger(self.text_tag.format(tag,self.timers[tag]))
|
||||
|
||||
def get_total_time(self):
|
||||
total = 0
|
||||
for v in self.timers.values():
|
||||
total += v
|
||||
self.logger(self.text_total.format(total))
|
||||
|
||||
def get_times_by_tag(self):
|
||||
for k in self.timers.keys():
|
||||
self.get_time_by_tag(k)
|
Loading…
Add table
Add a link
Reference in a new issue