forked from rogerwcpt/python-linq-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinq-restrictions.py
68 lines (44 loc) · 1.68 KB
/
linq-restrictions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import shared
def linq1():
numbers = [5, 4, 1, 3, 9, 8, 6, 7, 2, 0]
low_nums = (x for x in numbers if x < 5)
print("Numbers < 5:")
shared.printN(low_nums)
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)
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 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 linq5():
digits = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
index = 0
# Lambdas cant have multiple lines, so create a filter function
def filter_func(digit):
nonlocal index
result = len(digit) < index
index += 1
return result
short_digits = filter(filter_func, digits)
print("Short digits:")
for d in short_digits:
print("The word %s is shorter than its value." % d)
linq1()
# linq2()
# linq3()
# linq4()
# linq5()