11import click
2- from mbdiff .diff_query import DiffQuery
2+ from mbdiff .diff_query import DiffQuery , OPS , InvalidQuery
33from mbdiff .diff import diff_file
44from mbdiff .pretty_print import present_explanations , present_invalid
55
66
7+ def validate_query (ctx , param , value ):
8+ if not value :
9+ raise click .BadParameter ("Cannot be empty" )
10+ value_sp = value .split ()
11+ if len (value_sp ) != 3 :
12+ raise click .BadParameter (
13+ """Query needs to be in format: "COLUMN OPERATOR VALUE" OPERATOR must be one of: <,>,=,<=,>="""
14+ )
15+ metric , op , value = value_sp
16+ if op not in OPS :
17+ raise click .BadParameter ("Operator must be one of: <,>,=,<=,>=" )
18+ return value_sp
19+
20+
21+
722@click .command ()
823@click .argument ("data" , type = click .Path (exists = True ))
924@click .option ("--min-support" , default = 0.1 )
1025@click .option ("--min-risk" , default = 0.1 )
1126@click .option ("--max-order" , default = 3 )
12- @click .option ("--query" )
27+ @click .option ("--query" , callback = validate_query , default = None )
1328def main (data , min_support , min_risk , max_order , query ):
14- metric , op , value = query . split ()
29+ metric , op , value = query
1530 query = DiffQuery (metric , op , value )
16- explanations , invalid = diff_file (data , query , max_order , min_risk , min_support )
31+ try :
32+ explanations , invalid = diff_file (data , query , max_order , min_risk , min_support )
33+ except InvalidQuery as e :
34+ raise click .BadParameter (e )
1735 if explanations :
1836 explanations = sorted (explanations , key = lambda x : x [0 ], reverse = True )
1937 print ("Explanations" )
@@ -27,6 +45,5 @@ def main(data, min_support, min_risk, max_order, query):
2745 print ("There were no invalid or below threshold attribute combinations" )
2846
2947
30-
3148if __name__ == "__main__" :
3249 main ()
0 commit comments