-2

For example, I need *missing_vars to be an f-string in this logging. Because for logging.critical() one msg argument is needed, so I can't do this:

logging.critical(f'Required environment variables are missing: {*missing_vars}')

I can do it without *args, but I need it for a beautiful output, because it turns out like this:

>>> logging.critical(f'Required environment variables are missing: {missing_vars}')
Required environment variables are missing: ['PRACTICUM_TOKEN', 'TELEGRAM_TOKEN']`

But, I want like this:

Required environment variables are missing: PRACTICUM_TOKEN, TELEGRAM_TOKEN

My function:

def check_tokens():
    """Checks the availability of environment variables."""
    ENV_VARS = {
        'PRACTICUM_TOKEN': PRACTICUM_TOKEN,
        'TELEGRAM_TOKEN': TELEGRAM_TOKEN,
        'TELEGRAM_CHAT_ID': TELEGRAM_CHAT_ID,
    }
    missing_vars = [var for var, value in ENV_VARS.items() if not value]
    if missing_vars:
        logging.critical(f'Required environment variables are missing: {*missing_vars}') # <--
    else:
        return True
DeepSpace
  • 78,697
  • 11
  • 109
  • 154
finegorko
  • 57
  • 8
  • 4
    `f'{", ".join(map(repr, missing_vars))}'` – Mechanic Pig Aug 21 '22 at 10:11
  • 2
    `{*missing_vars}` doesn't work because a starred expression is not allowed in this context. `logging.critical` would have still got a single string argument – DeepSpace Aug 21 '22 at 10:14
  • then how do I get the beautiful output that I gave in the example? – finegorko Aug 21 '22 at 10:16
  • Using the code provided in the first context. You map each item to its representation then join each value together separated by a comma and a space – Miguel Guthridge Aug 21 '22 at 10:18
  • Change `repr` to `str` or use `", ".join(missing_vars)` directly if all elements of `missing_vars` are strings. – Mechanic Pig Aug 21 '22 at 10:25
  • thank you! output now is `CRITICAL:root:Required environment variables are missing: PRACTICUM_TOKEN, TELEGRAM_TOKEN, TELEGRAM_CHAT_ID` Now I'm wondering if my code can be shortened? Does it happen in practice that I declare message for `missing_vars` separately `from logging.critical()`. [check_tokens()](https://i.imgur.com/7NWDokT.png) – finegorko Aug 21 '22 at 10:28

1 Answers1

0

According to the comments of Mechanic Pig, I have updated my function and the output I need now, thank you!

def check_tokens():
    """Checks the availability of environment variables."""
    ENV_VARS = {
        'PRACTICUM_TOKEN': PRACTICUM_TOKEN,
        'TELEGRAM_TOKEN': TELEGRAM_TOKEN,
        'TELEGRAM_CHAT_ID': TELEGRAM_CHAT_ID,
    }
    missing_vars = [var for var, value in ENV_VARS.items() if not value]
    if missing_vars:
        logging.critical(
            f'Required environment variables are missing: '
            f'{", ".join(missing_vars)}'
        )
    else:
        return True

Output:

CRITICAL:root:Required environment variables are missing: PRACTICUM_TOKEN, TELEGRAM_TOKEN, TELEGRAM_CHAT_ID
finegorko
  • 57
  • 8