From fc51cdea0bb7a9188c90f41d66adcc8b9e0a8cf2 Mon Sep 17 00:00:00 2001 From: Johnson Wang Date: Sun, 22 Dec 2024 02:27:24 -0500 Subject: [PATCH] fix merge for ci from pr-186 --- genesis/datatypes.py | 8 ++++++-- genesis/options/options.py | 8 ++++++-- genesis/repr_base.py | 10 +++++++--- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/genesis/datatypes.py b/genesis/datatypes.py index 48cce0d..8396036 100644 --- a/genesis/datatypes.py +++ b/genesis/datatypes.py @@ -42,10 +42,10 @@ def _repr_elem(self, elem, common_length=0): def _repr_elem_colorized(self, elem, common_length=0): content = self._repr_elem(elem, common_length) idx = content.find(">") - formatted_content = f"{colors.BLUE}{formats.ITALIC}{content[:idx+1]}{formats.RESET}{content[idx+1:]}" + formatted_content = f"{colors.BLUE}{formats.ITALIC}{content[:idx + 1]}{formats.RESET}{content[idx + 1:]}" idx = formatted_content.find(":") if idx >= 0: - formatted_content = f"{formatted_content[:idx]}{colors.GRAY}:{colors.MINT}{formatted_content[idx+1:]}" + formatted_content = f"{formatted_content[:idx]}{colors.GRAY}:{colors.MINT}{formatted_content[idx + 1:]}" formatted_content += formats.RESET return formatted_content @@ -73,6 +73,10 @@ def _repr_brief(self): return repr_str def __repr__(self): + if not __debug__: + self.__colorized__repr__() + + def __colorized__repr__(self): repr_str = f"{colors.BLUE}{self._repr_type()}(len={colors.MINT}{formats.UNDERLINE}{len(self)}{formats.RESET}{colors.BLUE}, [" if len(self) == 0: diff --git a/genesis/options/options.py b/genesis/options/options.py index 746b4fe..cd46cfb 100644 --- a/genesis/options/options.py +++ b/genesis/options/options.py @@ -43,11 +43,15 @@ def copy_attributes_from(self, options, override=False): def _repr_type(cls): return f"<{cls.__module__}.{cls.__qualname__}>".replace("genesis", "gs") - def __repr__(self) -> str: + def __repr__(self): + if not __debug__: + self.__colorized__repr__() + + def __colorized__repr__(self) -> str: property_attrs = self.__dict__.keys() max_attr_len = max([len(attr) for attr in property_attrs]) - repr_str = f"{colors.CORN}{'─'*(max_attr_len + 3)} {formats.BOLD}{formats.ITALIC}{self._repr_type()}{formats.RESET} {colors.CORN}{'─' * (max_attr_len + 3)}\n" + repr_str = f"{colors.CORN}{'─' * (max_attr_len + 3)} {formats.BOLD}{formats.ITALIC}{self._repr_type()}{formats.RESET} {colors.CORN}{'─' * (max_attr_len + 3)}\n" for attr in property_attrs: formatted_str = f"{colors.BLUE}'{attr}'{formats.RESET}" diff --git a/genesis/repr_base.py b/genesis/repr_base.py index 544519d..86f18cf 100644 --- a/genesis/repr_base.py +++ b/genesis/repr_base.py @@ -31,7 +31,11 @@ def _repr_brief(self): repr_str += f", material: {self.material}" return repr_str - def __repr__(self) -> str: + def __repr__(self): + if not __debug__: + self.__colorized__repr__() + + def __colorized__repr__(self) -> str: all_attrs = self.__dir__() property_attrs = [] @@ -55,7 +59,7 @@ def __repr__(self) -> str: continue idx = content.find(">") # format with italic and color - formatted_content = f"{colors.MINT}{formats.ITALIC}{content[:idx+1]}{formats.RESET}{colors.MINT}{content[idx+1:]}{formats.RESET}" + formatted_content = f"{colors.MINT}{formats.ITALIC}{content[:idx + 1]}{formats.RESET}{colors.MINT}{content[idx + 1:]}{formats.RESET}" # in case it's multi-line if isinstance(getattr(self, attr), gs.List): # 4 = 2 x ' + : + space @@ -81,7 +85,7 @@ def __repr__(self) -> str: right_line_len = max(right_line_len, min_line_len) repr_str = ( - f"{colors.CORN}{'─'*left_line_len} {formats.BOLD}{formats.ITALIC}{self._repr_type()}{formats.RESET} {colors.CORN}{'─'*right_line_len}\n" + f"{colors.CORN}{'─' * left_line_len} {formats.BOLD}{formats.ITALIC}{self._repr_type()}{formats.RESET} {colors.CORN}{'─' * right_line_len}\n" + repr_str )