Skip to content

Commit ae6c62b

Browse files
committed
stocks become more classy
1 parent 4f9abaa commit ae6c62b

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

domain_model.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ def allocate(order, warehouse, shipments):
55
allocation = Allocation(order, {})
66
for source in ordered_sources:
77
allocation.supplement_with(source.allocation_for(order))
8-
allocation.decrement_source_quantities()
8+
allocation.decrement_available_quantities()
99
return allocation
1010

1111

@@ -28,33 +28,44 @@ def supplement_with(self, other):
2828
if sku not in self:
2929
self.sources[sku] = qty
3030

31-
def decrement_source_quantities(self):
31+
def decrement_available_quantities(self):
3232
for sku, source in self.sources.items():
33-
source[sku] -= self.order[sku]
33+
source.decrement_available(sku, self.order[sku])
3434

3535

3636
@dataclass
37-
class OrderLine:
37+
class Line:
3838
sku: str
3939
qty: int
4040

4141

42-
class Order:
43-
def __init__(self, quantities):
44-
self.quantities = quantities
42+
class _SkuLines:
43+
44+
def __init__(self, linesdict):
45+
self.linesdict = linesdict
4546

4647
def __getitem__(self, sku):
47-
return self.quantities[sku]
48+
return self.linesdict[sku]
49+
50+
def __contains__(self, sku):
51+
return sku in self.linesdict
4852

4953
@property
5054
def lines(self):
5155
return [
52-
OrderLine(sku, qty)
53-
for sku, qty in self.quantities.items()
56+
Line(sku, qty)
57+
for sku, qty in self.linesdict.items()
5458
]
5559

5660

57-
class _Stock(dict):
61+
class Order(_SkuLines):
62+
pass
63+
64+
65+
class _Stock(_SkuLines):
66+
67+
def decrement_available(self, sku, qty):
68+
self.linesdict[sku] -= qty
5869

5970
def allocation_for(self, order):
6071
return Allocation(order, {
@@ -82,4 +93,3 @@ def __init__(self, d, eta):
8293

8394
def __lt__(self, other):
8495
return self.eta < other.eta
85-

0 commit comments

Comments
 (0)