generated from mwc/lab_weather
36 lines
1.4 KiB
Python
36 lines
1.4 KiB
Python
# weather.py
|
|
# ------------
|
|
# By MWC Contributors
|
|
#
|
|
# Defines `print_weather`, which does all the work of fetching
|
|
# the requested weather data and printing it out to the screen
|
|
# in a sensible way.
|
|
#
|
|
# It's your job to implement this function.
|
|
|
|
from weather.weather_apis import(
|
|
geocode_location,
|
|
estimate_location,
|
|
get_weather_office,
|
|
get_forecast
|
|
)
|
|
|
|
def print_weather(location=None, metric=False, verbose=False):
|
|
"""Prints out a weather report using the provided location, or using
|
|
the user's current location if no location was provided.
|
|
When metric is True, prints out the weather in metric units.
|
|
When verbose is True, prints out a more detailed report.
|
|
"""
|
|
#ask for location -> geocode location
|
|
if location:
|
|
coordinates = geocode_location(location)
|
|
#if none -> estimate location
|
|
else:
|
|
coordinates = estimate_location()
|
|
#coordinate -> get weather office -> get forecast
|
|
office = get_weather_office(coordinates["lat"], coordinates["lng"])
|
|
forecast = get_forecast(office["office"], office["x"], office["y"], metric=False)
|
|
for entry in forecast:
|
|
print(entry["name"], 'it will be', entry["temperature"], 'degrees and', entry["description"], 'with', entry["wind_speed"], entry["wind_direction"], 'winds.')
|
|
|
|
#'name': 'This Afternoon', 'temperature': 53, 'wind_speed': '6 mph', 'wind_direction': 'NW', 'description': 'Mostly Sunny' |