Checks¶
-
has_code
(state, pattern, fixed=False, incorrect_msg=None, normalize=<function <lambda>>)¶
-
check_function
(state, name, index=0, missing_msg=None)¶
-
check_operator
(state, operator, missing_msg=None)¶
-
has_equal_value
(state, incorrect_msg=None, ndigits=4)¶
-
has_equal_formula
(state, incorrect_msg=None, ndigits=4)¶
Logic¶
-
classmethod
Ex.
__call__
(state=None)¶ Returns the current code state as a Chain instance.
This allows SCTs to be run without including their 1st argument,
state
.When writing SCTs on DataCamp, no State argument to
Ex
is necessary. The exercise State is built for you.Parameters: state – a State instance, which contains the student/solution code and results.
Example: code
# How to write SCT on DataCamp.com Ex().has_code(text="SELECT id") # Experiment locally - chain off of Ex(), created from state state = SomeStateProducingFunction() Ex(state).has_code(text="SELECT id") # Experiment locally - no Ex(), create and pass state explicitly state = SomeStateProducingFunction() has_code(state, text="SELECT id")
-
multi
(state, *tests)¶ Run multiple subtests. Return original state (for chaining).
This function could be thought as an AND statement, since all tests it runs must pass
Parameters: - state – State instance describing student and solution code, can be omitted if used with Ex()
- tests – one or more sub-SCTs to run.
Example: The SCT below checks two has_code cases..
Ex().multi(has_code('SELECT'), has_code('WHERE'))
The SCT below uses
multi
to ‘branch out’ to check that the SELECT statement has both a WHERE and LIMIT clause..Ex().check_node('SelectStmt', 0).multi( check_edge('where_clause'), check_edge('limit_clause') )
-
check_not
(state, *tests, msg)¶ Run multiple subtests that should fail. If all subtests fail, returns original state (for chaining)
- This function is currently only tested in working with
has_code()
in the subtests. - This function can be thought as a
NOT(x OR y OR ...)
statement, since all tests it runs must fail - This function can be considered a direct counterpart of multi.
Parameters: - state – State instance describing student and solution code, can be omitted if used with Ex()
- *tests – one or more sub-SCTs to run
- msg – feedback message that is shown in case not all tests specified in
*tests
fail.
Example: Thh SCT below runs two has_code cases..
Ex().check_not( has_code('INNER'), has_code('OUTER'), incorrect_msg="Don't use `INNER` or `OUTER`!" )
If students use
INNER (JOIN)
orOUTER (JOIN)
in their code, this test will fail.- This function is currently only tested in working with
-
check_or
(state, *tests)¶ Test whether at least one SCT passes.
If all of the tests fail, the feedback of the first test will be presented to the student.
Parameters: - state – State instance describing student and solution code, can be omitted if used with Ex()
- tests – one or more sub-SCTs to run
Example: The SCT below tests that the student typed either ‘SELECT’ or ‘WHERE’ (or both)..
Ex().check_or( has_code('SELECT'), has_code('WHERE') )
The SCT below checks that a SELECT statement has at least a WHERE c or LIMIT clause..
Ex().check_node('SelectStmt', 0).check_or( check_edge('where_clause'), check_edge('limit_clause') )
-
check_correct
(state, check, diagnose)¶ Allows feedback from a diagnostic SCT, only if a check SCT fails.
Parameters: - state – State instance describing student and solution code. Can be omitted if used with Ex().
- check – An sct chain that must succeed.
- diagnose – An sct chain to run if the check fails.
Example: The SCT below tests whether students query result is correct, before running diagnostic SCTs..
Ex().check_correct( check_result(), check_node('SelectStmt') )
-
disable_highlighting
(state)¶ Disable highlighting in the remainder of the SCT chain.
Include this function if you want to avoid that pythonwhat marks which part of the student submission is incorrect.
-
fail
(state, msg='fail')¶ Always fails the SCT, with an optional msg.
This function takes a single argument,
msg
, that is the feedback given to the student. Note that this would be a terrible idea for grading submissions, but may be useful while writing SCTs. For example, failing a test will highlight the code as if the previous test/check had failed.
Electives¶
-
has_chosen
(state, correct, msgs)¶ Verify exercises of the type MultipleChoiceExercise
Parameters: - state – State instance describing student and solution code. Can be omitted if used with Ex().
- correct – index of correct option, where 1 is the first option.
- msgs – list of feedback messages corresponding to each option.
Example: The following SCT is for a multiple choice exercise with 2 options, the first of which is correct.:
Ex().has_chosen(1, ['Correct!', 'Incorrect. Try again!'])
-
success_msg
(state, msg)¶ Changes the success message to display if submission passes.
Parameters: - state – State instance describing student and solution code. Can be omitted if used with Ex().
- msg – feedback message if student and solution ASTs don’t match
Example: The following SCT changes the success message:
Ex().success_msg("You did it!")