Last active
September 11, 2021 01:08
-
-
Save bayborodin/ae5c906248957db83fba3ddacf43ae30 to your computer and use it in GitHub Desktop.
Revisions
-
bayborodin revised this gist
Sep 11, 2021 . 1 changed file with 1 addition and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,9 +8,7 @@ class Calculator: """Basic functionality for registering records and calculating statistics.""" def __init__(self, limit: int) -> None: -
bayborodin created this gist
Sep 11, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,129 @@ from __future__ import annotations import datetime as dt import decimal from functools import reduce class Calculator: """Basic functionality for registering records and calculating statistics. """ def __init__(self, limit: int) -> None: self.limit = limit self.records = [] def add_record(self, record: Record) -> None: """Store a new record in the internal database.""" self.records.append(record) def get_today_stats(self) -> decimal: """Calculate the current day statistics.""" today = dt.datetime.now().date() today_records = list(filter( lambda record: record.date == today, self.records)) if today_records: return reduce(lambda x, y: x + y.amount, today_records, 0) return 0.0 def get_week_stats(self) -> decimal: """Calculate the last 7 days statistics.""" today = dt.datetime.now().date() date_from = today + dt.timedelta(days=-6) week_records = list( filter( lambda record: date_from <= record.date <= today, self.records ) ) if week_records: return reduce(lambda x, y: x + y.amount, week_records, 0) return 0.0 class Record: """The entry to be stored in the calculator.""" def __init__( self, amount: decimal, comment: str, date: str = None, ) -> None: self.amount = amount self.comment = comment if date: self.date = dt.datetime.strptime(date, '%d.%m.%Y').date() else: self.date = dt.datetime.now().date() class CaloriesCalculator(Calculator): """Calories calculator.""" def get_calories_remained(self) -> str: """Returns the remaining amount of calories.""" today_calories = self.get_today_stats() limit = self.limit remain = limit - today_calories if remain > 0: return ( 'Сегодня можно съесть что-нибудь ещё, ' f'но с общей калорийностью не более {remain} кКал' ) return 'Хватит есть!' class CashCalculator(Calculator): """Money spent calculator.""" EURO_RATE = 89.97 USD_RATE = 74.85 RUB_RATE = 1.0 def get_today_cash_remained(self, currency: str) -> str: """Returns the available cash budget.""" currencies = { 'rub': {'rate': self.RUB_RATE, 'label': 'руб'}, 'usd': {'rate': self.USD_RATE, 'label': 'USD'}, 'eur': {'rate': self.EURO_RATE, 'label': 'Euro'} } currency_rate = currencies[currency]['rate'] currency_label = currencies[currency]['label'] today_cash_spent = self.get_today_stats() limit = self.limit remain = round((limit - today_cash_spent) / currency_rate, 2) if remain > 0: return f'На сегодня осталось {remain} {currency_label}' elif remain == 0: return 'Денег нет, держись' return ( 'Денег нет, держись: ' f'твой долг - {abs(remain)} {currency_label}' )