Repository navigation
2. Шемякин Николай Александрович. 613 #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
195db9f
constants.py
shiba-rose 28b486f
task completed
shiba-rose 35d3caf
snowflake testing has been completed
shiba-rose 91ea672
Merge branch 'python-department:main' into main
shiba-rose 02ce39c
изменение дз номер 1
shiba-rose d8eef57
task2
shiba-rose e97c37d
Merge branch 'python-department:main' into main
shiba-rose e6e8238
Solve task 3
shiba-rose 46eaf4d
test
shiba-rose c522d75
no_test
shiba-rose e68b234
Merge branch 'python-department:main' into main
shiba-rose d10c40d
homework
shiba-rose File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ dependencies = [] | |
| [dependency-groups] | ||
| dev = [ | ||
| "mypy==2.3.1", | ||
| "pytest>=9.1.1", | ||
| "ruff==0.16.8", | ||
| ] | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,9 +22,18 @@ | |
|
|
||
| type CartLine = tuple[int, int] | ||
|
|
||
| # TODO: задайте позиции полей внутри кортежа CartLine | ||
| LINE_PRODUCT_ID_INDEX: Final = 0 | ||
| LINE_QUANTITY_INDEX: Final = 0 | ||
| LINE_QUANTITY_INDEX: Final = 1 | ||
|
|
||
|
|
||
| def find_cart_line( | ||
| cart: list[CartLine], | ||
| product_id: int, | ||
| ) -> CartLine | None: | ||
| for line in cart: | ||
| if line[LINE_PRODUCT_ID_INDEX] == product_id: | ||
| return line | ||
| return None | ||
|
|
||
|
|
||
| def add_to_cart( | ||
|
|
@@ -33,28 +42,24 @@ def add_to_cart( | |
| product_id: int, | ||
| quantity: int, | ||
| ) -> CartLine | 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.part2.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 line for ``product_id`` gains | ||
| ``quantity`` units -- a new line is created when the cart had none. | ||
|
|
||
| Args: | ||
| storage: The product store to draw stock from; modified in place on | ||
| success. | ||
| 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 cart line for ``product_id`` after the addition, or ``None`` | ||
| when the product is unknown or the store cannot cover the request. | ||
| """ | ||
| # TODO: реализуйте функцию | ||
| return (0, 0) | ||
| product = read_product(storage, product_id) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Лучше не экономить строки и оставлять пробелы (пустые строки) между логическими блоками. Так читабельность кода улучшается. |
||
| if product is None: | ||
| return None | ||
| if product[QUANTITY_INDEX] < quantity: | ||
| return print( | ||
| f"not enough stock for product {product_id}: {product[QUANTITY_INDEX]} available, {quantity} requested" | ||
| ) | ||
| id, name, price, stock_quantity = product | ||
| updated_product = (id, name, price, stock_quantity - quantity) | ||
| storage[storage.index(product)] = updated_product | ||
| for line in cart: | ||
| if line[LINE_PRODUCT_ID_INDEX] == product_id: | ||
| updated_line = (product_id, line[LINE_QUANTITY_INDEX] + quantity) | ||
| cart[cart.index(line)] = updated_line | ||
| return updated_line | ||
| new_line = (product_id, quantity) | ||
| cart.append(new_line) | ||
| return new_line | ||
|
|
||
|
|
||
| def remove_from_cart( | ||
|
|
@@ -63,27 +68,23 @@ def remove_from_cart( | |
| product_id: int, | ||
| quantity: int, | ||
| ) -> CartLine | None: | ||
| """Move ``quantity`` units of ``product_id`` from ``cart`` back to the store. | ||
|
|
||
| The cart line for ``product_id`` is looked up. If there is none, a | ||
| message is printed and nothing changes. If the line holds fewer units | ||
| than requested, an explanatory message is printed and nothing changes. | ||
| Otherwise the cart line loses ``quantity`` units -- the line is dropped | ||
| when it reaches zero -- and the store record gains ``quantity`` units | ||
| back. | ||
|
|
||
| Args: | ||
| storage: The product store to return stock to; modified in place on | ||
| success. | ||
| 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 cart line for ``product_id`` after the removal (a quantity of | ||
| zero means the line was dropped), or ``None`` when the cart has no | ||
| line for the product or holds too few units. | ||
| """ | ||
| # TODO: реализуйте функцию | ||
| return (0, 0) | ||
| line = find_cart_line(cart, product_id) | ||
| if line == None: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Пункт Т3 |
||
| return print(f"product {product_id} is not in the cart") | ||
| if line[LINE_QUANTITY_INDEX] < quantity: | ||
| return print( | ||
| f"cart holds only {line[LINE_QUANTITY_INDEX]} unit(s) of product {product_id}, cannot remove {quantity}" | ||
| ) | ||
| product = read_product(storage, product_id) | ||
| if product is None: | ||
| return None | ||
| id, name, price, storage_quantity = product | ||
| updated_product = (id, name, price, storage_quantity + quantity) | ||
| storage[storage.index(product)] = updated_product | ||
| id, cart_quantity = line | ||
| updated_line = (id, cart_quantity - quantity) | ||
| if cart_quantity - quantity == 0: | ||
| cart.remove(line) | ||
| else: | ||
| cart[cart.index(line)] = updated_line | ||
| return updated_line | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Вычисление сдвига дублировано в функции
decode_timestamp_ms. Это может быть опасно, так как если логика поменяется, то надо будет исправлять в 2х местах, а это можно забыть, поэтому лучше было вынести вычисление в отдельную константу.Пункт S1, минус балл.