Ejemplo n.º 1
0
def linq47():
    products = shared.getProductList()

    category_names = {p.Category for p in products}

    print("Category names:")
    shared.printS(category_names)
Ejemplo n.º 2
0
def linq7():
    products = shared.getProductList()

    product_names = (p.ProductName for p in products)

    print("Product Names:")
    shared.printS(product_names)
Ejemplo n.º 3
0
def linq2():
    products = shared.getProductList()

    sold_out_products = (x for x in products if x.UnitsInStock == 0)

    print("Sold out products:")
    for item in sold_out_products:
        print("%s is sold out!" % item.ProductName)
Ejemplo n.º 4
0
def linq33():
    products = shared.getProductList()

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

    shared.print_namespace(sorted_products)
Ejemplo n.º 5
0
def linq3():
    products = shared.getProductList()

    expensive_in_stock_products = (
        x for x in products if x.UnitsInStock > 0 and x.UnitPrice > 3.0000)

    print("In-stock products that cost more than 3.00:")
    for item in expensive_in_stock_products:
        print("%s is in stock and costs more than 3.00." % item.ProductName)
def linq42():
    products = shared.getProductList()

    sorted_by_category = sorted(products, key=lambda p: p.Category)
    order_groups = groupby(sorted_by_category, key=lambda p: p.Category)

    for key, items in order_groups:
        print("Products in the category '%s':" % key)
        print(list(items))
Ejemplo n.º 7
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)
Ejemplo n.º 8
0
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)
Ejemplo n.º 9
0
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)
Ejemplo n.º 10
0
def linq11():
    products = shared.getProductList()

    product_info = (SimpleNamespace(ProductName=p.ProductName,
                                    Category=p.Category,
                                    Price=p.UnitPrice) for p in products)

    print("Product Info:")
    for product in product_info:
        print("%s is in the category %s and costs %.2f per unit." %
              (product.ProductName, product.Category, product.Price))
Ejemplo n.º 11
0
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 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)
Ejemplo n.º 16
0
def linq30():
    products = shared.getProductList()

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

    shared.print_namespace(sorted_products)