Consider the following segment of code:
# Fetch my orders
orders = repo.get_orders_for(self._customer[0])
if len(orders) > 0:
current_order_no = -1
for (order_number, quantity, pid) in orders:
# Print new heading if needed
if order_number is not current_order_no:
print("Order #{}".format(order_number))
current_order_no = order_number
print(" Product #{}, ordered x{}".format(pid, quantity))
I feel like there's just a lot going on. I basically do a JOIN
on two tables, and return some order
s and their order_item
s. The JOIN
obviously duplicates the order_number on several rows, so I need a way of telling them apart. Is there a better way of doing this in Python (actually, I'll accept any language answer for curiosity!) Or is it my way of joining that's evil?
For the curious, the SQL statement is:
SELECT OrderNumber, Quantity, Product_ProductId FROM `order` JOIN `orderitem` ON `order`.OrderNumber = Order_OrderNumber WHERE CustomerAccount_CustomerId=%s ORDER BY OrderNumber