feat: добавлена поддержка логирования при трансформации

This commit is contained in:
ElectronixTM
2025-03-16 02:40:06 +03:00
parent 90f7569d7a
commit 53260b1168

View File

@ -33,6 +33,49 @@ class Rulebook:
"amount by setting class variable MAX_DEPTH,"
" but may be something wrong with your input")
def logged_transform(self, string: str) -> tuple[str | None, str]:
"""
Does exactly the same thing as transform, but logging the whole
process, so it can be used to create reports
NOTE: It's literally COPY + PASTE in it's worst, but I don't like
the idea of splitting anything apart, because of the specificity
of the task
Returns the result of transformations and full report
in str respectively
"""
CENTER_WIDTH: int = 30
log = "НАЧАЛО ЛИСТИНГА".center(CENTER_WIDTH, "=") + '\n'
log += self._log_rules()
log += f"Ввод: \"{string}\"\n"
for _ in range(self.MAX_DEPTH):
log += string + "\n"
rule = self.find_appropriate_rule(string)
if rule is None:
log += "NO_MORE_RULES".center(CENTER_WIDTH, "_") + '\n'
return string, log
log += " " * string.find(rule.operand) + self._log_rule(rule) + '\n'
string = self._apply_rule(rule, string)
if rule.is_blocking:
log += string + '\n'
log += "FINAL_RULE".center(CENTER_WIDTH, "_") + '\n'
return string, log
# raise ValueError("The amount of transformations exceeded "
# f"{self.MAX_DEPTH}. You can change maximum "
# "amount by setting class variable MAX_DEPTH,"
# " but may be something wrong with your input")
def _log_rule(self, rule) -> str:
arrow = "->|" if rule.is_blocking else "->"
return f"{rule.operand} {arrow} {rule.target}"
def _log_rules(self) -> str:
log = ""
for num, rule in enumerate(self.rules, start=1):
#log += f"\t{num} : {rule.operand} {arrow} {rule.target}\n"
log += " " * 4 + f"{num} : " + self._log_rule(rule) + "\n"
return log
def find_appropriate_rule(self, string) -> Rule | None:
"""