Skip to content

Commit 082436f

Browse files
committed
black chapter 7
1 parent 5e98fb1 commit 082436f

File tree

2 files changed

+27
-29
lines changed

2 files changed

+27
-29
lines changed

chapter_07_aggregate.asciidoc

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -358,20 +358,17 @@ Let's see how that looks in code form:
358358
[role="non-head"]
359359
----
360360
class Product:
361-
362361
def __init__(self, sku: str, batches: List[Batch]):
363362
self.sku = sku #<1>
364363
self.batches = batches #<2>
365364
366365
def allocate(self, line: OrderLine) -> str: #<3>
367366
try:
368-
batch = next(
369-
b for b in sorted(self.batches) if b.can_allocate(line)
370-
)
367+
batch = next(b for b in sorted(self.batches) if b.can_allocate(line))
371368
batch.allocate(line)
372369
return batch.reference
373370
except StopIteration:
374-
raise OutOfStock(f'Out of stock for sku {line.sku}')
371+
raise OutOfStock(f"Out of stock for sku {line.sku}")
375372
----
376373
====
377374

@@ -499,8 +496,8 @@ layer to see how it looks with `Product` as its main entrypoint:
499496
[source,python]
500497
----
501498
def add_batch(
502-
ref: str, sku: str, qty: int, eta: Optional[date],
503-
uow: unit_of_work.AbstractUnitOfWork
499+
ref: str, sku: str, qty: int, eta: Optional[date],
500+
uow: unit_of_work.AbstractUnitOfWork,
504501
):
505502
with uow:
506503
product = uow.products.get(sku=sku)
@@ -512,14 +509,14 @@ def add_batch(
512509
513510
514511
def allocate(
515-
orderid: str, sku: str, qty: int,
516-
uow: unit_of_work.AbstractUnitOfWork
512+
orderid: str, sku: str, qty: int,
513+
uow: unit_of_work.AbstractUnitOfWork,
517514
) -> str:
518515
line = OrderLine(orderid, sku, qty)
519516
with uow:
520517
product = uow.products.get(sku=line.sku)
521518
if product is None:
522-
raise InvalidSku(f'Invalid sku {line.sku}')
519+
raise InvalidSku(f"Invalid sku {line.sku}")
523520
batchref = product.allocate(line)
524521
uow.commit()
525522
return batchref
@@ -743,22 +740,19 @@ you might decide the cleanest trade-off is to put them in the domain:
743740
[source,python]
744741
----
745742
class Product:
746-
747743
def __init__(self, sku: str, batches: List[Batch], version_number: int = 0): #<1>
748744
self.sku = sku
749745
self.batches = batches
750746
self.version_number = version_number #<1>
751747
752748
def allocate(self, line: OrderLine) -> str:
753749
try:
754-
batch = next(
755-
b for b in sorted(self.batches) if b.can_allocate(line)
756-
)
750+
batch = next(b for b in sorted(self.batches) if b.can_allocate(line))
757751
batch.allocate(line)
758752
self.version_number += 1 #<1>
759753
return batch.reference
760754
except StopIteration:
761-
raise OutOfStock(f'Out of stock for sku {line.sku}')
755+
raise OutOfStock(f"Out of stock for sku {line.sku}")
762756
----
763757
====
764758

@@ -847,18 +841,18 @@ def test_concurrent_updates_to_version_are_not_allowed(postgres_session_factory)
847841
)
848842
assert version == 2 #<2>
849843
[exception] = exceptions
850-
assert 'could not serialize access due to concurrent update' in str(exception) #<3>
844+
assert "could not serialize access due to concurrent update" in str(exception) #<3>
851845
852-
orders = list(session.execute(
846+
orders = session.execute(
853847
"SELECT orderid FROM allocations"
854848
" JOIN batches ON allocations.batch_id = batches.id"
855849
" JOIN order_lines ON allocations.orderline_id = order_lines.id"
856850
" WHERE order_lines.sku=:sku",
857851
dict(sku=sku),
858-
))
859-
assert len(orders) == 1 #<4>
852+
)
853+
assert orders.rowcount == 1 #<4>
860854
with unit_of_work.SqlAlchemyUnitOfWork() as uow:
861-
uow.session.execute('select 1')
855+
uow.session.execute("select 1")
862856
----
863857
====
864858

@@ -885,10 +879,12 @@ on our session:
885879
====
886880
[source,python]
887881
----
888-
DEFAULT_SESSION_FACTORY = sessionmaker(bind=create_engine(
889-
config.get_postgres_uri(),
890-
isolation_level="REPEATABLE READ",
891-
))
882+
DEFAULT_SESSION_FACTORY = sessionmaker(
883+
bind=create_engine(
884+
config.get_postgres_uri(),
885+
isolation_level="REPEATABLE READ",
886+
)
887+
)
892888
----
893889
====
894890

@@ -927,10 +923,12 @@ query time:
927923
[role="non-head"]
928924
----
929925
def get(self, sku):
930-
return self.session.query(model.Product) \
931-
.filter_by(sku=sku) \
932-
.with_for_update() \
933-
.first()
926+
return (
927+
self.session.query(model.Product)
928+
.filter_by(sku=sku)
929+
.with_for_update()
930+
.first()
931+
)
934932
----
935933
====
936934

0 commit comments

Comments
 (0)