コード例 #1
0
def linq76():
    customers = shared.getCustomerList()

    order_counts = map(lambda cust: SimpleNamespace(CustomerID=cust.CustomerID,
                                                    OrderCount=len(cust.Orders)),
                       customers)

    shared.print_namespace(order_counts)
コード例 #2
0
def linq33():
    products = shared.getProductList()

    sorted_products = sorted(products,
                             key=lambda p: p.UnitsInStock,
                             reverse=True)

    shared.print_namespace(sorted_products)
コード例 #3
0
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)
コード例 #4
0
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)
コード例 #5
0
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)
コード例 #6
0
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)
コード例 #7
0
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)
コード例 #8
0
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)
コード例 #9
0
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)
コード例 #10
0
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)
コード例 #11
0
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)
コード例 #12
0
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)
コード例 #13
0
def linq30():
    products = shared.getProductList()

    sorted_products = sorted(products, key=lambda p: p.ProductName)

    shared.print_namespace(sorted_products)