gpf.common.validate module¶
The validate module can be used to verify if certain values are “truthy”. Most of the functions in this module return a boolean (True/False).
Exceptions to this rule are the pass_if()
and raise_if()
functions, which can be used for data assertions
and therefore serve as an alternative to the assert
statement (which should not be used in production environments).
>>> def test(text):
>>> pass_if(isinstance(text, str), TypeError, 'test() requires text input')
>>> print(text)
>>>
>>> test('Hello World')
'Hello World'
>>>
>>> test(123)
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: test() requires text input
-
gpf.common.validate.
is_text
(value: Any, allow_empty: bool = True) → bool[source]¶ Returns
True
if value is astr
instance.Parameters: - value – The value to check.
- allow_empty – If
True
(default), empty string values are allowed. IfFalse
, empty strings will evaluate as “falsy”.
-
gpf.common.validate.
is_number
(value: Any, allow_bool: bool = False) → bool[source]¶ Returns
True
if value is a built-in numeric object (e.g.int
,float
,long
,Decimal
, etc.).Note that
is_number()
will returnFalse
for booleans by default, which is non-standard behaviour, becausebool
is a subclass ofint
:>>> isinstance(True, int) True >>> is_number(True) False
Parameters: - value – The value to check.
- allow_bool – If the standard Python boolean evaluation behavior is desired, set this to
True
.
-
gpf.common.validate.
is_iterable
(value: Any) → bool[source]¶ Returns
True
if value is an iterable container (e.g.list
ortuple
but not a generator).Note that
is_iterable()
will returnFalse
for string-like objects as well, even though they are iterable. The same applies to generators and sets:>>> my_list = [1, 2, 3] >>> is_iterable(my_list) True >>> is_iterable(set(my_list)) False >>> is_iterable(v for v in my_list) False >>> is_iterable(str(my_list)) False
Parameters: value – The value to check.
-
gpf.common.validate.
is_guid
(value: Any) → bool[source]¶ Returns
True
when the given value is a GUID-like object andFalse
otherwise. The function effectively tries to parse the value as agpf.tools.guids.Guid
object.Parameters: value – A string or a GUID-like object.
-
gpf.common.validate.
has_value
(obj: Any, strip: bool = False) → bool[source]¶ Returns
True
when obj is “truthy”.Note that
has_value()
will returnTrue
for theFalse
boolean and0
integer values. Python normally evaluates these values as “falsy”, but for databases for example, these values are perfectly valid. This is whyhas_value()
considers them to be “truthy”:>>> my_int = 0 >>> my_bool = False >>> if not (my_int and my_bool): >>> print('Both values are "falsy".') 'Both values are "falsy".' >>> has_value(my_int) True >>> has_value(my_bool) True
Other usage examples:
>>> has_value(None) False >>> has_value({}) False >>> has_value('test') True >>> has_value(' ', strip=True) False
Parameters: - obj – The object for which to evaluate its value.
- strip – If
True
and obj is astr
, obj will be stripped before evaluation. Defaults toFalse
.
-
gpf.common.validate.
signature_matches
(func: Callable, template_func: Callable) → bool[source]¶ Checks if the given func (function or instancemethod) has a signature equal to template_func. If the function is not callable or the signature does not match,
False
is returned.Parameters: - func – A callable function or (instance) method.
- template_func – A template function to which the callable function argument count should be compared. This should not be a an instance method.
Return type: bool
-
gpf.common.validate.
pass_if
(expression: Any, exc_type: type, exc_val: Any = '') → bool[source]¶ Raises an error of type err_type when expression is falsy and silently passes otherwise. Opposite of
raise_if()
.Parameters: - expression – An expression or value to evaluate.
- exc_type – The error to raise when expression evaluates to
False
, e.g.AttributeError
. - exc_val – An optional message or exception value to include with the error (recommended).
Examples: >>> my_value = 0 >>> pass_if(my_value) Traceback (most recent call last): File "<input>", line 1, in <module> AssertionError >>> pass_if(my_value == 0) >>> pass_if(my_value == 1, ValueError, 'my_value should be 1') Traceback (most recent call last): File "<input>", line 1, in <module> ValueError: my_value should be 1
-
gpf.common.validate.
raise_if
(expression: Any, exc_type: type, exc_val: Any = '') → bool[source]¶ Raises an error of type err_type when expression is truthy and silently passes otherwise. Opposite of
pass_if()
.Parameters: - expression – An expression or value to evaluate.
- exc_type – The error to raise when expression evaluates to
True
, e.g.AttributeError
. - exc_val – An optional message or exception value to include with the error (recommended).
Examples: >>> my_value = 0 >>> raise_if(my_value) >>> raise_if(my_value == 1) >>> raise_if(my_value == 0, ValueError, 'my_value should not be 0') Traceback (most recent call last): File "<input>", line 1, in <module> ValueError: my_value should not be 0