mirror of
https://github.com/Redume/Kekkai.git
synced 2025-02-23 12:43:12 +03:00
refactor(chart): Added docstring in each file and function. Fixed long lines
This commit is contained in:
parent
c5d3d9fe9b
commit
ed8cf91558
7 changed files with 190 additions and 30 deletions
|
@ -1,3 +1,8 @@
|
|||
"""
|
||||
This module provides functionality to generate currency rate charts
|
||||
based on historical data retrieved from the database.
|
||||
"""
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from matplotlib import pyplot as plt
|
||||
|
@ -5,7 +10,29 @@ from matplotlib import pyplot as plt
|
|||
from chart.function.gen_unique_name import generate_unique_name
|
||||
from ..database.server import create_pool
|
||||
|
||||
async def create_chart(from_currency: str, conv_currency: str, start_date: str, end_date: str) -> (str, None):
|
||||
async def create_chart(
|
||||
from_currency: str,
|
||||
conv_currency: str,
|
||||
start_date: str,
|
||||
end_date: str
|
||||
) -> (str, None):
|
||||
"""
|
||||
Generates a line chart of currency rates for a given date range.
|
||||
|
||||
The chart shows the exchange rate trend between `from_currency` and
|
||||
`conv_currency` within the specified `start_date` and `end_date` range.
|
||||
The generated chart is saved as a PNG file, and the function returns the
|
||||
file name. If data is invalid or insufficient, the function returns `None`.
|
||||
|
||||
Args:
|
||||
from_currency (str): The base currency (e.g., "USD").
|
||||
conv_currency (str): The target currency (e.g., "EUR").
|
||||
start_date (str): The start date in the format 'YYYY-MM-DD'.
|
||||
end_date (str): The end date in the format 'YYYY-MM-DD'.
|
||||
|
||||
Returns:
|
||||
str | None: The name of the saved chart file, or `None` if the operation fails.
|
||||
"""
|
||||
pool = await create_pool()
|
||||
|
||||
if not validate_date(start_date) or not validate_date(end_date):
|
||||
|
@ -58,6 +85,15 @@ async def create_chart(from_currency: str, conv_currency: str, start_date: str,
|
|||
|
||||
|
||||
def validate_date(date_str: str) -> bool:
|
||||
"""
|
||||
Validates whether the provided string is a valid date in the format 'YYYY-MM-DD'.
|
||||
|
||||
Args:
|
||||
date_str (str): The date string to validate.
|
||||
|
||||
Returns:
|
||||
bool: `True` if the string is a valid date, `False` otherwise.
|
||||
"""
|
||||
try:
|
||||
datetime.strptime(date_str, '%Y-%m-%d')
|
||||
return True
|
||||
|
|
|
@ -1,9 +1,23 @@
|
|||
"""
|
||||
This module provides a function to generate a unique name for chart files.
|
||||
"""
|
||||
import datetime
|
||||
import random
|
||||
import string
|
||||
|
||||
|
||||
async def generate_unique_name(currency_pair: str, date: datetime) -> str:
|
||||
"""
|
||||
Generates a unique name for a chart file based on the currency pair,
|
||||
current date, and a random suffix.
|
||||
|
||||
Args:
|
||||
currency_pair (str): A string representing the currency pair (e.g., "USD_EUR").
|
||||
date (datetime.datetime): The current datetime object.
|
||||
|
||||
Returns:
|
||||
str: A unique name in the format "CURRENCYPAIR_YYYYMMDD_RANDOMSUFFIX".
|
||||
"""
|
||||
date_str = date.strftime("%Y%m%d")
|
||||
random_suffix = ''.join(random.choices(string.ascii_uppercase + string.digits, k=6))
|
||||
unique_name = f"{currency_pair}_{date_str}_{random_suffix}"
|
||||
|
|
Loading…
Add table
Reference in a new issue