Comparison Module
The modules.comparison module provides utilities for comparing two text files
character by character. It is used in the test suite to verify that processed
packet output matches expected reference files exactly.
Overview
The comparison pipeline works in three stages:
- Read – both files are loaded as UTF-8 strings via
read_text. - Locate –
find_first_mismatchscans character by character for the first divergence. - Report –
compare_filesandcompare_text_contentreturn the mismatch index, file lengths, and the differing characters for easy diagnosis.
API Reference
modules.comparison
Utilities for comparing text file contents character by character.
Used primarily in tests to verify that processed payload output matches expected reference files.
compare_files(path1, path2)
Compare two text files and report the first point of divergence.
Both paths are resolved to absolute paths before reading so that relative paths are handled correctly regardless of the working directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path1
|
str | Path
|
Path to the first file (actual output). |
required |
path2
|
str | Path
|
Path to the second file (expected reference). |
required |
Returns:
| Type | Description |
|---|---|
int | None
|
A five-tuple of |
int
|
|
int
|
|
str | None
|
difference, or |
str | None
|
|
tuple[int | None, int, int, str | None, str | None]
|
|
tuple[int | None, int, int, str | None, str | None]
|
characters at |
tuple[int | None, int, int, str | None, str | None]
|
a length difference. |
Source code in modules/comparison.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
compare_text_content(data1, data2)
Compare two strings and return mismatch details.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data1
|
str
|
The first string to compare. |
required |
data2
|
str
|
The second string to compare. |
required |
Returns:
| Type | Description |
|---|---|
int | None
|
A three-tuple of |
int
|
|
Source code in modules/comparison.py
42 43 44 45 46 47 48 49 50 51 52 53 54 | |
find_first_mismatch(data1, data2)
Find the index of the first differing character between two strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data1
|
str
|
The first string to compare. |
required |
data2
|
str
|
The second string to compare. |
required |
Returns:
| Type | Description |
|---|---|
int | None
|
The zero-based index of the first mismatch, or |
int | None
|
are identical. |
Source code in modules/comparison.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | |
read_text(path)
Read a file's contents as a UTF-8 string, replacing undecodable bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Absolute or relative path to the file. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The file contents as a string. |
Source code in modules/comparison.py
10 11 12 13 14 15 16 17 18 19 | |