Drop the verbatim config.txt and input.txt listings from the example; both files are in the repository root. Merge the whitespace and line terminator notes into a single input-handling decision.
98 lines
3.4 KiB
Markdown
98 lines
3.4 KiB
Markdown
# Replace Configured Values in a Text File
|
|
|
|
Reads `value1=value2` pairs from a configuration file, replaces every
|
|
non-overlapping occurrence of `value1` with `value2` in a text file, and prints
|
|
the resulting lines sorted by how many symbols were replaced, most-changed
|
|
first.
|
|
|
|
## Requirements
|
|
|
|
Python 3.9 or newer. No runtime dependencies.
|
|
|
|
## Usage
|
|
|
|
```bash
|
|
python main.py <config_file> <text_file>
|
|
```
|
|
|
|
### Example
|
|
|
|
With the provided `config.txt` and `input.txt`, running:
|
|
|
|
```bash
|
|
python main.py config.txt input.txt
|
|
```
|
|
|
|
produces:
|
|
|
|
```
|
|
b#sjxf_znsyybvo! 6 symbols replaced
|
|
dzjf#zemfzoxfnz% 5
|
|
jgreyk6hnze 3
|
|
xnhjrfyjvth3nxr 1
|
|
```
|
|
|
|
(annotations added here for clarity; the program prints the lines only)
|
|
|
|
## Design decisions
|
|
|
|
The assignment leaves a few points open. The choices made here are:
|
|
|
|
**"Total number of symbols replaced" counts source symbols.** A line where
|
|
`bb` and `a` were both replaced counts 3, being the combined length of the
|
|
matched `value1` strings, not the number of replacements and not the length of
|
|
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
|
|
contains both `b` and `bb`, the input `bbbb` is read as two `bb` matches rather
|
|
than four `b` matches. The assignment guarantees unique values but not that
|
|
none is a prefix of another, so `prepare_pairs` orders the pairs by descending
|
|
length once, and `replace_line` takes the first match at each position. Without
|
|
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
|
|
replacement count and Python's sort is stable, so lines replaced an equal
|
|
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
|
|
lines but also to output the resulting text. Dropping lines that happened to
|
|
match nothing would silently lose content, so every line is printed and lines
|
|
with zero replacements simply sort last.
|
|
|
|
**Input is preserved apart from the line terminator.** Both `\n` and `\r\n` are
|
|
stripped; every other character is kept and eligible for replacement. The
|
|
configuration format has no quoting either, so whitespace inside a value is
|
|
significant — the trailing space in `a=z ` is part of the replacement value.
|
|
|
|
## Error handling
|
|
|
|
The configuration file is validated while loading: a line without `=` is
|
|
rejected, reporting the offending line number. 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
|
|
|
|
```bash
|
|
pip install -r requirements-dev.txt
|
|
pytest
|
|
```
|
|
|
|
- `tests/unit/` covers configuration parsing and the replacement algorithm,
|
|
including the overlapping-pattern and empty-input edge cases, 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
|
|
what unit tests cannot: argument handling, exit status, stderr messages, and
|
|
the sort order as actually printed.
|
|
|
|
## Layout
|
|
|
|
```
|
|
main.py command line entry point, sorting and output
|
|
config_loader.py configuration file parsing and validation
|
|
replacer.py replacement algorithm
|
|
tests/ unit and integration tests
|
|
```
|