Skip to content

Commit 1223c18

Browse files
committed
refactor out some parent classes to remove duplication
1 parent e2c4eea commit 1223c18

2 files changed

Lines changed: 14 additions & 19 deletions

File tree

domain_model.py

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
from typing import List
33
from datetime import date
44

5-
65
@dataclass
76
class OrderLine:
87
sku: str
98
quantity: int
109

1110

11+
1212
@dataclass
13-
class Order:
13+
class SkuLines:
1414
lines: List[OrderLine]
1515

1616
@property
@@ -21,6 +21,12 @@ def skus(self):
2121
def quantities(self):
2222
return {l.sku: l.quantity for l in self.lines}
2323

24+
25+
26+
27+
@dataclass
28+
class Order(SkuLines):
29+
2430
@property
2531
def fully_allocated(self):
2632
return self.allocation.is_complete
@@ -37,21 +43,9 @@ def allocate(self, stock, shipments):
3743
self.allocation.apply()
3844

3945

40-
class StockLine(OrderLine):
41-
pass
42-
4346

4447
@dataclass
45-
class Stock:
46-
lines: List[StockLine]
47-
48-
@property
49-
def skus(self):
50-
return set(l.sku for l in self.lines)
51-
52-
@property
53-
def quantities(self):
54-
return {l.sku: l.quantity for l in self.lines}
48+
class Stock(SkuLines):
5549

5650
def can_allocate(self, line: OrderLine):
5751
return line.sku in self.skus and self.quantities[line.sku] > line.quantity
@@ -60,6 +54,8 @@ def allocate(self, sku, quantity):
6054
for line in self.lines:
6155
if line.sku == sku:
6256
line.quantity -= quantity
57+
return
58+
raise Exception(f'sku {sku} not found in stock skus ({self.skus})')
6359

6460

6561
@dataclass
@@ -90,7 +86,7 @@ def sources(self):
9086
return {l.sku: l.source for l in self.lines}
9187

9288
@staticmethod
93-
def for_(order, source):
89+
def for_(order: Order, source: Stock):
9490
return Allocation(
9591
lines=[
9692
AllocationLine(sku=line.sku, source=source) for line in order.lines
@@ -113,4 +109,3 @@ def apply(self):
113109
for line in self.lines:
114110
line.source.allocate(line.sku, self.order.quantities[line.sku])
115111

116-

test_allocation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
later = tomorrow + timedelta(days=10)
77

88
Order = lambda d: domain_model.Order(lines=[domain_model.OrderLine(sku, qty) for sku, qty in d.items()])
9-
Stock = lambda d: domain_model.Stock(lines=[domain_model.StockLine(sku, qty) for sku, qty in d.items()])
9+
Stock = lambda d: domain_model.Stock(lines=[domain_model.OrderLine(sku, qty) for sku, qty in d.items()])
1010
Shipment = lambda d, eta: domain_model.Shipment(
11-
lines=[domain_model.StockLine(sku, qty) for sku, qty in d.items()],
11+
lines=[domain_model.OrderLine(sku, qty) for sku, qty in d.items()],
1212
eta=eta,
1313
)
1414

0 commit comments

Comments
 (0)