Utility Functions

A collection of functions that have no other place in the package, and serve general utility purposes.

exception AssignmentCheckerError

Allows us to define a custom exception type for when the assignment checker encounters a genuine problem with the submission.

copy_tree(src: Path, dest: Path, into: bool = False) Path

Copies the directory tree to the location on the filesystem.

Returns the path to the copied tree.

Parameters:
  • src – Root directory whose tree should be copied.

  • dest – Path to the copy destination.

  • into – If True, then src will be copied into dest, under the name dest / src.stem, rather than directly to the destination location.

match_to_unique_assignments(possible_mappings: Dict[Object, Set[Value]]) Dict[Object, Value]

Given a set of objects, and possible assignments to a set of values for each object, determine a 1:1 mapping of objects to values that is compatible with the possible options.

If no such mapping is possible, the returned dictionary is empty.

Example

>>> possible_mappings = {
    "a": [1, 2, 3],
    "b": [3],
    "c": [2, 3],
}
>>> match_to_unique_assigments(possible_mappings)
{"a": 1, "b", 3, "c": 2}
on_readonly_error(f: Callable[[Path], None], path: Path, exc_info) None

Error handler for shutil.rmtree.

If the error is due to an access error (read only file) it attempts to add write permission and then retries.

If the error is for another reason it re-raises the error.

Usage : shutil.rmtree(path, onerror=on_readonly_error)

provide_tmp_directory(clean_on_error: bool = True, clean_on_success: bool = True, pass_dir_as_arg: str | None = None, where: Path | None = None) Callable[[Callable[[Any], Any]], Callable[[Any], Any]]

Wraps the execution of a function with the creation and optional teardown of a temporary directory, that can be optionally passed to the wrapped function.

Parameters:

clean_on_error – If True, the temporary directory that is created will be removed if

the wrapped function raises an error. :param clean_on_success: If True, the temporary directory that is created will be removed if the wrapped function returns without raising an error. :param pass_dir_as_arg: If provided, the wrapped function will automatically be passed a keyword argument whose name is the value of pass_dir_as_arg, and whose value is the path to the created temporary directory. :param where: If provided, this should be a path to a predefined location to use as the temporary directory. It must not currently exist on the filesystem, to ensure safety when deleting it.