-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistory.py
33 lines (23 loc) · 843 Bytes
/
history.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from datetime import datetime
from pathlib import Path
from weather_api_service import Weather
from weather_formatter import format_weather
class WeatherStorage:
"""
Interface for a weather storage
"""
def save_weather(self, weather: Weather) -> None:
raise NotImplementedError()
class PlainFileWeatherStorage(WeatherStorage):
"""
Stores weather data in a plain text file
"""
def __init__(self, file_path: Path) -> None:
self._file_path = file_path
def save_weather(self, weather: Weather) -> None:
now = datetime.now()
formatted_weather = format_weather(weather)
with open(self._file_path, 'a') as f:
f.write(f"{now}\n{formatted_weather}\n")
def save_weather(weather: Weather, storage: WeatherStorage) -> None:
storage.save_weather(weather)