Skip to content

Commit 99e955a

Browse files
committed
tweak a few constructors, start adding some type hints
1 parent ae6c62b commit 99e955a

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

domain_model.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from dataclasses import dataclass
2+
from datetime import date
3+
24

35
def allocate(order, warehouse, shipments):
46
ordered_sources = [warehouse] + sorted(shipments)
5-
allocation = Allocation(order, {})
7+
allocation = Allocation(order)
68
for source in ordered_sources:
79
allocation.supplement_with(source.allocation_for(order))
810
allocation.decrement_available_quantities()
@@ -13,23 +15,23 @@ def allocate(order, warehouse, shipments):
1315

1416
class Allocation:
1517

16-
def __init__(self, order, sources):
18+
def __init__(self, order):
1719
self.order = order
18-
self.sources = sources
20+
self._sources = {}
1921

2022
def __getitem__(self, sku):
21-
return self.sources[sku]
23+
return self._sources[sku]
2224

2325
def __contains__(self, sku):
24-
return sku in self.sources
26+
return sku in self._sources
2527

2628
def supplement_with(self, other):
27-
for sku, qty in other.sources.items():
29+
for sku, qty in other._sources.items():
2830
if sku not in self:
29-
self.sources[sku] = qty
31+
self._sources[sku] = qty
3032

3133
def decrement_available_quantities(self):
32-
for sku, source in self.sources.items():
34+
for sku, source in self._sources.items():
3335
source.decrement_available(sku, self.order[sku])
3436

3537

@@ -41,20 +43,20 @@ class Line:
4143

4244
class _SkuLines:
4345

44-
def __init__(self, linesdict):
45-
self.linesdict = linesdict
46+
def __init__(self, lines: dict):
47+
self._lines = lines
4648

4749
def __getitem__(self, sku):
48-
return self.linesdict[sku]
50+
return self._lines[sku]
4951

5052
def __contains__(self, sku):
51-
return sku in self.linesdict
53+
return sku in self._lines
5254

5355
@property
5456
def lines(self):
5557
return [
5658
Line(sku, qty)
57-
for sku, qty in self.linesdict.items()
59+
for sku, qty in self._lines.items()
5860
]
5961

6062

@@ -65,15 +67,17 @@ class Order(_SkuLines):
6567
class _Stock(_SkuLines):
6668

6769
def decrement_available(self, sku, qty):
68-
self.linesdict[sku] -= qty
70+
self._lines[sku] -= qty
6971

70-
def allocation_for(self, order):
71-
return Allocation(order, {
72+
def allocation_for(self, order: Order):
73+
allocation = Allocation(order)
74+
allocation._sources = { # TODO: this ain't right
7275
line.sku: self
7376
for line in order.lines
7477
if line.sku in self
7578
and self[line.sku] > line.qty
76-
})
79+
}
80+
return allocation
7781

7882

7983
class Warehouse(_Stock):
@@ -87,9 +91,9 @@ class Shipment(_Stock):
8791
def __repr__(self):
8892
return f'<Shipment {super().__repr__()}>'
8993

90-
def __init__(self, d, eta):
94+
def __init__(self, lines: dict, eta: date):
9195
self.eta = eta
92-
super().__init__(d)
96+
super().__init__(lines)
9397

9498
def __lt__(self, other):
9599
return self.eta < other.eta

0 commit comments

Comments
 (0)