- import datetime as dt
- DATE_FORMAT = '%d.%m.%Y'
- class Record:
- def __init__(self, amount, comment='', date=None):
- self.amount = amount
- self.comment = comment
- if date is None:
- self.date = dt.date.today()
- else:
- self.date = dt.datetime.strptime(date, DATE_FORMAT).date()
- class Calculator:
- week_ago = dt.date.today() - dt.timedelta(days=7)
- now = dt.date.today()
- def __init__(self, limit):
- self.limit = limit
- self.records = []
- def add_record(self, record):
- self.records.append(record)
- def get_today_stats(self):
- return sum(record.amount for record in self.records
- if record.date == dt.date.today())
- def get_week_stats(self):
- return sum(record.amount for record in self.records
- if self.week_ago < record.date <= dt.date.today())
- class CaloriesCalculator(Calculator):
- REMAINS = ("Сегодня можно съесть что-нибудь ещё, "
- "но с общей калорийностью не более "
- "{val} кКал")
- ENOUGH = "Хватит есть!"
- def get_calories_remained(self):
- allow_calories = self.limit - self.get_today_stats()
- if allow_calories > 0:
- return self.REMAINS.format(val=allow_calories)
- return self.ENOUGH
- class CashCalculator(Calculator):
- USD_RATE = 60.0
- EURO_RATE = 70.0
- REMNANT = "На сегодня осталось {bal} {name}"
- INANE = "Денег нет, держись"
- DUTY = "Денег нет, держись: твой долг - {bal} {name}"
- EMPTY = "Валюта не найдена {name}"
- CURRENCIES = {
- 'rub': (1, 'руб'),
- "usd": (USD_RATE, 'USD'),
- "eur": (EURO_RATE, 'Euro')
- }
- def get_today_cash_remained(self, currency):
- if currency not in self.CURRENCIES:
- raise ValueError(self.EMPTY.format(name=currency))
- balance = self.limit - self.get_today_stats()
- if balance == 0:
- return self.INANE
- rate, name = self.CURRENCIES[currency]
- today_balance = abs(round(balance / rate, 2))
- if balance < 0:
- return self.DUTY.format(
- bal=today_balance, name=name)
- today_to_balance = round(balance / rate, 2)
- return self.REMNANT.format(
- bal=today_to_balance, name=name)
[text] dddd
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Editor
You can edit this paste and save as new: