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
|
#!/usr/bin/env python3
"""
Another demo of filtering capabilities.
Demonstrates how to use named parameters
"""
from allpairspy import AllPairs
def is_valid_combination(values, names):
dictionary = dict(zip(names, values))
"""
Should return True if combination is valid and False otherwise.
Dictionary that is passed here can be incomplete.
To prevent search for unnecessary items filtering function
is executed with found subset of data to validate it.
"""
rules = [
# Brand Y does not support Windows 98
# Brand X does not work with XP
# Contractors are billed in 30 min increments
lambda d: "98" == d["os"] and "Brand Y" == d["brand"],
lambda d: "XP" == d["os"] and "Brand X" == d["brand"],
lambda d: "Contr." == d["employee"] and d["increment"] < 30,
]
for rule in rules:
try:
if rule(dictionary):
return False
except KeyError:
pass
return True
# sample parameters are is taken from
# http://www.stsc.hill.af.mil/consulting/sw_testing/improvement/cst.html
parameters = [
("brand", ["Brand X", "Brand Y"]),
("os", ["98", "NT", "2000", "XP"]),
("network", ["Internal", "Modem"]),
("employee", ["Salaried", "Hourly", "Part-Time", "Contr."]),
("increment", [6, 10, 15, 30, 60]),
]
pairwise = AllPairs(
[x[1] for x in parameters],
filter_func=lambda values: is_valid_combination(values, [x[0] for x in parameters]),
)
print("PAIRWISE:")
for i, pairs in enumerate(pairwise):
print(f"{i:2d}: {pairs}")
|