def linq76(): customers = shared.getCustomerList() order_counts = map(lambda cust: SimpleNamespace(CustomerID=cust.CustomerID, OrderCount=len(cust.Orders)), customers) shared.print_namespace(order_counts)
def linq4(): customers = shared.getCustomerList() wa_customers = (x for x in customers if x.Region == "WA") print("Customers from Washington and their orders:") for customer in wa_customers: print("Customer %s : %s" % (customer.CustomerID, customer.CompanyName)) for order in customer.Orders: print(" Order %s: %s" % (order.OrderID, order.OrderDate))
def linq53(): products = shared.getProductList() customers = shared.getCustomerList() product_first_chars = {p.ProductName[0] for p in products} customer_first_chars = {c.CompanyName[0] for c in customers} unique_first_chars = product_first_chars.difference(customer_first_chars) print("First letters from Product names, but not from Customer names:") shared.printS(unique_first_chars)
def linq51(): products = shared.getProductList() customers = shared.getCustomerList() product_first_chars = {p.ProductName[0] for p in products} customer_first_chars = {c.CompanyName[0] for c in customers} unique_first_chars = product_first_chars.intersection(customer_first_chars) print("Common first letters from Product names and Customer names:") shared.printS(unique_first_chars)
def linq15(): customers = shared.getCustomerList() orders_less_than_500 = ((customer, order) for customer in customers for order in customer.Orders if order.Total < 500.00) orders = (SimpleNamespace(customer_id=x[0].CustomerID, order_id=x[1].OrderID, total=x[1].Total) for x in orders_less_than_500) shared.print_namespace(orders)
def linq95(): products = shared.getProductList() customers = shared.getCustomerList() customer_names = [p.ProductName for p in products] product_names = [c.CompanyName for c in customers] all_names = customer_names + product_names print("Customer and product names:") shared.printS(all_names)
def linq18(): customers = shared.getCustomerList() the_date = datetime.datetime(1998, 1, 1) order_greater_than_date = ((customer, order) for customer in customers for order in customer.Orders if order.OrderDate > the_date) orders = (SimpleNamespace(customer_id=x[0].CustomerID, order_id=x[1].OrderID, orderDate=x[1].OrderDate) for x in order_greater_than_date) shared.print_namespace(orders)
def linq19(): customers = shared.getCustomerList() index = 0 def get_cust_index_func(): nonlocal index index += 1 return index customer_orders = ((cust, get_cust_index_func(), order) for cust in customers for order in cust.Orders) for triplet in customer_orders: print("Customer #%d has an order with OrderID %d" % (triplet[1], triplet[2].OrderID))
def linq23(): customers = shared.getCustomerList() order_greater_than_date = ((cust, order) for cust in customers for order in cust.Orders if cust.Region == "WA") orders = [ SimpleNamespace(customer_id=x[0].CustomerID, order_id=x[1].OrderID, orderDate=x[1].OrderDate) for x in order_greater_than_date ] all_but_first2 = orders[2:] print("All but first 2 orders in WA:") shared.print_namespace(all_but_first2)
def linq21(): customers = shared.getCustomerList() order_greater_than_date = ((cust, order) for cust in customers for order in cust.Orders if cust.Region == "WA") orders = [ SimpleNamespace(customer_id=x[0].CustomerID, order_id=x[1].OrderID, orderDate=x[1].OrderDate) for x in order_greater_than_date ] first_3_orders = orders[:3] print("First 3 orders in WA:") shared.print_namespace(first_3_orders)