From 53260b116805f28b7a581d0150092e89cf9dc818 Mon Sep 17 00:00:00 2001 From: ElectronixTM Date: Sun, 16 Mar 2025 02:40:06 +0300 Subject: [PATCH] =?UTF-8?q?feat:=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=BF=D0=BE=D0=B4=D0=B4=D0=B5=D1=80=D0=B6?= =?UTF-8?q?=D0=BA=D0=B0=20=D0=BB=D0=BE=D0=B3=D0=B8=D1=80=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D0=BF=D1=80=D0=B8=20=D1=82=D1=80=D0=B0?= =?UTF-8?q?=D0=BD=D1=81=D1=84=D0=BE=D1=80=D0=BC=D0=B0=D1=86=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/rulebook.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/rulebook.py b/src/rulebook.py index 8a202ee..aa07d26 100644 --- a/src/rulebook.py +++ b/src/rulebook.py @@ -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: """