From Newsgroup: comp.lang.python.announce
We all know that the assert statement should not be used in production
code. Instead, you can use assertion-like functions from the easycheck
package.
Consider the following example. You have a function that fits a statistical model that requires sample size (n) to be at least 20. So,
assert n >= 20
would not enable the function to continue if the condition is violated. But it's the assert statement, which should not be used that way. Here's where easycheck shines:
easycheck.check_if(n >= 20)
is the easiest solution. It would throw AttributeError without a message.
But we can improve this a lot; for example:
class TooSmallSampleSizeError(Exception): ...
easycheck.check_if(n >=20, handle_with=TooSmallSampleSizeError, message=f"Sample size of {n} is too small for the model to run")
You can also issue warnings with easycheck:
class TooSmallSampleSizeWarning(Warning): ...
easycheck.check_if(n >=20, TooSmallSampleSizeWarning, f"Sample of {n}
can be too small")
If n is smaller than 20, you will see the following warning: "TooSmallSampleSizeWarning: Sample of 10 is too small"
The easycheck package offers functions dedicated to
- checking general conditions (like above)
- checking an object's type
- checking path(s)
- checking an object's length
- comparing floating-point numbers
- checking function arguments
- combining several conditions
Most of them have aliases dedicated to unit testing. The package's
advantages are the readability of the code, and the ease of using the
package's functions.
You can install easycheck from PyPi (pip install easycheck). You can find easycheck on GitHub:
https://github.com/nyggus/easycheck/. If you like it, consider leaving a star. Contributions are also welcome!
Enjoy easycheck-ing,
Marcin
--- Synchronet 3.19c-Linux NewsLink 1.113