Utility classes and functions

class mtrchk.org.momotor.evaluate.compare.level.CompareLevel(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)

Enum to indicate the difference between two strings.

classmethod index(item)

Returns the index of the given item in the enumeration.

Return type:

int

BLANK_LINES = 'blank'

The two strings compare identical except for case, whitespace, and blank lines.

CASE = 'case'

The two strings compare identical except for case.

DIFFERENT = 'different'

The two strings compare different

IDENTICAL = 'identical'

The two strings compare identical.

WHITESPACE = 'whitespace'

The two strings compare identical except for case and whitespace.

mtrchk.org.momotor.evaluate.compare.mixin.casefold(line)

Fold the case for the given line.

Return type:

str | None

mtrchk.org.momotor.evaluate.compare.mixin.collapse_ws(line)

Collapse all whitespace in the given line to a single space.

Return type:

str | None

mtrchk.org.momotor.evaluate.compare.mixin.compare_streams(left, right, *, ignore_last_newline=False, ignore_trailing_whitespace=False, sort_left=False, sort_right=False, encoding=None)

Compare two files as efficient as possible.

Consumes each stream line-by-line if no sorting is required, otherwise reads the entire file into memory, sorts it, and then compares the sorted lines.

Returns a CompareLevel indicating the amount of difference between the two streams.

Parameters:
  • left (File) – first stream

  • right (File) – second stream

  • ignore_last_newline (bool)

  • ignore_trailing_whitespace (bool)

  • sort_left (str | bool | None) – Sort the left file before comparison

  • sort_right (str | bool | None) – Sort the right file before comparison

  • encoding (str | None) – Encoding to use for reading the files

Return type:

CompareLevel

Returns:

The level of difference

mtrchk.org.momotor.evaluate.compare.mixin.get_sort_fn(sort_opt)

Return a sort function based on the given option value.

sort_opt is either a boolean value (yes, no, true, false, etc.), or human, nat, os, real to sort using a natural sort algorithm.

See the documentation of the natsort package for details on the natural sort algorithms.

Parameters:

sort_opt (str | bool | None) – The sort option name

Return type:

Callable[[Iterable[str]], list[str]] | None

Returns:

The sort function, or None if no sorting is required

Raises:

CheckletError – if the option value is invalid

mtrchk.org.momotor.evaluate.compare.mixin.iter_file(stream)

Iterate over the given stream, yielding lines without trailing newlines.

The return value of the generator (available as StopIteration.value) is a boolean, indicating whether the final line ended with a newline.

Parameters:

stream (Iterable[str]) – stream of strings to iterate over

Return type:

Generator[str, None, bool]

mtrchk.org.momotor.evaluate.compare.mixin.iter_sorted(stream, sort_fn)

Iterate over the given stream, yielding lines without trailing newlines, sorted by the given function.

The return value of the generator (available as StopIteration.value) is a boolean, indicating whether the final line ended with a newline.

Parameters:
Return type:

Generator[str, None, bool]

mtrchk.org.momotor.evaluate.compare.mixin.rstrip(line)

Strip trailing whitespace from the given line.

Return type:

str | None

mtrchk.org.momotor.evaluate.compare.mixin.strip_newlines(line)

Strip all newlines from the given line.

Return type:

str