is_boolean_expression function¶
(Shortest import: from brian2.parsing.expressions import is_boolean_expression)
- brian2.parsing.expressions.is_boolean_expression(expr, variables)[source]¶
Determines if an expression is of boolean type or not
- Parameters
expr : str
The expression to test
variables : dict-like of
VariableThe variables used in the expression.
- Returns
isbool : bool
Whether or not the expression is boolean.
Raises
SyntaxErrorIf the expression ought to be boolean but is not, for example
x<y and zwherezis not a boolean variable.
Notes
We test the following cases recursively on the abstract syntax tree:
The node is a boolean operation. If all the subnodes are boolean expressions we return
True, otherwise we raise theSyntaxError.The node is a function call, we return
TrueorFalsedepending on whether the function description has the_returns_boolattribute set.The node is a variable name, we return
TrueorFalsedepending on whetheris_booleanattribute is set or if the name isTrueorFalse.The node is a comparison, we return
True.The node is a unary operation, we return
Trueif the operation isnot, otherwiseFalse.Otherwise we return
False.