Advertisement
DiasITPython

Untitled

May 19th, 2021
270
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import datetime as dt
  2. from typing import Optional
  3.  
  4. DATE_FORMAT = '%d.%m.%Y'
  5. TODAY = dt.date.today()
  6.  
  7.  
  8. class Calculator:
  9.  
  10.     def __init__(self, limit):
  11.         self.limit = limit
  12.         self.records = []
  13.  
  14.     def add_record(self, record):
  15.         self.records.append(record)
  16.  
  17.     def get_today_stats(self):
  18.         return sum(record.amount for record in self.records if
  19.                    record.date == TODAY)
  20.  
  21.     def get_week_stats(self):
  22.         week_ago = TODAY - dt.timedelta(days=7)
  23.         return sum(record.amount for record in self.records if
  24.                    week_ago < record.date <= TODAY)
  25.  
  26.     def remained(self):
  27.         return self.limit - self.get_today_stats()
  28.  
  29.  
  30. class Record:
  31.     def __init__(self, amount, comment, date: Optional[str] = None):
  32.         self.amount = abs(amount)
  33.         self.comment = comment
  34.         if date is not None:
  35.             self.date = dt.datetime.strptime(date, DATE_FORMAT).date()
  36.         else:
  37.             self.date = dt.date.today()
  38.  
  39.  
  40. class CaloriesCalculator(Calculator):
  41.     def get_calories_remained(self):
  42.         calories_remained = self.remained()
  43.         if calories_remained <= 0:
  44.             return 'Хватит есть!'
  45.         else:
  46.             return ('Сегодня можно съесть что-нибудь ещё, но с общей '
  47.                     f'калорийностью не более {calories_remained} кКал'
  48.                     .format(calories_remained=self.remained))
  49.  
  50.  
  51. class CashCalculator(Calculator):
  52.     USD_RATE = 60.0
  53.     EURO_RATE = 70.0
  54.     RUB_RATE = 1.0
  55.  
  56.     def get_today_cash_remained(self, currency):
  57.         cash_remained = self.remained()
  58.         money = {
  59.             'eur': ('Euro', self.EURO_RATE),
  60.             'usd': ('USD', self.USD_RATE),
  61.             'rub': ('руб', self.RUB_RATE),
  62.         }
  63.         sign, rate = money[currency]
  64.         if currency not in money:
  65.             raise KeyError('Валюта не найдена')
  66.         cash_remained = round(cash_remained / rate, 2)
  67.         if cash_remained > 0:
  68.             return f'На сегодня осталось {cash_remained} {sign}'
  69.         if cash_remained < 0:
  70.             cash_remained = abs(cash_remained)
  71.             return f'Денег нет, держись: твой долг - {cash_remained} {sign}'
  72.         else:
  73.             return 'Денег нет, держись'
  74.  
Advertisement
RAW Paste Data Copied
Advertisement