diff --git a/review-evaluation/order_service.py b/review-evaluation/order_service.py index 99319fd..b521d89 100644 --- a/review-evaluation/order_service.py +++ b/review-evaluation/order_service.py @@ -5,21 +5,18 @@ import json def can_fulfil(items, stock): """True when every line in the cart is in stock.""" - for item in items: - if stock.get(item.sku, 0) < item.quantity: - return False - return True + return all(stock.get(item.sku, 0) >= item.quantity for item in items) def load_order(path): """Read one order. Missing files and malformed JSON raise.""" - with open(path) as handle: - return json.load(handle) + with open(path) as order_file: + return json.load(order_file) def release_expired(stock, reservations, now): """Return expired stock and retain unexpired reservations.""" - for reservation in list(reservations): + for reservation in reservations.copy(): if reservation.expires_at < now: stock[reservation.sku] = stock.get(reservation.sku, 0) + reservation.quantity reservations.remove(reservation) @@ -27,7 +24,4 @@ def release_expired(stock, reservations, now): def subtotal(items): """Sum price times quantity for every cart line.""" - total = Decimal("0") - for item in items: - total += item.price * item.quantity - return total + return sum((item.price * item.quantity for item in items), Decimal("0"))