[text] dddd

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

Editor

You can edit this paste and save as new:


File Description
  • dddd
  • Paste Code
  • 28 Feb-2021
  • 2.45 Kb
You can Share it: