Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Solution #682

wants to merge 2 commits into from

Conversation

molodsh1y1
Copy link

Screenshot 2024-12-23 085121

Copy link

@Arsen-hrynevych Arsen-hrynevych left a 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
Comment on lines 1 to 6
**/.idea/
**/.vscode/
**/.DS_Store
**/venv/
**/.pytest_cache/
**/**__pycache__/

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
Comment on lines 1 to 12
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"]

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
`

Comment on lines +13 to +14
URL = "http://api.weatherapi.com/v1/current.json?"
FILTERING = "Paris"

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
Comment on lines 1 to 9
import os
import requests
from requests.exceptions import (
ConnectionError,
JSONDecodeError,
RequestException
)

from dotenv import load_dotenv

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")

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
Comment on lines 18 to 43
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:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add short docstings

Copy link

@AnyoneClown AnyoneClown left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants