Skip to content

Commit 44f5163

Browse files
committed
try to factor out common allocate helper fns
1 parent 5a79e53 commit 44f5163

1 file changed

Lines changed: 29 additions & 28 deletions

File tree

‎domain_model.py‎

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from dataclasses import dataclass
2-
from datetime import date
32

43
@dataclass
54
class Line:
@@ -13,55 +12,57 @@ class OrderLine(Line):
1312

1413

1514
@dataclass
16-
class Shipment:
17-
id: str
18-
eta: date
19-
lines: list
15+
class Shipment(list):
16+
def __init__(self, id, eta, lines):
17+
self.id = id
18+
self.eta = eta
19+
super().__init__(lines)
2020

2121

2222
def skus(thing):
23-
try:
24-
return {line.sku for line in thing}
25-
except TypeError:
26-
return {line.sku for line in thing.lines}
23+
return {line.sku for line in thing}
2724

2825

29-
def allocate_to(line, allocation, quantities):
30-
for quantity in quantities:
31-
if quantity.sku == line.sku and quantity.quantity > line.quantity:
26+
def allocate_to(line, allocation, source):
27+
for source_line in source:
28+
if source_line.sku == line.sku and source_line.quantity > line.quantity:
3229
line.allocation = allocation
33-
quantity.quantity -= line.quantity
30+
source_line.quantity -= line.quantity
3431
return
3532

36-
def allocate_to_stock(line, stock):
37-
allocate_to(line, 'STOCK', stock)
33+
def allocate_to_stock(order, stock):
34+
for line in order:
35+
allocate_to(line, 'STOCK', stock)
36+
3837

39-
def allocate_to_shipment(line, shipment):
40-
allocate_to(line, shipment.id, shipment.lines)
38+
def allocate_to_shipment(order, shipment):
39+
for line in order:
40+
allocate_to(line, shipment.id, shipment)
4141

4242

43-
def allocate_to_shipments(line, shipments):
43+
def allocate_to_shipments(order, shipments):
44+
for line in order:
45+
_allocate_line_to_shipments(line, shipments)
46+
47+
48+
def _allocate_line_to_shipments(line, shipments):
4449
for shipment in shipments:
45-
allocate_to_shipment(line, shipment)
46-
if line.allocation is not None:
50+
allocate_to(line, shipment.id, shipment)
51+
if line.allocation:
4752
return
4853

4954

5055
def allocate(order, stock, shipments):
5156
if skus(order) <= skus(stock):
52-
for line in order:
53-
allocate_to_stock(line, stock)
57+
allocate_to_stock(order, stock)
5458
return
5559

5660
shipments.sort(key=lambda s: s.eta)
5761

5862
for shipment in shipments:
5963
if skus(order) <= skus(shipment):
60-
for line in order:
61-
allocate_to(line, shipment.id, shipment.lines)
64+
allocate_to_shipment(order, shipment)
6265
return
6366

64-
for line in order:
65-
allocate_to_stock(line, stock)
66-
if line.allocation is None:
67-
allocate_to_shipments(line, shipments)
67+
allocate_to_shipments(order, shipments)
68+
allocate_to_stock(order, stock)

0 commit comments

Comments
 (0)