-
Notifications
You must be signed in to change notification settings - Fork 678
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Solution #682
base: master
Are you sure you want to change the base?
Solution #682
Conversation
molodsh1y1
commented
Dec 23, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mostly correct. Thanks
.dockerignore
Outdated
**/.idea/ | ||
**/.vscode/ | ||
**/.DS_Store | ||
**/venv/ | ||
**/.pytest_cache/ | ||
**/**__pycache__/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please include more files
Dockerfile
Outdated
FROM python:3.12.0-slim | ||
|
||
ENV PYTHONUNBUFFERED 1 | ||
|
||
WORKDIR /app | ||
|
||
COPY requirements.txt requirements.txt | ||
RUN pip install -r requirements.txt | ||
|
||
COPY . . | ||
|
||
CMD ["python", "app/main.py"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
`# syntax=docker/dockerfile:1
# Comments are provided throughout this file to help you get started.
# If you need more help, visit the Dockerfile reference guide at
# https://docs.docker.com/go/dockerfile-reference/
# Want to help us make this template better? Share your feedback here: https://forms.gle/ybq9Krt8jtBL3iCk7
ARG PYTHON_VERSION=3.12
FROM python:${PYTHON_VERSION}-slim as base
# Prevents Python from writing pyc files.
ENV PYTHONDONTWRITEBYTECODE=1
# Keeps Python from buffering stdout and stderr to avoid situations where
# the application crashes without emitting any logs due to buffering.
ENV PYTHONUNBUFFERED=1
WORKDIR /app
# Create a non-privileged user that the app will run under.
# See https://docs.docker.com/go/dockerfile-user-best-practices/
ARG UID=10001
RUN adduser \
--disabled-password \
--gecos "" \
--home "/nonexistent" \
--shell "/sbin/nologin" \
--no-create-home \
--uid "${UID}" \
appuser
# Download dependencies as a separate step to take advantage of Docker's caching.
# Leverage a cache mount to /root/.cache/pip to speed up subsequent builds.
# Leverage a bind mount to requirements.txt to avoid having to copy them into
# into this layer.
RUN --mount=type=cache,target=/root/.cache/pip \
--mount=type=bind,source=requirements.txt,target=requirements.txt \
python -m pip install -r requirements.txt
# Switch to the non-privileged user to run the application.
USER appuser
# Copy the source code into the container.
COPY . .
# Run the application.
CMD python app/main.py
`
URL = "http://api.weatherapi.com/v1/current.json?" | ||
FILTERING = "Paris" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make it dynamic also
app/main.py
Outdated
import os | ||
import requests | ||
from requests.exceptions import ( | ||
ConnectionError, | ||
JSONDecodeError, | ||
RequestException | ||
) | ||
|
||
from dotenv import load_dotenv |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Imports should be grouped and separated by a blank line according to standard library, external libraries, and custom imports. This practice enhances both code readability and maintainability.
Read more about topic here
app/main.py
Outdated
|
||
URL = "http://api.weatherapi.com/v1/current.json?" | ||
FILTERING = "Paris" | ||
API_KEY = os.getenv("API_KEY") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
os.getenv doesn't raise a KeyError exception if the key is not provided. You can use os.environ and remove the if statement.
app/main.py
Outdated
def get_weather() -> None: | ||
# write your code here | ||
pass | ||
if not API_KEY: | ||
print("API key is missing. Please add it to the environment variables") | ||
return | ||
|
||
url = f"{URL}q={FILTERING}&key={API_KEY}" | ||
weather_data = fetch_data(url) | ||
if weather_data: | ||
print_weather_data(weather_data) | ||
|
||
|
||
def fetch_data(url: str) -> dict: | ||
try: | ||
response = requests.get(url) | ||
response.raise_for_status() | ||
return response.json() | ||
except ConnectionError as exc: | ||
print(f"Connection error: {exc}") | ||
except JSONDecodeError as exc: | ||
print(f"JSON decoding error: {exc}") | ||
except RequestException as exc: | ||
print(f"Request error: {exc}") | ||
return {} | ||
|
||
|
||
def print_weather_data(data: dict) -> None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add short docstings
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM