Home > other >  How to check the availability of environment variables correctly?
How to check the availability of environment variables correctly?

Time:08-21

I did a token check, if at least one token is missing, 'True' will not be. Now I need to deduce which variable is missing, how to do it?

PRACTICUM_TOKEN = os.getenv('PRACTICUM_TOKEN')
TELEGRAM_TOKEN = os.getenv('TELEGRAM_TOKEN')
TELEGRAM_CHAT_ID = os.getenv('TELEGRAM_CHAT_ID')


def check_tokens():
    """Checks the availability of environment variables."""
    ENV_VARS = [PRACTICUM_TOKEN, TELEGRAM_TOKEN, TELEGRAM_CHAT_ID]
    if not all(ENV_VARS):
        print('Required environment variables are missing:', ...)
    else:
        return True

CodePudding user response:

I might suggest putting these values inside a class. The check tokens method can be part of the class, and you can use __dict__ to dynamically get reference to all of the tokens you defined without having to duplicate code.

class Environment:
    def __init__(self):
        self.PRACTICUM_TOKEN = os.getenv('PRACTICUM_TOKEN')
        self.TELEGRAM_TOKEN = os.getenv('TELEGRAM_TOKEN')
        self.TELEGRAM_CHAT_ID = os.getenv('TELEGRAM_CHAT_ID')

    def check_tokens(self):
        """Checks the availability of environment variables."""
        missing_vars = [var for var, value in self.__dict__.items() if not value]
        if missing_vars:
            print('Required environment variables are missing:', *missing_vars)
            return False
        else:
            return True

print(Environment().check_tokens())

CodePudding user response:

A simpler version than the one by flakes:

ENV_VARS = ['PRACTICUM_TOKEN', 'TELEGRAM_TOKEN', 'TELEGRAM_CHAT_ID']


def check_tokens():
    """Checks the availability of environment variables."""
    undefined_vars = set(filter(lambda v: not os.getenv(v), ENV_VARS))
    if not undefined_vars:
        return True
    print(f"Required environment variables are missing: {undefined_vars}")
    return False
  • Related