Skip to content

deallocate and fix issues#114

Open
uguryiilmz wants to merge 3 commits intocosmicpython:chapter_04_service_layer_exercisefrom
uguryiilmz:deallocate
Open

deallocate and fix issues#114
uguryiilmz wants to merge 3 commits intocosmicpython:chapter_04_service_layer_exercisefrom
uguryiilmz:deallocate

Conversation

@uguryiilmz
Copy link
Copy Markdown

I couldn't find the solution to the exercise for deallocation, so I wanted to share my own. Couple realization I had:

  • there are typos in certain places, instead of returning batchref, it returns batch id. They're fixed in the code.
  • sqlalchemy version is not pinned, so it'll try to install the latest one which is not compatible with the source code.(updated the requirements.txt)
  • sku number is really not used anywhere logically for the deallocation method, as we cannot allocate anything to a batch if orderline and batch id doesn't match. we also know that orderline needs to unique, so data modeling doesn't really give any room to use sku for deallocation, I was not sure why it was included in the exercises to be used, maybe an oversight? I still used it as a placeholder but has no functional importance

Comment thread test_services.py
Comment on lines +23 to +26
for batch in self._batches:
for line in batch._allocations:
if line.orderid == order_id and line.sku == sku:
return line
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't resist suggesting this formulation with next() and a nested list comp. lol

return next(
    (l for b in self._batches for l in b._allocations if l.orderid == order_id and l.sku == sku), 
    None,
)

@hjwp
Copy link
Copy Markdown
Contributor

hjwp commented Apr 16, 2026

Hey, looks good! great job. hope you found the exercise interesting? i think you may be right about the sku...

@uguryiilmz
Copy link
Copy Markdown
Author

@hjwp Thanks, Harry. I found the book extremely helpful. This is my second time reading it, and I decided to buy a copy because I'll always carry this around. Especially in the age of AI, architecture and maintenance will be more crucial than ever. Grateful for all the hard-earned experience and wisdom that you shared in the book.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants