[text] ggg

Viewer

  1. import datetime as dt
  2.  
  3. DATE_FORMAT = '%d.%m.%Y'
  4.  
  5.  
  6. class Record:
  7.     def __init__(self, amount, comment='', date=None):
  8.         self.amount = amount
  9.         self.comment = comment
  10.         if date is None:
  11.             self.date = dt.date.today()
  12.         else:
  13.             self.date = dt.datetime.strptime(date, DATE_FORMAT).date()
  14.  
  15.  
  16. class Calculator:
  17.     week_ago = dt.date.today() - dt.timedelta(days=7)
  18.  
  19.     def __init__(self, limit):
  20.         self.limit = limit
  21.         self.records = []
  22.  
  23.     def add_record(self, record):
  24.         self.records.append(record)
  25.  
  26.     def get_today_stats(self):
  27.         return sum(record.amount for record in self.records
  28.                    if record.date == dt.date.today())
  29.  
  30.     def get_week_stats(self):
  31.         if self.week_ago < record.date <= dt.date.today():
  32.             return sum(record.amount for record in self.records)
  33.  
  34.  
  35. class CaloriesCalculator(Calculator):
  36.     REMAINS = ("Сегодня можно съесть что-нибудь ещё, "
  37.                "но с общей калорийностью не более "
  38.                "{val} кКал")
  39.     ENOUGH = "Хватит есть!"
  40.  
  41.     def get_calories_remained(self):
  42.         allow_calories = self.limit - self.get_today_stats()
  43.         if allow_calories > 0:
  44.             return self.REMAINS.format(val=allow_calories)
  45.         return self.ENOUGH
  46.  
  47.  
  48. class CashCalculator(Calculator):
  49.     USD_RATE = 60.0
  50.     EURO_RATE = 70.0
  51.     REMNANT = "На сегодня осталось {bal} {name}"
  52.     INANE = "Денег нет, держись"
  53.     DUTY = "Денег нет, держись: твой долг - {bal} {name}"
  54.     EMPTY = "Валюта не найдена {name}"
  55.     CURRENCIES = {
  56.         'rub': (1, 'руб'),
  57.         "usd": (USD_RATE, 'USD'),
  58.         "eur": (EURO_RATE, 'Euro')
  59.     }
  60.  
  61.     def get_today_cash_remained(self, currency):
  62.         if currency not in self.CURRENCIES:
  63.             raise ValueError(self.EMPTY.format(name=currency))
  64.         balance = self.limit - self.get_today_stats()
  65.         if balance == 0:
  66.             return self.INANE
  67.         rate, name = self.CURRENCIES[currency]
  68.         today_balance = abs(round(balance / rate, 2))
  69.         if balance < 0:
  70.             return self.DUTY.format(
  71.                 bal=today_balance, name=name)
  72.         return self.REMNANT.format(
  73.             bal=balance, name=name)

Editor

You can edit this paste and save as new: