generated from mwc/lab_weather
Compare commits
4 Commits
ab5959075d
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
953cc4de24 | ||
|
|
86a3efa082 | ||
|
|
8445b404bb | ||
|
|
938be1783b |
@@ -17,7 +17,11 @@ def count_people(people):
|
|||||||
>>> count_people(friends)
|
>>> count_people(friends)
|
||||||
10
|
10
|
||||||
"""
|
"""
|
||||||
raise NotImplementedError()
|
count = 0
|
||||||
|
for x in people:
|
||||||
|
count= count+1
|
||||||
|
return count
|
||||||
|
|
||||||
|
|
||||||
def get_email(people, name):
|
def get_email(people, name):
|
||||||
"""Returns the named person's email address. If there is no such person, returns None.
|
"""Returns the named person's email address. If there is no such person, returns None.
|
||||||
@@ -27,7 +31,11 @@ def get_email(people, name):
|
|||||||
>>> get_email(friends, "Tad Winters")
|
>>> get_email(friends, "Tad Winters")
|
||||||
None
|
None
|
||||||
"""
|
"""
|
||||||
raise NotImplementedError()
|
for person in people:
|
||||||
|
if person["name"] == name:
|
||||||
|
if "email" in person:
|
||||||
|
return person["email"]
|
||||||
|
return None
|
||||||
|
|
||||||
def count_favorite_colors(people, name):
|
def count_favorite_colors(people, name):
|
||||||
"""Returns the number of colors liked by the named person. If there is no such person, returns None.
|
"""Returns the number of colors liked by the named person. If there is no such person, returns None.
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ version = "0.1.0"
|
|||||||
description = ""
|
description = ""
|
||||||
authors = ["Chris <chris@chrisproctor.net>"]
|
authors = ["Chris <chris@chrisproctor.net>"]
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
packages = [ { include = "weather"} ]
|
||||||
|
|
||||||
[tool.poetry.dependencies]
|
[tool.poetry.dependencies]
|
||||||
python = "^3.10"
|
python = "^3.10"
|
||||||
|
|||||||
44
weather.py
Normal file
44
weather.py
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
from argparse import ArgumentParser
|
||||||
|
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.
|
||||||
|
"""
|
||||||
|
print("Not finished...")
|
||||||
|
|
||||||
|
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}")
|
||||||
43
weather.py.save
Normal file
43
weather.py.save
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
^X
|
||||||
|
^X
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
ls
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -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 metric is True, prints out the weather in metric units.
|
||||||
When verbose is True, prints out a more detailed report.
|
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)
|
||||||
|
|
||||||
|
|||||||
@@ -13,8 +13,13 @@
|
|||||||
# You will need to use these functions, but you don't need to edit this file.
|
# You will need to use these functions, but you don't need to edit this file.
|
||||||
|
|
||||||
import geocoder
|
import geocoder
|
||||||
|
from geocoder.osm import OsmQuery
|
||||||
import requests
|
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):
|
def geocode_location(location_string):
|
||||||
"""Translates a location string into latitude and longitude coordinates.
|
"""Translates a location string into latitude and longitude coordinates.
|
||||||
Uses the OpenStreetMap API. Returns a dict with keys 'lat' and 'lng'
|
Uses the OpenStreetMap API. Returns a dict with keys 'lat' and 'lng'
|
||||||
@@ -23,7 +28,7 @@ def geocode_location(location_string):
|
|||||||
>>> geocode_location('11 Wall Street, New York')
|
>>> geocode_location('11 Wall Street, New York')
|
||||||
{"lat": -74.010865, "lng": 40.7071407}
|
{"lat": -74.010865, "lng": 40.7071407}
|
||||||
"""
|
"""
|
||||||
result = geocoder.osm(location_string)
|
result = OsmQueryWithHeaders(location_string)
|
||||||
if result:
|
if result:
|
||||||
lat, lng = result.latlng
|
lat, lng = result.latlng
|
||||||
return {'lat': lat, 'lng': lng}
|
return {'lat': lat, 'lng': lng}
|
||||||
Reference in New Issue
Block a user