I have a block of code in a function that does some comparisons, namely:
if customer_info['total_dls'] < min_training_actions \
or customer_info['percentage'] > train_perc_cutoff:
continue
elif customer_id not in test_set \
or test_set[customer_id]['total_dls'] < min_testing_actions:
num_uncertain +=1
continue
elif test_set[customer_id]['percentage'] <= test_perc_cutoff:
num_correct +=1
else:
num_incorrect +=1
Now sometimes I need to do those comparisons to be greater than, and sometimes I need them to be less than. All of the rest of the code is exactly the same. Now, I could just make two functions that reuse basically the same code, but before I do, is there some clean way to variabalize the comparison operator, so I could just use the same block of code and pass in the comparison as a variable? Something like: compare(var1, var2, polarity)
. I know I can make this myself, but I'm wondering what the standard is in cases like this. Is there some pretty pythonic way of doing this I'm unaware of?
[Edit] Adding emphasis to the most important part of the question [/Edit]