CAIT

A package of tools for capturing student code by matching it against patterns.

Helper functions for traversing through the code structure.

find_function_calls(name: str, root=None, report=<pedal.core.report.Report object>)[source]

Returns a list of CaitNodes representing all of the function calls that were found. This includes both methods and regular functions.

Parameters:
  • name (str) – The name of the function to search.

  • root – A subtree of a parse tree to revert to.

  • report (Report) – The name of the Report to refer to.

Returns:

Relevant call nodes.

Return type:

List[CaitNode]

find_function_definition(name, root=None, report=<pedal.core.report.Report object>)[source]

Finds the given function definition based on the given name.

Parameters:
  • name (str) – The name of the function.

  • root – A subtree of a parse tree to revert to.

  • report (Report) – The name of the Report to refer to.

Returns:

The first occurrence of a

function with the given name.

Return type:

pedal.cait.cait_node.CaitNode

find_operation(op_name, root=None, report=<pedal.core.report.Report object>)[source]

Returns all the occurrences of the operator op_name in the source code. You can specify the operator as a string like "+" or "<<". Supports all comparison, boolean, binary, and unary operators.

find_prior_initializations(node, report=<pedal.core.report.Report object>)[source]

DEPRECATED

Given a Name node, returns a list of all the assignment statements that incorporate that Name node prior to that line. Returns None if no Name is given.

function_is_called(name: str, report=<pedal.core.report.Report object>) int[source]

DEPRECATED

is_top_level(ast_node, report=<pedal.core.report.Report object>) bool[source]

Determines if the ast_node is at the top-level of the program. Correctly handles expression statements (so a print call on its own will be considered a statement, even though its technically an expression).

Parameters:
  • ast_node (pedal.cait.cait_node.CaitNode) – The CaitNode to check

  • report (Report)

Returns:

Whether the node is from the top level

Return type:

bool

Cait Report Data

Field

Type

Initial

Description

ast

CaitNode

None

The CaitNode tree that was most recently parsed out.

cache

{str: CaitNode}

{}

A dictionary mapping previously parsed code to CaitNode trees.

success

bool

True

Whether the most recent parsing was successful.

error

Exception

None

The most recent exception, or None.

exception CaitException[source]
data_state(node, report=<pedal.core.report.Report object>)[source]

Determines the Tifa State of the given node.

Parameters:
  • node (str or AstNode or CaitNode) – The Name node to look up in TIFA.

  • report (Report) – The report to attach data to. Defaults to MAIN_REPORT.

Returns:

The State of the object (Tifa State) or None if it doesn’t exist

data_type(node, report=<pedal.core.report.Report object>)[source]

Looks up the type of the node using Tifa’s analysis.

Parameters:
  • node (str or AstNode or CaitNode) – The Name node to look up in TIFA.

  • report (Report) – The report to attach data to. Defaults to MAIN_REPORT.

Returns:

The type of the object (Tifa type) or None if a type doesn’t exist

def_use_error(node, report=<pedal.core.report.Report object>)[source]

Checks if node is a name and has a def_use_error

Parameters:
  • node (str or AstNode or CaitNode) – The Name node to look up.

  • report (Report) – The report to attach data to. Defaults to MAIN_REPORT.

Returns:

True if the given name has a def_use_error

expire_cait_cache(report=<pedal.core.report.Report object>)[source]

Deletes the most recent CAIT run and any cached CAIT parses.

Parameters:

report (Report) – The report to attach data to. Defaults to MAIN_REPORT.

find_asts(node_name: str, student_code=None, report=<pedal.core.report.Report object>)[source]

Find all occurrences of the given AST node, based on the name of the AST node (e.g., "For" or "FunctionDef").

Parameters:
  • node_name – the string representing the “type” of node to look for

  • student_code (str) – Optionally, different code to parse and search.

  • report – defaults to MAIN_REPORT unless another one is given.

Returns:

a list of Ast Nodes (cait_nodes) of self that are of the specified type (including self if self

meets that criteria)

Returns:

find_expr_sub_matches(pattern, student_code, is_mod=False, report=<pedal.core.report.Report object>)[source]

Finds pattern in student_code # TODO: Add code to make pattern accept CaitNodes # TODO: Make this function without so much meta knowledge :param pattern: the expression to find (str that MUST evaluate to a Module node with a single child or an AstNode) :param student_code: student subtree :param is_mod: currently hack for multiline sub matches :type is_mod: bool :param report: defaults to MAIN_REPORT unless another one exists

Returns:

a list of matches or False if no matches found

find_match(pattern, student_code=None, report=<pedal.core.report.Report object>, cut=False, use_previous=None)[source]

Apply Tree Inclusion and return the first match of the pattern in the student_code.

Parameters:
  • pattern (str) – The CaitExpression to match against.

  • student_code (str) – The string of student code to check against. Defaults to the code of the Source tool in the Report.

  • report (Report) – The report to attach data to.

  • cut (bool) – Set to true to trim root to first branch

  • use_previous (AstMap) – If user wants to continue off of a previously found match

Returns:

The first matching node for the given pattern, or

None if nothing was found.

Return type:

CaitNode or None

find_matches(pattern, student_code=None, cut=False, report=<pedal.core.report.Report object>, use_previous=None)[source]

Apply Tree Inclusion and return all matches of the pattern in the student_code.

Parameters:
  • pattern (str) – The CaitExpression to match against.

  • student_code (str) – The string of student code to check against.

  • report (Report) – The report to attach data to.

  • cut (bool) – Set to true to trim root to first branch

  • use_previous (AstMap) – If user wants to continue off of a previously found match

Returns:

All matching nodes for the given pattern.

Return type:

list[pedal.cait.ast_map.AstMap]

find_submatches(pattern, student_code, is_mod=False, report=<pedal.core.report.Report object>)[source]

Incomplete.

parse_program(student_code=None, report=<pedal.core.report.Report object>)[source]

Parses student code and produces a CAIT representation.

Parameters:
  • student_code (str) – The student source code to parse. If None, defaults to the code within the Source tool of the given Report.

  • report (Report) – The report to attach data to. Defaults to MAIN_REPORT.

Returns:

A CAIT-enhanced representation of the root Node.

Return type:

CaitNode

reparse_if_needed(student_code=None, report=<pedal.core.report.Report object>)[source]

Retrieves the current report for CAIT. If there is no CAIT report, it will generate one. If source code is given, that will be used instead of the report’s submission.

Parameters:
  • student_code (str) – The code to parse into the a CaitNode tree. If None, then it will use the code in the report’s submission.

  • report (Report) – The report to attach data to.

Returns:

Returns the Cait Report

Return type:

dict

require_tifa(self)[source]

Confirms that TIFA was run successfully, otherwise raises a CaitException.

TODO: Is this deprecated?

reset(report=<pedal.core.report.Report object>)[source]
Parameters:

report

Returns: