Skip to content

Commit 2554a1a

Browse files
committed
constructor fn, sorting are now more OO
1 parent eb37763 commit 2554a1a

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

domain_model.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ class Allocation(dict):
66
def skus(self):
77
return self.keys()
88

9+
@staticmethod
10+
def for_(order, source):
11+
return Allocation({
12+
sku: source
13+
for sku, quantity in order.lines.items()
14+
if source.can_allocate(sku, quantity)
15+
})
16+
17+
918
def supplement_with(self, allocation):
1019
for sku, quantity in allocation.items():
1120
if sku in self:
@@ -16,6 +25,8 @@ def fully_allocates(self, order):
1625
return self.skus == order.skus
1726

1827

28+
29+
1930
@dataclass
2031
class Order:
2132
lines: dict
@@ -31,12 +42,8 @@ def fully_allocated(self):
3142

3243
def allocate(self, stock, shipments):
3344
self.allocation = Allocation()
34-
for source in [stock] + sorted(shipments, key=lambda x: x.eta):
35-
source_allocation = Allocation({
36-
sku: source
37-
for sku, quantity in self.lines.items()
38-
if source.can_allocate(sku, quantity)
39-
})
45+
for source in [stock] + sorted(shipments):
46+
source_allocation = Allocation.for_(self, source)
4047
if source_allocation.fully_allocates(self):
4148
self.allocation = source_allocation
4249
return
@@ -49,9 +56,11 @@ def can_allocate(self, sku, quantity):
4956
return sku in self and self[sku] > quantity
5057

5158

52-
@dataclass
5359
class Shipment(Stock):
5460

61+
def __lt__(self, other):
62+
return self.eta < other.eta
63+
5564
def __init__(self, lines, eta):
5665
self.eta = eta
5766
super().__init__(lines)

0 commit comments

Comments
 (0)