-
Notifications
You must be signed in to change notification settings - Fork 725
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
Solution #675
base: master
Are you sure you want to change the base?
Solution #675
Conversation
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.
Nice, I especially approve of the use of factory methods.
IMHO, main.py does too much work, most of the calculations could be moved to the classes. Consider it)
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.
Thank you for your hard work on this project! While your code meets many of the task requirements, there are several critical issues related to error handling that need to be addressed before it can be approved. These issues could lead to runtime errors if the expected data is not present, which impacts the core functionality of your application. Specifically, the code assumes the presence of certain keys in dictionaries and JSON data without checking for their existence, which could result in KeyError
exceptions. Adding error handling for these cases will make your code more robust and reliable. Once these changes are made, your solution will be much stronger. Keep up the great work, and I look forward to seeing the improvements! 😊
✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨
🔄 Fix the issues, commit the changes, and then re-request my review using the Re-request review
button in the Reviewers tab.
app/car.py
Outdated
def create_car(cls, car_info: dict) -> Car: | ||
return cls(car_info["brand"], car_info["fuel_consumption"]) |
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.
The create_car
method assumes that the dictionary car_info
will always contain the keys 'brand' and 'fuel_consumption'. It would be safer to add error handling to manage cases where these keys might be missing, such as using car_info.get('brand')
with a default value or raising an informative exception.
app/customer.py
Outdated
def create_customer(cls, customer_info: dict) -> Customer: | ||
return cls( | ||
customer_info["name"], | ||
customer_info["product_cart"], | ||
customer_info["location"], | ||
customer_info["money"], | ||
Car.create_car(customer_info["car"]), | ||
) |
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.
The create_customer
method assumes that the dictionary customer_info
will always contain the keys 'name', 'product_cart', 'location', 'money', and 'car'. It would be safer to add error handling to manage cases where these keys might be missing, such as using customer_info.get('name')
with a default value or raising an informative exception.
app/main.py
Outdated
data_from_file = json.load(file) | ||
fuel_price = data_from_file["FUEL_PRICE"] |
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.
The code assumes that the JSON file contains the key 'FUEL_PRICE'. It would be safer to add error handling to manage cases where this key might be missing, such as using data_from_file.get('FUEL_PRICE')
with a default value or raising an informative exception.
app/main.py
Outdated
customers_list = [ | ||
Customer.create_customer(customer) | ||
for customer in data_from_file["customers"] | ||
] |
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.
The code assumes that the JSON file contains a list under the key 'customers'. Consider adding error handling to manage cases where this key might be missing or the data is not in the expected format.
app/main.py
Outdated
shops_list = [ | ||
Shop.create_shop(shop) for shop in data_from_file["shops"] | ||
] |
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.
The code assumes that the JSON file contains a list under the key 'shops'. Consider adding error handling to manage cases where this key might be missing or the data is not in the expected format.
price = quantity * min_cost_shop[0].products[product] | ||
formate_price = f"{int(price)}" \ | ||
if price == int(price) else f"{price}" |
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.
The calculation of formate_price
could be simplified by using Python's string formatting capabilities, such as f'{price:.2f}'
to ensure two decimal places, which would handle both integer and float cases more elegantly.
app/shop.py
Outdated
def create_shop(cls, shop_info: dict) -> Shop: | ||
return cls( | ||
shop_info["name"], | ||
shop_info["location"], | ||
shop_info["products"] | ||
) |
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.
The create_shop
method assumes that the dictionary shop_info
will always contain the keys 'name', 'location', and 'products'. It would be safer to add error handling to manage cases where these keys might be missing, such as using shop_info.get('name')
with a default value or raising an informative exception.
for product in self._products: | ||
products_total_cost += (self._products[product] | ||
* customer.products[product]) |
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.
The calculate_total_product_cost
method assumes that the customer always wants to buy products available in the shop. Consider adding a check to ensure that the product exists in the customer's product list before attempting to access it, to avoid potential KeyError
exceptions.
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.
Sorry, the pull request review was unsuccessful for the following reasons:
Warning
Something went wrong while processing the review.
[CODE: 3]
If the issue is within your control, please address it.
The next review attempt will automatically launch in 1 hour.
If you believe this is an error or need assistance, please contact the support team.
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.
Sorry, the pull request review was unsuccessful for the following reasons:
Warning
Something went wrong while processing the review.
[CODE: 3]
If the issue is within your control, please address it.
The next review attempt will automatically launch in 1 hour.
If you believe this is an error or need assistance, please contact the support team.
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.
Sorry, the pull request review was unsuccessful for the following reasons:
Warning
Something went wrong while processing the review.
[CODE: 3]
If the issue is within your control, please address it.
The next review attempt will automatically launch in 1 hour.
If you believe this is an error or need assistance, please contact the support team.
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.
Great job on implementing the code structure and functionality as per the requirements! 🎉 While the code meets most of the task requirements, there are a few areas that need improvement to enhance robustness and clarity:
-
Error Handling: The error messages for missing keys in the JSON data and other parts of the code are not very informative. Providing more context, such as specifying which key is missing, will make debugging easier.
-
Runtime Errors: In the
calculate_total_product_cost
method, ensure that products in the customer's cart exist in the shop's inventory to avoidKeyError
. Similarly, check the lengths ofhome_location
andshop_location
lists in thecalculate_cost_one_way
method to preventIndexError
. -
Type Consistency: Align the type hint for the
money
property in theCustomer
class with the constructor to avoid potential type inconsistencies. -
Logical Conditions: Consider using
<=
instead of<
when checking if a customer can afford a trip to ensure they can proceed even if their money is exactly equal to the cost.
These improvements will enhance the code's robustness and user-friendliness. Keep up the good work, and make sure to address these issues in future iterations to avoid similar problems. Your effort and dedication to learning are commendable! 🚀
✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨
return cls(car_info["brand"], car_info["fuel_consumption"]) | ||
except KeyError as e: | ||
print(e, "not exist") |
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.
The error handling in the create_car
method catches KeyError
, but the error message 'not exist' is not very informative. Consider providing more context in the error message, such as which key is missing, to make debugging easier.
self, fuel_price: float, home_location: list, shop_location: list | ||
) -> float: | ||
distance = sqrt( | ||
(shop_location[0] - home_location[0]) ** 2 | ||
+ (shop_location[1] - home_location[1]) ** 2 |
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.
The calculate_cost_one_way
method assumes that home_location
and shop_location
are lists with at least two elements. If these lists are shorter, an IndexError
will occur. Consider adding checks to ensure these lists have the expected length before accessing their elements.
except KeyError as e: | ||
print(e, "not exist") |
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.
Similar to the Car
class, the error handling in the create_customer
method catches KeyError
, but the error message 'not exist' is not very informative. Consider providing more context in the error message, such as which key is missing, to make debugging easier.
def money(self) -> int | float: | ||
return self._money | ||
|
||
@money.setter | ||
def money(self, money: int | float) -> None: |
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.
The type hint for the money
property is int | float
, but the constructor only accepts int
. Consider aligning the type hint in the constructor with the property to avoid potential type inconsistencies.
except KeyError as e: | ||
print(e, "not exist") |
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.
The error message 'not exist' for missing keys in the JSON data is not very informative. Consider providing more context in the error message, such as which key is missing, to make debugging easier.
except KeyError as e: | ||
print(e, "not exist") |
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.
Similar to the previous comment, the error message 'not exist' for missing keys in the JSON data is not very informative. Consider providing more context in the error message, such as which key is missing, to make debugging easier.
except KeyError as e: | ||
print(e, "not exist") |
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.
Again, the error message 'not exist' for missing keys in the JSON data is not very informative. Consider providing more context in the error message, such as which key is missing, to make debugging easier.
if min_cost_shop[1] < customer.money: | ||
print(f"{customer.name} rides to {min_cost_shop[0].name}\n") | ||
print("Date: 04/01/2021 12:33:41") | ||
print(f"Thanks, {customer.name}, for your purchase!") | ||
print("You have bought:") | ||
for product, quantity in customer.products.items(): | ||
price = quantity * min_cost_shop[0].products[product] | ||
formate_price = f"{int(price)}" \ | ||
if price == int(price) else f"{price}" | ||
print(f"{quantity} {product + 's'} " | ||
f"for {formate_price} dollars") | ||
print( | ||
f"Total cost is " | ||
f"{min_cost_shop[0].calculate_total_product_cost(customer)} " | ||
f"dollars" | ||
) | ||
print("See you again!\n") | ||
print(f"{customer.name} rides home") | ||
customer.money -= min_cost_shop[1] | ||
print(f"{customer.name} now has {customer.money} dollars\n") |
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.
The code assumes that the customer can afford the trip if min_cost_shop[1] < customer.money
. However, this does not account for the case where the customer's money is exactly equal to the cost. Consider using <=
to ensure the customer can afford the trip even if their money is exactly equal to the cost.
except KeyError as e: | ||
print(e, "not exist") |
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.
The error handling in the create_shop
method catches KeyError
, but the error message 'not exist' is not very informative. Consider providing more context in the error message, such as which key is missing, to make debugging easier.
for product in self._products: | ||
products_total_cost += (self._products[product] | ||
* customer.products[product]) |
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.
The calculate_total_product_cost
method assumes that all products in the customer's cart are available in the shop. If a product is not available, this will raise a KeyError
. Consider adding a check to ensure the product exists in the shop's inventory before attempting to access its price.
No description provided.