Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from datetime import datetime, date, timedelta
- from decimal import Decimal
- DATE_FORMAT = '%Y-%m-%d'
- goods = {}
- def add(items, title, amount, expiration_date=None):
- if title in items:
- if expiration_date:
- expiration_date = datetime.strptime(expiration_date, '%Y-%m-%d').date()
- items[title].append({'amount': amount, 'expiration_date': expiration_date})
- else:
- items[title] = [{'amount': amount, 'expiration_date': expiration_date}]
- #add({}, 'Паэлья', Decimal('1'), '2024-03-20')
- #print(goods)
- def add_by_note(items, note):
- string = note.split()
- if string[-1].count('/') == 2 and all(char.isdigit() or char == '/' for char in string[-1]):
- quantity = Decimal(string[-1])
- product = ' '.join(string[:-1])
- else:
- return 'Неправильно. Последняя часть должна быть датой в формате dd/mm/yyyy'
- items.append({'product': product, 'quantity': quantity})
- items = add(items, product, quantity, string[-1])
- return items
- def find(items, needle):
- outcome = []
- for key in items:
- if needle.lower() in key.lower():
- outcome.append(key)
- return outcome
- def amount(items, needle):
- total_amount = 0
- matching_items = [item for item in items if item.find(needle) != -1]
- for item in matching_items:
- total_amount += item.amount
- return total_amount
- def expire(items, in_advance_days=0):
- today = date.today()
- expiration_date = item.expiration_date
- warning_date = expiration_date - timedelta(days=in_advance_days)
- return today >= expiration_date or today >= warning_date
Advertisement
Add Comment
Please, Sign In to add comment