b for b in sorted(batches) if b.can_allocate(line)
)
batch.allocate(line)
return batch.reference
1.4.1
파이썬 마법 메서드 사용 시 모델과 파이썬 숙어 함께 사용 가능
이전 코드에서
next
()
를 사용하는 것을 좋아하는 독자도 있고, 싫어하는 독자도 있을 것이다.
하지만 배치 리스트에 대해
sorted
()
를 사용할 수 있다는 점이 멋지다는 데는 대부분 동의할
것이다.
sorted
()
가 작동하게 하려면
__
gt
__
를 도메인 모델이 구현해야 한다.
마법 메서드로 도메인의 의미 표현 가능(
model
.
py
)
class Batch:
...
def __gt__(self, other):
if self.eta is None:
return False
if other.eta is None:
return True
return self.eta > other.eta
멋지다.
1.4.2
예외를 사용해 도메인 개념 표현 가능
마지막으로 다룰 개념이 있다. 예외로 도메인 개념을 표현하는 것이다. 도메인 전문가와 대화
하는 과정에서
품절
out
of
stock
로 주문을 할당할 수 없는 경우도 있다고 배웠다. 이런 개념을
도메
인 예외
domain
exception
를 사용해 찾아낼 수
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.