has_equal_ast¶
-
has_equal_ast
(incorrect_msg='FMT: Your code does not seem to match the solution.', code=None, exact=True, state=None)¶ Test whether abstract syntax trees match between the student and solution code.
Parameters: - incorrect_msg – message displayed when ASTs mismatch.
- code – optional code to use instead of the solution AST
- exact – whether the representations must match exactly. If false, the solution AST only needs to be contained within the student AST (similar to using test student typed).
Example: Student and Solution Code:
dict(a = 'value').keys()
SCT:
# all pass Ex().has_equal_ast() Ex().has_equal_ast(code = "dict(a = 'value').keys()") Ex().has_equal_ast(code = "dict(a = 'value')", exact = False)
An abstract syntax tree (AST) is a way of representing the high-level structure of python code.
Example: quotes¶
Whether you use the concrete syntax x = "1"
or x = '1'
, the abstract syntax is the same: x is being assigned to the string “1”.
Example: parenthesis¶
Grouping by parentheses produces the same AST, when the same statement would work the same without them.
For example, (True or False) and True
, and True or False and True
, are the same due to operator precedence.
Example: spacing¶
The same holds for different types of spacing that essentially specify the same statement: x = 1
or x = 1
.
Caveat: evaluating¶
What the AST doesn’t represent is values that are found through evaluation. For example, the first item in the list in
x = 1
[x, 2, 3]
and
[1, 2, 3]
Is not the same. In the first case, the AST represents that a variable x
needs to be evaluated in order to find out what its value is. In the second case, it just represents the value 1
.