-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathmodel.py
More file actions
68 lines (51 loc) · 1.8 KB
/
model.py
File metadata and controls
68 lines (51 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from __future__ import annotations
from dataclasses import dataclass, field, replace
from datetime import date
from typing import Any, Optional, Iterable, List, Set
class OutOfStock(Exception):
pass
def allocate(line: OrderLine, batches: List[Batch]) -> Optional[Batch]:
for b in sorted(batches):
if b.can_allocate(line):
return b.allocate(line)
raise OutOfStock(f"Out of stock for sku {line.sku}")
@dataclass(frozen=True)
class OrderLine:
orderid: str
sku: str
qty: int
@dataclass(frozen=True)
class Batch:
ref: str
sku: str
_purchased_quantity: int
eta: Optional[date]
_allocations: Set[OrderLine] = field(default_factory=set)
def __repr__(self) -> str:
return f"<Batch {self.ref}>"
def __eq__(self, other: Any) -> bool:
if not isinstance(other, Batch):
return False
return other.ref == self.ref
def __hash__(self) -> int:
return hash(self.ref)
def __gt__(self, other: Batch) -> bool:
if self.eta is None:
return False
if other.eta is None:
return True
return self.eta > other.eta
def allocate(self, line: OrderLine) -> Optional[Batch]:
if self.can_allocate(line):
return replace(self, _allocations=self._allocations | {line})
return None
def deallocate(self, line: OrderLine) -> Batch:
return replace(self, _allocations=self._allocations - {line})
@property
def allocated_quantity(self) -> int:
return sum(line.qty for line in self._allocations)
@property
def available_quantity(self) -> int:
return self._purchased_quantity - self.allocated_quantity
def can_allocate(self, line: OrderLine) -> bool:
return self.sku == line.sku and self.available_quantity >= line.qty