Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved verbosity handling #226

Open
r4lv opened this issue Jun 15, 2018 · 0 comments
Open

Improved verbosity handling #226

r4lv opened this issue Jun 15, 2018 · 0 comments

Comments

@r4lv
Copy link
Contributor

r4lv commented Jun 15, 2018

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant