Skip to content
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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

Solution #610

wants to merge 6 commits into from

Conversation

DSahalatyi
Copy link

No description provided.

app/main.py Outdated

def shop_trip() -> None:

(fuel_price, customers, shops) = get_data_from_file()

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

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
Comment on lines 18 to 43
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:

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
Comment on lines 93 to 95
# left str date for tests, otherwise:
# datetime.strftime(datetime.now(), "%d/%m/%Y %H:%M:%S")
print("Date: 04/01/2021 12:33:41")

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
Comment on lines 10 to 11
px: int
py: int

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

Comment on lines 45 to 66
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"
)

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

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" :)

Comment on lines 30 to 34
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)

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?

Comment on lines 69 to 79
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(

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

Comment on lines 84 to 99
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"]

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants