5 Commits

2 changed files with 60 additions and 30 deletions

View File

@@ -36,42 +36,71 @@ xnhjrfyjvth3nxr 1
## Design decisions ## Design decisions
The assignment leaves a few points open. The choices made here are: The assignment leaves a few points open:
**"Total number of symbols replaced" counts source symbols.** A line where **"Total number of symbols replaced" counts source symbols.** A line where `bb`
`bb` and `a` were both replaced counts 3, being the combined length of the and `a` were replaced counts 3, the combined length of the matched `value1`
matched `value1` strings, not the number of replacements and not the length of strings, not the number of replacements.
the substituted text. This makes the count a measure of how much of the
original line changed.
**Overlapping patterns resolve to the longest match.** If the configuration **Overlapping patterns resolve to the longest match.** With both `b` and `bb`
contains both `b` and `bb`, the input `bbbb` is read as two `bb` matches rather configured, `bbbb` is two `bb` matches, not four `b`. Values are unique but may
than four `b` matches. The assignment guarantees unique values but not that still be prefixes of one another, so `prepare_pairs` orders by descending length
none is a prefix of another, so `prepare_pairs` orders the pairs by descending and `replace_line` takes the first match at each position. Otherwise the output
length once, and `replace_line` takes the first match at each position. Without would depend on configuration file ordering.
a rule like this the output would depend on configuration file ordering.
**Ties keep their original file order.** The final sort keys only on the **Ties keep their original file order.** The sort keys only on the count, and
replacement count and Python's sort is stable, so lines replaced an equal Python's sort is stable.
number of times appear in the order they had in the input file.
**Unchanged lines are still printed.** The assignment says to sort the changed **Unchanged lines are still printed.** Dropping lines that matched nothing
lines but also to output the resulting text. Dropping lines that happened to would silently lose content, so every line is printed; those with no
match nothing would silently lose content, so every line is printed and lines replacements sort last.
with zero replacements simply sort last.
**Input is preserved apart from the line terminator.** Both `\n` and `\r\n` are **Only the line terminator is stripped.** `\n` and `\r\n` both; every other
stripped; every other character is kept and eligible for replacement. The character stays replaceable. The format has no quoting, so the trailing space in
configuration format has no quoting either, so whitespace inside a value is `a=z ` is part of the value.
significant — the trailing space in `a=z ` is part of the replacement value.
## Performance characteristics
`replace_line` tries every pair at each position, so cost grows with both the
line length and the number of pairs. Two alternatives, measured on the same
20,000-line input of 40-character lines and producing identical output:
| Pairs | this implementation | regex alternation | prefix tree |
| ----: | ------------------: | ----------------: | ----------: |
| 3 | 1x | 1x | 1x |
| 100 | 20x | 1.2x | 1.1x |
| 1000 | 207x | 1.1x | 1.1x |
Median of repeated runs, relative to each column's own three-pair baseline, so
the figures do not depend on the machine. The three-pair run takes about a
quarter of a second. Neither alternative is quite flat, because the larger
configurations in this benchmark also use longer keys, which cost slightly more
to match.
The regex alternation joins every `value1` into one pattern (`a|bb|c`) that
Python's `re` module scans in C: a constant-factor win, the same work done
faster. The prefix tree is the algorithmic one, taking the pair count out of the
complexity rather than making each step cheaper. The explicit loop is kept
because at the sizes described here the difference is not observable, and it
keeps the longest-match rule visible in the code.
The alternation is only equivalent because `prepare_pairs` orders the patterns
longest first: `re` is leftmost-first rather than leftmost-longest, so `b|bb`
would otherwise match `b` against the input `bb`.
Memory is proportional to the input size, since every line is held before
sorting.
## Error handling ## Error handling
The configuration file is validated while loading: a line without `=` is The configuration file is validated while loading: a line without `=`, and a
rejected, reporting the offending line number. Missing files, unreadable line whose `value1` is empty, are both rejected, reporting the offending line
files, and input that is not valid UTF-8 are reported as messages on stderr number. An empty `value1` would match at every position without consuming
rather than tracebacks, and exit with status 1. Status 2 is reserved by anything, which is why it is refused rather than ignored.
`argparse` for command line misuse.
Missing files, unreadable files, and input that is not valid UTF-8 are reported
as messages on stderr rather than tracebacks, and exit with status 1. Status 2
is reserved by `argparse` for command line misuse.
## Tests ## Tests
@@ -81,8 +110,9 @@ pytest
``` ```
- `tests/unit/` covers configuration parsing and the replacement algorithm, - `tests/unit/` covers configuration parsing and the replacement algorithm,
including the overlapping-pattern and empty-input edge cases, plus a line by including the overlapping-pattern case, empty input, and the rejection of an
line check of the worked example in `test_sample_data.py`. empty `value1`, plus a line by line check of the worked example in
`test_sample_data.py`.
- `tests/integration/test_cli.py` invokes `main.py` as a subprocess to cover - `tests/integration/test_cli.py` invokes `main.py` as a subprocess to cover
what unit tests cannot: argument handling, exit status, stderr messages, and what unit tests cannot: argument handling, exit status, stderr messages, and
the sort order as actually printed. the sort order as actually printed.

View File