0
0
Microservicessystem_design~10 mins

Environment-based configuration in Microservices - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to access the environment variable for the database URL.

Microservices
db_url = os.environ.get('[1]')
Drag options to blanks, or click blank then click option'
Adatabase
Bdb_url
CENV_DB
DDATABASE_URL
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or incorrect environment variable names.
Confusing variable names with environment variable keys.
2fill in blank
medium

Complete the code to load environment variables from a .env file in a microservice.

Microservices
from dotenv import load_dotenv

load_dotenv('[1]')
Drag options to blanks, or click blank then click option'
A.env
Bconfig.env
Cenv.txt
Dsettings.env
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect or uncommon filenames for environment files.
Forgetting the dot prefix in the filename.
3fill in blank
hard

Fix the error in the code to correctly select configuration based on environment.

Microservices
config = configs.get(os.environ.get('[1]'), configs['development'])
Drag options to blanks, or click blank then click option'
Aenv
Benvironment
CENV
Dconfig
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or incorrect keys that do not match environment variables.
Not providing a default configuration.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that filters environment variables starting with 'APP_'.

Microservices
app_config = {key: value for key, value in os.environ.items() if key.[1]('APP_') and value [2] ''}
Drag options to blanks, or click blank then click option'
Astartswith
Bendswith
C!=
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'endswith' instead of 'startswith' for the key check.
Checking if value equals empty string instead of not equals.
5fill in blank
hard

Fill all three blanks to define a function that returns the correct config value or a default.

Microservices
def get_config_value(key, default=None):
    return os.environ.get([1], [2]) if os.environ.get([3]) else default
Drag options to blanks, or click blank then click option'
Akey
Bdefault
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variables for the key in the function.
Confusing the default value placement.