8000 Sample code for the article on `uv` · realpython/materials@257a83f · GitHub
[go: up one dir, main page]

Skip to content

Commit 257a83f

Browse files
committed
Sample code for the article on uv
1 parent a0f3307 commit 257a83f

File tree

4 files changed

+234
-0
lines changed

4 files changed

+234
-0
lines changed

python-uv/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Managing Python Projects With `uv`: An All-in-One Solution
2+
3+
This folder provides the code examples for the Real Python tutorial [Managing Python Projects With `uv`: An All-in-One Solution](https://realpython.com/python-uv/).

python-uv/main.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import argparse
2+
3+
import requests
4+
5+
WEATHER_EMOJIS = {
6+
"clear": "☀️",
7+
"sunny": "☀️",
8+
"cloudy": "☁️",
9+
"partly cloudy": "⛅",
10+
"rain": "🌧️",
11+
"light rain": "🌦️",
12+
"heavy rain": "🌊",
13+
"storm": "🌩️",
14+
"snow": "❄️",
15+
"fog": "🌫️",
16+
"mist": "🌫️",
17+
"default": "🌍",
18+
}
19+
20+
API_QUERY_TEMPLATE = "https://wttr.in/{city}?format=%C+%t"
21+
22+
23+
def get_weather_emoji(condition, weather_emojis=WEATHER_EMOJIS):
24+
condition = condition.lower()
25+
for key, emoji in weather_emojis.items():
26+
if key in condition:
27+
return emoji
28+
return weather_emojis["default"]
29+
30+
31+
def get_weather(city, api_query_template=API_QUERY_TEMPLATE):
32+
api_query = api_query_template.format(city=city)
33+
try:
34+
response = requests.get(api_query)
35+
response.raise_for_status()
36+
weather_info = response.text.strip().split("+")
37+
if len(weather_info) < 2:
38+
return "Error: Unexpected weather data format."
39+
condition = weather_info[0].strip()
40+
temperature = weather_info[1].strip()
41+
emoji = get_weather_emoji(condition)
42+
return f"{emoji} {condition} -> {temperature}"
43+
except requests.RequestException:
44+
return "Error: Could not retrieve weather data."
45+
46+
47+
def parse_cli_args():
48+
parser = argparse.ArgumentParser(
49+
prog="weather",
50+
description="Weather information for the specified city.",
51+
epilog="Thanks for using %(prog)s! :)",
52+
)
53+
parser.add_argument(
54+
"city",
55+
nargs="+",
56+
help="Name of the city to get weather information for",
57+
)
58+
parser.add_argument(
59+
"--version",
60+
action="version",
61+
version="%(prog)s 0.1.0",
62+
)
63+
return parser.parse_args()
64+
65+
66+
def main():
67+
args = parse_cli_args()
68+
weather = get_weather(" ".join(args.city))
69+
print(weather)
70+
71+
72+
if __name__ == "__main__":
73+
main()

python-uv/pyproject.toml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[project]
2+
name = "rpweather"
3+
version = "0.1.0"
4+
description = "Display weather information for the specified city."
5+
readme = "README.md"
6+
requires-python = ">=3.13"
7+
dependencies = [
8+
"requests>=2.32.3",
9+
]
10+
11+
[dependency-groups]
12+
dev = [
13+
"pytest>=8.3.5",
14+
]
15+
16+
[project.scripts]
17+
rpweather = "main:main"
18+
19+
[build-system]
20+
requires = ["setuptools>=78.1.0", "wheel>=0.45.1"]
21+
build-backend = "setuptools.build_meta"

python-uv/uv.lock

Lines changed: 137 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
0