Guest User

Untitled

a guest
Mar 15th, 2024
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.73 KB | None | 0 0
  1. from datetime import datetime, date, timedelta
  2. from decimal import Decimal
  3. DATE_FORMAT = '%Y-%m-%d'
  4. goods = {}
  5.  
  6.  
  7. def add(items, title, amount, expiration_date=None):
  8.     if title in items:
  9.         if expiration_date:
  10.              expiration_date = datetime.strptime(expiration_date, '%Y-%m-%d').date()
  11.              items[title].append({'amount': amount, 'expiration_date': expiration_date})
  12.         else:
  13.             items[title] = [{'amount': amount, 'expiration_date': expiration_date}]    
  14.                              
  15.  #add({}, 'Паэлья', Decimal('1'), '2024-03-20')
  16. #print(goods)
  17. def add_by_note(items, note):
  18.     string = note.split()
  19.     if string[-1].count('/') == 2 and all(char.isdigit() or char == '/' for char in string[-1]):
  20.         quantity = Decimal(string[-1])
  21.         product = ' '.join(string[:-1])
  22.     else:
  23.         return 'Неправильно. Последняя часть  должна быть датой в формате dd/mm/yyyy'
  24.    
  25.     items.append({'product': product, 'quantity': quantity})
  26.     items = add(items, product, quantity, string[-1])
  27.     return items
  28.    
  29.    
  30. def find(items, needle):
  31.     outcome = []
  32.     for key in items:
  33.         if needle.lower() in key.lower():
  34.             outcome.append(key)
  35.     return outcome
  36.  
  37.  
  38. def amount(items, needle):
  39.     total_amount = 0
  40.     matching_items = [item for item in items if item.find(needle) != -1]
  41.     for item in matching_items:
  42.         total_amount += item.amount
  43.     return total_amount
  44.  
  45.  
  46. def expire(items, in_advance_days=0):
  47.     today = date.today()
  48.     expiration_date = item.expiration_date
  49.     warning_date = expiration_date - timedelta(days=in_advance_days)
  50.  
  51.     return today >= expiration_date or today >= warning_date
Advertisement
Add Comment
Please, Sign In to add comment