couldnt get past my error message

Yellow wiggly lines under weather_apis.py in the beginning. Tried to
follow error message which says that apis.py isn't included in the
directory. I tried to move it into lab_weather. It still doesn't work.
This commit is contained in:
Aminah
2024-10-31 11:04:58 -04:00
parent 86a3efa082
commit 953cc4de24
2 changed files with 54 additions and 1 deletions

View File

@@ -21,4 +21,57 @@ def print_weather(location=None, metric=False, verbose=False):
When metric is True, prints out the weather in metric units.
When verbose is True, prints out a more detailed report.
"""
print("Not finished...") # YOUR CODE HERE!
if location:
coordinates = geocode_location(location)
if not coordinates:
print("Error: Location not found.")
return
else:
coordinates = estimate_location()
if not coordinates:
print("Error: Unable to estimate location.")
return
office_info = get_weather_office(coordinates['lat'], coordinates['lng'])
if not office_info:
print("Error: Weather office not found for this location.")
return
forecast = get_forecast(office_info['office'], office_info['x'], office_info['y'], metric=metric)
if not forecast:
print("Error: Forecast data is unavailable.")
return
temp_unit = "°C" if metric else "°F"
print(f"Weather Forecast for {location or 'your location'}:")
for period in forecast:
temp = period['temperature']
description = period['description']
wind = period['wind_speed']
wind_dir = period['wind_direction']
print(f"{period['name']}: {temp}{temp_unit}, {description}")
if verbose:
print(f" Wind: {wind} {wind_dir}")
from argparse import ArgumentParser
from weather.weather_main import print_weather # Ensure the correct import path if your file is renamed
def weather_cli():
"""Provides a command-line interface for weather.
This function creates an ArgumentParser, which parses command line arguments.
Then it calls `print_weather` with the provided arguments.
"""
parser = ArgumentParser("weather", description="Prints out a weather report")
parser.add_argument("-l", "--location", help="Location for weather forecast")
parser.add_argument("-m", "--metric", action="store_true", help="Use metric units")
parser.add_argument("-v", "--verbose", action="store_true", help="Verbose output")
args = parser.parse_args()
print_weather(location=args.location, metric=args.metric, verbose=args.verbose)

View File

@@ -1,90 +0,0 @@
# weather_apis.py
# ---------------
# By MWC Contributors
#
# This module contains functions which interact with external APIs related to weather.
# The module relies on USA-specific services; it will need to be extended using local
# services for other regions.
# The National Weather Service (NWS) provides weather forecasting services across US
# states and territories. NWS divides the country into a grid of 2.5km squares, and
# provides a forecast for each grid square.
#
# You will need to use these functions, but you don't need to edit this file.
import geocoder
from geocoder.osm import OsmQuery
import requests
class OsmQueryWithHeaders(OsmQuery):
def _build_headers(self, provider_key, **kwargs):
return {"User-Agent": "Making With Code CS Curriculum"}
def geocode_location(location_string):
"""Translates a location string into latitude and longitude coordinates.
Uses the OpenStreetMap API. Returns a dict with keys 'lat' and 'lng'
as shown below. When no result is found, returns None.
>>> geocode_location('11 Wall Street, New York')
{"lat": -74.010865, "lng": 40.7071407}
"""
result = OsmQueryWithHeaders(location_string)
if result:
lat, lng = result.latlng
return {'lat': lat, 'lng': lng}
def estimate_location(ip_address=None):
"""Estimates a location based on the request's IP address, returning
latitude and longitude coodrdinates. When no IP address is provided,
uses the user's current IP address.
>>> geocode_ip_address()
{'lat': 23.6585116, 'lng': -102.0077097}
"""
result = geocoder.ip(ip_address or 'me')
if result:
lat, lng = result.latlng
return {'lat': lat, 'lng': lng}
def get_weather_office(lat, lng):
"""Looks up the NWS weather office for a pair of lat/lng coordinates.
Returns a dict containing keys 'office', 'x', and 'y'.
If no matching weather office is found, returns None.
>>> coords = geocode_ip_address()
>>> get_weather_office(coords['lat'], coords['lng'])
{'office': 'BUF', 'x': 39, 'y': 59}
"""
url = "https://api.weather.gov/points/{},{}".format(lat, lng)
response = requests.get(url)
if response.ok:
result = response.json()
return {
"office": result['properties']['gridId'],
"x": result['properties']['gridX'],
'y': result['properties']['gridY']
}
def get_forecast(office, x, y, metric=False):
"""Fetches the weather forecast for the given NWS office, and (x, y) NWS grid tile.
Returns a list of time periods, where each time period is a dict containing keys
as shown below. If no forecast can be found, returns None.
When metric is True, returns temperatures in Celcius and wind speeds in km/hr.
"""
url = "https://api.weather.gov/gridpoints/{}/{},{}/forecast".format(office, x, y)
if metric:
url += "?units=si"
response = requests.get(url)
if response.ok:
result = response.json()
forecast = []
for period in result['properties']['periods']:
forecast.append({
'name': period['name'],
'temperature': period['temperature'],
'wind_speed': period['windSpeed'],
'wind_direction': period['windDirection'],
'description': period['shortForecast'],
})
return forecast