-
Notifications
You must be signed in to change notification settings - Fork 696
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 #610
base: master
Are you sure you want to change the base?
Solution #610
Conversation
app/main.py
Outdated
|
||
def shop_trip() -> None: | ||
|
||
(fuel_price, customers, shops) = get_data_from_file() |
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.
You can remove parentheses here
app/misc.py
Outdated
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.
What is misc? Consider more descriptive name, to be honest, I don't understand now what it is!
app/misc.py
Outdated
def get_data_from_file() -> tuple: | ||
with open(base_dir / file_name, "r") as file: | ||
file_data = json.load(file) | ||
return ( | ||
file_data["FUEL_PRICE"], | ||
file_data["customers"], | ||
file_data["shops"] | ||
) | ||
|
||
|
||
def calculate_distance(p1: list, p2: list) -> float: | ||
p1 = Point(*p1) | ||
p2 = Point(*p2) | ||
|
||
return math.sqrt((p2.px - p1.px) ** 2 + (p2.py - p1.py) ** 2) | ||
|
||
|
||
def calculate_fuel_spent( | ||
fuel_price: float, | ||
car: dict, | ||
distance: float | ||
) -> float: | ||
return car["fuel_consumption"] / 100 * distance * fuel_price | ||
|
||
|
||
def calculate_product_cost(cart: dict, products: dict) -> int | float: |
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.
Create a classes and put all related functions in them
app/misc.py
Outdated
# left str date for tests, otherwise: | ||
# datetime.strftime(datetime.now(), "%d/%m/%Y %H:%M:%S") | ||
print("Date: 04/01/2021 12:33:41") |
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.
try to import from datetime import datetime
or import datetime
-- some of this version must work with tests, I don't remember which one
app/misc.py
Outdated
px: int | ||
py: int |
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.
Why you decided to make it px, py and not just x, y? Looks like just x, y will be more self-descriptive here
app/shop_trip_functions.py
Outdated
class Cart: | ||
def __init__(self, cart: dict, products: dict) -> None: | ||
self.cart = cart | ||
self.products = products | ||
|
||
def calculate_product_cost(self) -> int | float: | ||
total_cost = 0 | ||
for product in self.cart: | ||
total_cost += self.cart[product] * self.products[product] | ||
return total_cost | ||
|
||
def generate_receipt(self) -> None: | ||
for product in self.cart: | ||
product_cost = self.cart[product] * self.products[product] | ||
|
||
# removing .0 from product_cost | ||
product_cost = int(product_cost) \ | ||
if product_cost % 1 == 0 else product_cost | ||
|
||
print( | ||
f"{self.cart[product]} {product}s for {product_cost} dollars" | ||
) |
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.
Let's move Point and Cart to the separate modules to have same structure throughout the code
app/shop_trip_functions.py
Outdated
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.
Let's also avoid this file creation. You can move some main logic to the "main.py" :)
app/shop_trip_functions.py
Outdated
def calculate_distance(p1: list, p2: list) -> float: | ||
p1 = Point(*p1) | ||
p2 = Point(*p2) | ||
|
||
return math.sqrt((p2.x - p1.x) ** 2 + (p2.y - p1.y) ** 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.
Can we make it Point method?
app/shop_trip_functions.py
Outdated
def print_trip_log(fuel_price: float, customer: Customer, shops: list) -> None: | ||
print(f"{customer.name} has {customer.money} dollars") | ||
|
||
shops_trip_cost = {} | ||
for shop in shops: | ||
distance = calculate_distance(customer.location, shop.location) | ||
trip_fuel_price = 2 * calculate_fuel_spent( | ||
fuel_price, customer.car, distance | ||
) | ||
|
||
product_cost = Cart( |
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.
This can be moved in shop as a Shop class method
app/shop_trip_functions.py
Outdated
total_spending = round(product_cost + trip_fuel_price, 2) | ||
|
||
print( | ||
f"{customer.name}'s trip to the {shop.name} costs {total_spending}" | ||
) | ||
|
||
shops_trip_cost[shop] = { | ||
"products_cost": product_cost, | ||
"total": total_spending | ||
} | ||
|
||
closest_shop = min( | ||
shops_trip_cost, key=lambda shop: shops_trip_cost[shop]["total"] | ||
) | ||
|
||
total_spending = shops_trip_cost[closest_shop]["total"] |
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.
Devide it by smaller functions (or methods). Your function is names "print_trip_log", but does much more than just that
You can create find_closest_shop
or calculate_total_spending
function/methods ect.
No description provided.