Initial implementation with unit and integration tests
Reads value1=value2 pairs from a configuration file and replaces every non-overlapping occurrence of value1 with value2 in a text file, printing the resulting lines sorted by the number of symbols replaced. Overlapping patterns resolve to the longest match so the result does not depend on configuration file ordering. Ties keep their original file order.
This commit is contained in:
23
config_loader.py
Normal file
23
config_loader.py
Normal file
@@ -0,0 +1,23 @@
|
||||
"""Configuration file parsing."""
|
||||
|
||||
|
||||
def load_config(filename: str) -> list[tuple[str, str]]:
|
||||
"""Load value1=value2 pairs from the configuration file."""
|
||||
pairs = []
|
||||
|
||||
with open(filename, "r", encoding="utf-8") as file:
|
||||
for line_number, line in enumerate(file, start=1):
|
||||
line = line.rstrip("\n\r")
|
||||
|
||||
if not line:
|
||||
continue
|
||||
|
||||
if "=" not in line:
|
||||
raise ValueError(
|
||||
f"Invalid configuration at line {line_number}: {line!r}"
|
||||
)
|
||||
|
||||
value1, value2 = line.split("=", 1)
|
||||
pairs.append((value1, value2))
|
||||
|
||||
return pairs
|
||||
Reference in New Issue
Block a user