Repository navigation
2. Рустаев Самир Рустамович. 614 #92
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
Open
samirhappy
wants to merge
4
commits into
python-department:main
Choose a base branch
from
samirhappy:part2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
|
|
@@ -14,13 +14,13 @@ | |
|
|
||
| from decimal import Decimal | ||
|
|
||
| from .storage import ( # noqa: F401 | ||
| from .storage import ( | ||
| NAME_INDEX, | ||
| PRODUCT_ID_INDEX, | ||
| PRODUCT_ID_MIN, | ||
| Product, | ||
| ) | ||
| from .utils import normalize_price # noqa: F401 | ||
| from .utils import normalize_price | ||
|
|
||
|
|
||
| def generate_product_id(storage: list[Product]) -> int: | ||
|
|
@@ -34,8 +34,14 @@ def generate_product_id(storage: list[Product]) -> int: | |
| :data:`~src.part2.storage.PRODUCT_ID_MIN` when ``storage`` is | ||
| empty. | ||
| """ | ||
| # TODO: реализуйте функцию | ||
| return 0 | ||
| if not storage: | ||
| return PRODUCT_ID_MIN | ||
|
|
||
| max_id = 0 | ||
| for product in storage: | ||
| current_id = int(product[PRODUCT_ID_INDEX]) | ||
| max_id = max(current_id, max_id) | ||
| return max_id + 1 | ||
|
|
||
|
|
||
| def create_product( | ||
|
|
@@ -56,24 +62,34 @@ def create_product( | |
| ``storage`` is left unchanged and a message naming the clashing | ||
| name is printed. | ||
| """ | ||
| # TODO: реализуйте функцию | ||
| return 0 | ||
| name, price, quantity = fields | ||
| for product in storage: | ||
| if product[NAME_INDEX] == name: | ||
| print(f"Product name '{name}' is already taken") | ||
| return None | ||
|
|
||
| new_id = generate_product_id(storage) | ||
| normalized_price = normalize_price(price) | ||
| storage.append((new_id, name, normalized_price, quantity)) | ||
| return new_id | ||
|
|
||
|
|
||
| def read_product(storage: list[Product], product_id: int) -> Product | None: | ||
| """Return the product stored under ``product_id``. | ||
|
|
||
| Args: | ||
| storage: The product store to search. | ||
| product_id: The identifier to look up. | ||
|
|
||
| Returns: | ||
| The matching ``(product_id, name, price, quantity)`` record, or | ||
| ``None`` when no product carries that identifier (a message is | ||
| printed in that case). | ||
| """ | ||
| # TODO: реализуйте функцию | ||
| return (0, "", Decimal(0), 0) | ||
| for product in storage: | ||
| if product[PRODUCT_ID_INDEX] == product_id: | ||
| return product | ||
| print(f"No product with id {product_id}") | ||
| return None | ||
|
|
||
|
|
||
| def update_product( | ||
|
|
@@ -99,8 +115,15 @@ def update_product( | |
| ``None`` when no product carries that identifier (``storage`` is | ||
| left unchanged and a message is printed). | ||
| """ | ||
| # TODO: реализуйте функцию | ||
| return (0, "", Decimal(0), 0) | ||
| name, price, quantity = fields | ||
| normalized_price = normalize_price(price) | ||
| for i in range(len(storage)): | ||
|
Member
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 storage[i][PRODUCT_ID_INDEX] == product_id: | ||
| updated_product = (product_id, name, normalized_price, quantity) | ||
| storage[i] = updated_product | ||
| return updated_product | ||
| print(f"No product with id {product_id}") | ||
| return None | ||
|
|
||
|
|
||
| def delete_product(storage: list[Product], product_id: int) -> int | None: | ||
|
|
@@ -116,5 +139,9 @@ def delete_product(storage: list[Product], product_id: int) -> int | None: | |
| product carried that identifier (``storage`` is left unchanged and | ||
| a message is printed). | ||
| """ | ||
| # TODO: реализуйте функцию | ||
| return 0 | ||
| for i in range(len(storage)): | ||
| if storage[i][PRODUCT_ID_INDEX] == product_id: | ||
| del storage[i] | ||
| return product_id | ||
| print(f"No product with id {product_id}") | ||
| return None | ||
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
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.
Очень много операций помещено в этот цикл, хотя большая часть могла быть описана вне цикла. Это бы упростило чтение кода