47 lines
1.5 KiB
Python
47 lines
1.5 KiB
Python
import os
|
||
|
||
# 1. Загрузка заказов из файла
|
||
def load_orders(filepath: str) -> list[dict]:
|
||
"""
|
||
Читает файл с заказами.
|
||
Формат строки: user_id, item_name, quantity, price_per_item
|
||
"""
|
||
orders = []
|
||
with open(filepath, 'r', encoding='utf-8') as file:
|
||
for line in file:
|
||
line = line.strip()
|
||
if not line:
|
||
continue
|
||
parts = line.split(',')
|
||
if len(parts) != 4:
|
||
continue
|
||
|
||
user_id = int(parts[0].strip())
|
||
item_name = parts[1].strip()
|
||
quantity = int(parts[2].strip())
|
||
price = float(parts[3].strip())
|
||
total = quantity * price
|
||
|
||
orders.append({
|
||
'user_id': user_id,
|
||
'item_name': item_name,
|
||
'quantity': quantity,
|
||
'price': price,
|
||
'total': total
|
||
})
|
||
return orders
|
||
|
||
|
||
# 2. Валидация заказов (удаление некорректных)
|
||
def validate_orders(orders: list[dict]) -> list[dict]:
|
||
"""
|
||
Отбрасывает заказы с quantity <= 0, price <= 0 или пустым item_name.
|
||
"""
|
||
valid_orders = []
|
||
for order in orders:
|
||
if (order['quantity'] > 0 and
|
||
order['price'] > 0 and
|
||
order['item_name'].strip() != ''):
|
||
valid_orders.append(order)
|
||
return valid_orders
|