From cdf977f0db6b9884b0e07a41423f2c3f7fd697b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Tue, 21 Nov 2023 22:56:22 +0100 Subject: [PATCH] whitespace handling changes --- api/advent22_api/core/config.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/api/advent22_api/core/config.py b/api/advent22_api/core/config.py index d1cdb1a..ebc1ac9 100644 --- a/api/advent22_api/core/config.py +++ b/api/advent22_api/core/config.py @@ -23,8 +23,11 @@ class TransformedString(BaseModel): # Leerzeichen an Anfang und Ende entfernen STRIP = "STRIP" + # whitespace durch Leerzeichen ersetzen + SPACE = "SPACE" + # whitespace entfernen - IGNORE = "IGNORE" + REMOVE = "REMOVE" # special chars @@ -43,7 +46,7 @@ class TransformedString(BaseModel): value: str - whitespace: __Whitespace = __Whitespace.IGNORE + whitespace: __Whitespace = __Whitespace.REMOVE case: __Case = __Case.UPPER @field_validator("whitespace", "case", mode="before") @@ -54,21 +57,24 @@ class TransformedString(BaseModel): def clean(self) -> str: result = self.value - # Whitespace bearbeiten - if self.whitespace is self.__Whitespace.STRIP: + # Whitespace verarbeiten + if self.whitespace is TransformedString.__Whitespace.STRIP: result = result.strip() - elif self.whitespace is self.__Whitespace.IGNORE: + elif self.whitespace is TransformedString.__Whitespace.SPACE: + result = re.sub(string=result, pattern=r"\s+", repl=" ") + + elif self.whitespace is TransformedString.__Whitespace.REMOVE: result = re.sub(string=result, pattern=r"\s+", repl="") # Groß-/Kleinschreibung verarbeiten - if self.case is self.__Case.UPPER: + if self.case is TransformedString.__Case.UPPER: result = result.upper() - elif self.case is self.__Case.LOWER: + elif self.case is TransformedString.__Case.LOWER: result = result.lower() - elif self.case is self.__Case.RANDOM: + elif self.case is TransformedString.__Case.RANDOM: rnd = Random(self.value) def randomcase(c: str) -> str: