Skip to content

Commit e29efff

Browse files
committed
slightly simpler impl
1 parent 0710aae commit e29efff

File tree

2 files changed

+7
-21
lines changed

2 files changed

+7
-21
lines changed

domain_model.py

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
from dataclasses import dataclass
2-
3-
@dataclass
41
class Order(dict):
5-
def __init__(self, d):
2+
def __init__(self, lines):
63
self.allocations = {}
7-
super().__init__(d)
4+
super().__init__(lines)
85

96

107
class Shipment(dict):
@@ -21,20 +18,11 @@ def skus(thing):
2118
return thing.keys()
2219

2320

24-
def allocate_to(order, source, name):
21+
def allocate_to(order, source):
2522
for sku, quantity in order.items():
2623
if source.get(sku, 0) > quantity:
2724
source[sku] -= quantity
28-
order.allocations[sku] = name
29-
30-
31-
def allocate_to_stock(order, stock):
32-
allocate_to(order, stock, 'STOCK')
33-
34-
def allocate_to_shipment(order, shipment):
35-
allocate_to(order, shipment, shipment.id)
36-
37-
25+
order.allocations[sku] = getattr(source, 'id', 'STOCK')
3826

3927

4028
def allocate_to_shipments(order, shipments):
@@ -50,15 +38,15 @@ def allocate_to_shipments(order, shipments):
5038

5139
def allocate(order, stock, shipments):
5240
if skus(order) <= skus(stock):
53-
allocate_to_stock(order, stock)
41+
allocate_to(order, stock)
5442
return
5543

5644
shipments.sort(key=lambda s: s.eta)
5745

5846
for shipment in shipments:
5947
if skus(order) <= skus(shipment):
60-
allocate_to_shipment(order, shipment)
48+
allocate_to(order, shipment)
6149
return
6250

63-
allocate_to_stock(order, stock)
51+
allocate_to(order, stock)
6452
allocate_to_shipments(order, shipments)

test_allocation.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
)
1010

1111

12-
13-
1412
def random_id():
1513
return uuid.uuid4().hex[:10]
1614

0 commit comments

Comments
 (0)