question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] Disable multiple help across files

See original GitHub issue

1. Briefly

I don’t understand, how I can disable multiple help, if my program contains multiple files.

2. Configuration

For example, I have a folder with 4 files:

  • config.py (see #33):
import logbook
import sys

log = logbook.Logger("Sasha Logbook")


def clize_log_level(*, logbook_level: 'll'="NOTICE"):
    """Change log levels via command line.

    User select, which logging messages to see. See about 6 log levels here:
    https://logbook.readthedocs.io/en/stable/quickstart.html

    :param logbook_level: user select logging level
    """
    if logbook_level == "DEBUG":
        logbook.StreamHandler(sys.stdout,
                              level=logbook.DEBUG).push_application()
    elif logbook_level == "ERROR":
        logbook.StreamHandler(sys.stdout,
                              level=logbook.ERROR).push_application()
    else:
        logbook.StreamHandler(sys.stdout,
                              level=logbook.NOTICE).push_application()

  • first_test.py:
from clize import run
from config import clize_log_level
from config import log

run(clize_log_level, exit=False)

log.debug("First test + debug message")
log.notice("First test + notice message")
log.error("First test + error message")


first_test_variable = True

  • second_test.py:
from clize import run
from config import clize_log_level
from config import log

run(clize_log_level, exit=False)

log.debug("Second test + debug message")
log.notice("Second test + notice message")
log.error("Second test + error message")


second_test_variable = True

  • run_tests.py:
from clize import run
from config import clize_log_level
from config import log
from first_test import first_test_variable
from second_test import second_test_variable

if first_test_variable and second_test_variable is True:
    log.debug("Run tests + debug message")
    log.notice("Run tests + notice message")
    log.error("Run tests + error message")

3. Behavior of the program

If I run run_tests.py, run first_test.py and second_test.py. Logging messages from first_test.py and second_test.py return.

The program works as expected:

D:\SashaClize>python run_tests.py --ll=DEBUG
[2018-01-06 15:21:38.251565] DEBUG: Sasha Logbook: First test + debug message
[2018-01-06 15:21:38.251565] NOTICE: Sasha Logbook: First test + notice message
[2018-01-06 15:21:38.251565] ERROR: Sasha Logbook: First test + error message
[2018-01-06 15:21:38.252565] DEBUG: Sasha Logbook: Second test + debug message
[2018-01-06 15:21:38.253197] NOTICE: Sasha Logbook: Second test + notice message
[2018-01-06 15:21:38.253332] ERROR: Sasha Logbook: Second test + error message
[2018-01-06 15:21:38.253457] DEBUG: Sasha Logbook: Run tests + debug message
[2018-01-06 15:21:38.253556] NOTICE: Sasha Logbook: Run tests + notice message
[2018-01-06 15:21:38.253556] ERROR: Sasha Logbook: Run tests + error message
D:\SashaClize>python run_tests.py --ll=ERROR
[2018-01-06 15:22:01.131626] ERROR: Sasha Logbook: First test + error message
[2018-01-06 15:22:01.132525] ERROR: Sasha Logbook: Second test + error message
[2018-01-06 15:22:01.133558] ERROR: Sasha Logbook: Run tests + error message

4. Problem

One problem: help menu show for me 2 times:

D:\SashaClize>python run_tests.py --help
Usage: run_tests.py [OPTIONS]

Change log levels via command line.

User select, which logging messages to see. See about 6 log levels here: https://logbook.readthedocs.io/en/stable/quickstart.html

Options:
  --logbook-level, --ll=STR   user select logging level (default: NOTICE)

Other actions:
  -h, --help                  Show the help
Usage: run_tests.py [OPTIONS]

Change log levels via command line.

User select, which logging messages to see. See about 6 log levels here: https://logbook.readthedocs.io/en/stable/quickstart.html

Options:
  --logbook-level, --ll=STR   user select logging level (default: NOTICE)

Other actions:
  -h, --help                  Show the help

If I have more <number>_test files, I have more help repeats.

How I need edit in my code, that don’t see help multiple times?

Thanks.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
epsycommented, Jan 20, 2018

Oh no. don’t worry, I just had a busy week.


It looks like this would work correctly, good job on restructuring things into functions. Is there still an issue?

1reaction
epsycommented, Jan 6, 2018

You’re calling run(clize_log_level, ...) once in each module, therefore arguments get interpreted twice. You can’t notice it normally because your function does not product output of its own, but when you use --help, then the help is printed twice, because arguments were printed twice.

What you can do is move that call to run(clize_log_level., ...) into config.py. That way, because Python runs a module’s code only once, your log-level setter will only run once.


Note that this is a highly unusual way to structure code for Python. Normally you would call clize.run(...) only in the module first executed by Python. You can do that by putting it into an if __name__ == '__main__' block like in the tutorial.

Your test logging code would go in a function of its own that you’d call after clize, e.g.:

def first_test():
    log.debug("First test + debug message")
    log.notice("First test + notice message")
    log.error("First test + error message")

if __name__ == '__main__':
    run(clize_log_level, exit=False)
    first_test()

You would do the same in run_tests.py, by importing the functions you created and running all three


In the future, you should look to avoid using exit=False: if there is an error in processing the parameters, the program would continue anyway.

This means making a way to have both the log-level setter and the “business” function be one function together. Fortunately you can pass functions as parameters in Python, e.g. set_log_level_then_call(first_test, loglevel='DEBUG'). The log level You just have to hide this from clize so it can just worry about the loglevel parameter:

def set_loglevel_then_call_first(*args, **kwargs):
    set_log_level_then_call(first_test, *args, **kwargs)

Or alternatively: set_loglevel_then_call_first = functools.partial(set_log_level_then_call, first_test), which does the same.

Eventually you’ll want to reverse the roles and make set_loglevel a decorator.

But for now I think you can ignore this last section 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Completely disable file grouping, always, everywhere, in all ...
This is seriously a problem for me. My assets are organized alphabetically for a reason. When I have to right-click and choose "Group...
Read more >
Disable eslint for all files in directory and subdirectories
When it comes to ignoring multiple files at the same time, we can use ** . For example: src/** means ignore all files...
Read more >
Anti Ballot Box Stuffing - disable multiple responses
To enable the Anti Ballot Box Stuffing, go to: Login » My Surveys (Select Survey) » Edit » Settings » Multiple Responding. Survey...
Read more >
Disabling and enabling inspections - WebStorm - JetBrains
When you disable an inspection, you turn it off. It means that the code analysis engine stops searching project files for the problem...
Read more >
Shared Links Frequently Asked Questions - Box Support
What happens if I disable a shared link? For security purposes, disabled shared links are deleted and cannot be added back to a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found