I'm writing a library of Python functions to transform a dataset, and for each one I need to make some basic checks on the transformed data:
- There should be no negative values in the dataset after transforming
- The sum of all values in the transformed dataset should be in some specified ratio to the sum of the values before transforming.
For the first task, I can give all my functions a decorator with a name like check_for_negatives.
For the second task however, the ratio of output to input will vary with each call to a particular function, depending on the input parameters - so the caller might expect the sum of the dataset to be unchanged, or to be x1.5, for different calls of the same function.
The only way I can see to do this with decorators is to have a decorator to check the input:output ratio, but pass in the expected ratio as an argument to the function itself (though the function wouldn't use it directly). This doesn't seem very nice; is there a better way?