Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 32 additions & 56 deletions src/part5/cart.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
an explanatory message to stdout and returns ``None``.
"""

from .storage import Product
from .storage import QUANTITY_KEY, Product


# TODO: при необходимости добавьте свои вспомогательные функции
Expand All @@ -22,33 +22,19 @@ def add_to_cart(
product_id: int,
quantity: int,
) -> int | None:
"""Move ``quantity`` units of ``product_id`` from the store into ``cart``.

The product is looked up in ``storage``. If it is missing, a message is
printed (by :func:`~src.part5.crud.read_product`) and nothing
changes. If the store holds fewer units than requested, an explanatory
message is printed and nothing changes. Otherwise the store record is
decremented by ``quantity`` and the cart gains ``quantity`` units of
``product_id`` -- the product is added when the cart had none of it.

Args:
storage: The product store to draw stock from; modified in place on
success.
product_names: The normalised names in ``storage``; threaded
through to :func:`~src.part5.crud.update_product`, which
keeps it in sync (a quantity-only update never renames a
product, so it is left unchanged here).
cart: The cart to add the units to; modified in place on success.
product_id: The identifier of the product to add.
quantity: Number of units to move into the cart.

Returns:
The quantity of ``product_id`` in the cart after the addition, or
``None`` when the product is unknown or the store cannot cover the
request.
"""
# TODO: реализуйте функцию (используйте своё решение части 4)
return 0
if product_id not in storage:
print(f"no product with id {product_id}")
return None
if storage[product_id][QUANTITY_KEY] < quantity:
print(
f"not enough stock for product {product_id}: "
f"{storage[product_id][QUANTITY_KEY]} available, "
f"{quantity} requested"
)
return None
storage[product_id][QUANTITY_KEY] -= quantity
cart[product_id] = cart.get(product_id, 0) + quantity
return cart[product_id]


def remove_from_cart(
Expand All @@ -58,31 +44,21 @@ def remove_from_cart(
product_id: int,
quantity: int,
) -> int | None:
"""Move ``quantity`` units of ``product_id`` from ``cart`` back to the store.

If the cart holds no units of ``product_id``, a message is printed and
nothing changes. If it holds fewer units than requested, an explanatory
message is printed and nothing changes. Otherwise the cart loses
``quantity`` units of the product -- the product is dropped from the
cart when its quantity reaches zero -- and the store record gains
``quantity`` units back.

Args:
storage: The product store to return stock to; modified in place on
success.
product_names: The normalised names in ``storage``; threaded
through to :func:`~src.part5.crud.update_product`, which
keeps it in sync (a quantity-only update never renames a
product, so it is left unchanged here).
cart: The cart to take the units from; modified in place on
success.
product_id: The identifier of the product to remove.
quantity: Number of units to move back into the store.

Returns:
The quantity of ``product_id`` left in the cart after the removal
(zero means the product was dropped from the cart), or ``None`` when
the cart does not hold the product or holds too few units.
"""
# TODO: реализуйте функцию (используйте своё решение части 4)
return 0
if product_id not in cart:
print(f"product {product_id} is not in the cart")
return None
if cart[product_id] < quantity:
print(
f"cart holds only {cart[product_id]} unit(s) "
f"of product {product_id}, cannot remove {quantity}"
)
return None
if product_id not in storage:
print(f"no product with id {product_id}")
return None
storage[product_id][QUANTITY_KEY] += quantity
cart[product_id] -= quantity
if not cart[product_id]:
del cart[product_id]
return 0
return cart[product_id]
21 changes: 11 additions & 10 deletions src/part5/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,24 @@
from typing import Final


# TODO: замените заглушки (0) на корректные значения (используйте своё решение
# части 1).
# TODO: замените заглушки (0) на корректные значения, см. TASK.md.
# Все константы, зависящие от ёмкостей *_BITS, должны вычисляться из них.

# Twitter's original Snowflake epoch: 2010-11-04 01:42:54.657 UTC.
EPOCH_MS_DEFAULT: Final[int] = 0
EPOCH_MS_DEFAULT: Final[int] = 1288834974657

# Node identifier used when the caller does not supply one.
NODE_ID_DEFAULT: Final[int] = 0
NODE_ID_DEFAULT: Final[int] = 1

# Width of each field, in bits.
TIMESTAMP_BITS: Final[int] = 0
NODE_ID_BITS: Final[int] = 0
SEQUENCE_ID_BITS: Final[int] = 0
TIMESTAMP_BITS: Final[int] = 41
NODE_ID_BITS: Final[int] = 10
SEQUENCE_ID_BITS: Final[int] = 12

# Largest value each field can hold.
TIMESTAMP_MS_MAX: Final[int] = 0
NODE_ID_MAX: Final[int] = 0
SEQUENCE_ID_MAX: Final[int] = 0
TIMESTAMP_MS_MAX: Final[int] = 2**TIMESTAMP_BITS - 1
NODE_ID_MAX: Final[int] = 2**NODE_ID_BITS - 1
SEQUENCE_ID_MAX: Final[int] = 2**SEQUENCE_ID_BITS - 1

# Здесь можно добавить собственные вспомогательные константы
# (например, сдвиги полей при сборке идентификатора).
Loading
Loading