problem
For now, multiple output-related things exist in VIP:
verbose parameter to functions
debug parameter to functions
- timing information
- progress bars.
solution
I propose a new class, Logger, which takes care of these.
Internally, a Logger is defined by a numeric logging level (roughly following the logging levels of the standard library's logging module):
- ERROR = 40
- WARNING = 30
- INFO = 20
- DEBUG = 10
and one or multiple boolean tags:
progressbar = True
timing = True.
example
The new Logger class can be used as follows:
from xyz import Logger
@Logger.timing()
def andromeda(datacube=None, verbose=False):
logger = Logger("andromeda", verbose)
if datacube is None:
logger.warn("The datacube is not set, that is maybe not what you want!")
for i in Progressbar(range(1000), verbose=logger):
logger.debug("loop #{}...".format(i))
# ...
logger.info("done.")
I can then call andromeda like so:
andromeda(verbose=True) / andromeda(verbose="info")
- These set
level=20, progressbar=True, timing=True.
- It will show the timing information, the progressbar, and the output of
info and warn with the string "andromeda: " prefixed.
- if I want to disable the timing information, e.g. when I run
andromeda inside a loop, I can add -timing to the verbose parameter: andromeda(verbose="info-timing") .
- →
level=20, progressbar=True, timing=False
- if I want to disable the output, I use
andromeda(verbose=False) or andromeda(verbose='error').
- It sets internally
level=40, progressbar=False, timing=False.
- if I want just the progressbar, I can enable it separately using
+progressbar: andromeda(verbose='error+progressbar')
- →
level=40, progressbar=True, timing=False
notes
The Progressbar class would check if the progressbar=True is set inside the Logger object it gets. The @Logger.timing() decorator does the same for the timing tag.
The Logger objects are entirely backwards-compatible to functions which expect a boolean as their verbose parameter, as they behave like a boolean when using e.g. if logger:. So there is no need to change anything existing.
I have the Logger class already coded, except for the @Logger.timing. I'll add that shortly, so we can try it out.
What do you think?
problem
For now, multiple output-related things exist in VIP:
verboseparameter to functionsdebugparameter to functionssolution
I propose a new class,
Logger, which takes care of these.Internally, a
Loggeris defined by a numeric logging level (roughly following the logging levels of the standard library'sloggingmodule):and one or multiple boolean tags:
progressbar = Truetiming = True.example
The new
Loggerclass can be used as follows:I can then call
andromedalike so:andromeda(verbose=True)/andromeda(verbose="info")level=20,progressbar=True,timing=True.infoandwarnwith the string"andromeda: "prefixed.andromedainside a loop, I can add-timingto theverboseparameter:andromeda(verbose="info-timing").level=20,progressbar=True,timing=Falseandromeda(verbose=False)orandromeda(verbose='error').level=40,progressbar=False,timing=False.+progressbar:andromeda(verbose='error+progressbar')level=40,progressbar=True,timing=Falsenotes
The
Progressbarclass would check if theprogressbar=Trueis set inside theLoggerobject it gets. The@Logger.timing()decorator does the same for thetimingtag.The
Loggerobjects are entirely backwards-compatible to functions which expect a boolean as theirverboseparameter, as they behave like a boolean when using e.g.if logger:. So there is no need to change anything existing.I have the
Loggerclass already coded, except for the@Logger.timing. I'll add that shortly, so we can try it out.What do you think?