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 linq33(): products = shared.getProductList() sorted_products = sorted(products, key=lambda p: p.UnitsInStock, reverse=True) shared.print_namespace(sorted_products)
def linq37(): products = shared.getProductList() # negate secondary sort because its a number for reverse order sorted_products = sorted(products, key=lambda product: (product.Category, -product.UnitPrice)) shared.print_namespace(sorted_products)
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 linq83(): products = shared.getProductList() sorted_by_category = sorted(products, key=lambda p: p.Category) grouped_by_category = groupby(sorted_by_category, key=lambda p: p.Category) category_cheapest_price = map(lambda g: SimpleNamespace(Category=g[0], CheapestPrice=min(p.UnitPrice for p in g[1])), grouped_by_category) shared.print_namespace(category_cheapest_price)
def linq80(): products = shared.getProductList() sorted_by_category = sorted(products, key=lambda p: p.Category) grouped_by_category = groupby(sorted_by_category, key=lambda p: p.Category) category_counts = map(lambda g: SimpleNamespace(Category=g[0], TotalUnitsInStock=sum(p.UnitsInStock for p in g[1])), grouped_by_category) shared.print_namespace(category_counts)
def linq77(): products = shared.getProductList() sorted_by_category = sorted(products, key=lambda p: p.Category) grouped_by_category = groupby(sorted_by_category, key=lambda p: p.Category) category_counts = map(lambda g: SimpleNamespace(Category=g[0], ProductCount=len(list(g[1]))), grouped_by_category) shared.print_namespace(category_counts)
def linq87(): products = shared.getProductList() sorted_by_category = sorted(products, key=lambda p: p.Category) grouped_by_category = groupby(sorted_by_category, key=lambda p: p.Category) category_expensive_price = map( lambda g: SimpleNamespace(Category=g[0], MostExpensive=max(p.UnitPrice for p in g[1])), grouped_by_category) shared.print_namespace(category_expensive_price)
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 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)
def linq34(): words = ["aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry"] sorted_words = sorted(words, key=lambda s: s.casefold(), reverse=True) shared.print_namespace(sorted_words)
def linq30(): products = shared.getProductList() sorted_products = sorted(products, key=lambda p: p.ProductName) shared.print_namespace(sorted_products)