CommencerCommencer gratuitement

Retrieving key-value data with Redis

Now that you've practiced storing key-value pairs with Redis and Python, it's time to explore the other half of that process: retrieving key-value pairs. In this example, you'll practice retrieving a few different key-value pairs that you might find in a web application built to report on weather data.

A connection object has been created and stored in the variable redis_conn. Enjoy!

Cet exercice fait partie du cours

Introduction to NoSQL

Afficher le cours

Instructions

  • Use the redis_conn variable and the appropriate method to try and parse the temperature for each city in the list of cities from Redis.
  • If the temperature value is None for the passed key, set "unknown temperature" as the value for that key, and print a short message.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

# Loop through each of the cities
for city in cities:
	# Grab the temperature
    temperature = ____.___(f"{city}_temp")
    
    # Check if the temperature is None
    if temperature is None:
    	# Store an unknown temperature
        ____.____(f"{city}_temp", "unknown temperature")
        print(f"Unknown temperature in {city}")
    
    else:
      	# Otherwise, print the temperature
    	print(f"The temperature in {city} is {temperature}")
Modifier et exécuter le code