Skip to content

Commit 609d5a2

Browse files
committed
Fix code issue 4, add explanation re nested tuple unpacking
1 parent b98b29b commit 609d5a2

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

chapter_06_uow.asciidoc

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,11 @@ def insert_batch(session, ref, sku, qty, eta):
169169
170170
171171
def get_allocated_batch_ref(session, orderid, sku):
172-
[[orderlineid]] = session.execute(
172+
[[orderlineid]] = session.execute( #<1>
173173
"SELECT id FROM order_lines WHERE orderid=:orderid AND sku=:sku",
174174
dict(orderid=orderid, sku=sku),
175175
)
176-
[[batchref]] = session.execute(
176+
[[batchref]] = session.execute( #<1>
177177
"SELECT b.reference FROM allocations JOIN batches AS b ON batch_id = b.id"
178178
" WHERE orderline_id=:orderlineid",
179179
dict(orderlineid=orderlineid),
@@ -182,7 +182,15 @@ def get_allocated_batch_ref(session, orderid, sku):
182182
----
183183
====
184184

185-
// TODO: that double-unpacking is freaking ppl out. maybe [(orderlineid, )] ?
185+
<1> The `[[orderlineid]] =` syntax is a little too-clever-by-half, apologies.
186+
What's happening is that `session.execute` returns a list of rows,
187+
where each row is a tuple of column values;
188+
in our specific case, it's a list of one row,
189+
which is a tuple with one column value in.
190+
The double-square-bracket on the left hand side
191+
is doing (double) assignment-unpacking to get the single value
192+
back out of these two nested sequences.
193+
It becomes readable once you've used it a few times!
186194

187195

188196
=== Unit of Work and Its Context Manager

code

0 commit comments

Comments
 (0)