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
Variable
The variables used in the expression.
Returns: isbool : bool
Whether or not the expression is boolean.
Raises
SyntaxError
- If the expression ought to be boolean but is not, for example
x<y and z
wherez
is 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
True
orFalse
depending on whether the function description has the_returns_bool
attribute set. - The node is a variable name, we return
True
orFalse
depending on whetheris_boolean
attribute is set or if the name isTrue
orFalse
. - The node is a comparison, we return
True
. - The node is a unary operation, we return
True
if the operation isnot
, otherwiseFalse
. - Otherwise we return
False
.