summaryrefslogtreecommitdiff
path: root/robusta_krr/utils
diff options
context:
space:
mode:
authorПавел Жуков <33721692+LeaveMyYard@users.noreply.github.com>2023-04-10 18:31:50 +0300
committerПавел Жуков <33721692+LeaveMyYard@users.noreply.github.com>2023-04-10 18:31:50 +0300
commitab04b499210e07c9b1ad4c55e1cf98ecaef801e0 (patch)
tree54ff66e46248c9d40b83d45e5f8ceaaa288241a3 /robusta_krr/utils
parent43618112e45953528cde593275f755881f852d67 (diff)
Improve simple strategy, automate __display_name__
Diffstat (limited to 'robusta_krr/utils')
-rw-r--r--robusta_krr/utils/display_name.py20
1 files changed, 20 insertions, 0 deletions
diff --git a/robusta_krr/utils/display_name.py b/robusta_krr/utils/display_name.py
new file mode 100644
index 0000000..3cc89ce
--- /dev/null
+++ b/robusta_krr/utils/display_name.py
@@ -0,0 +1,20 @@
+from typing import Callable, TypeVar
+
+_T = TypeVar("_T")
+
+
+def add_display_name(*, postfix: str) -> Callable[[type[_T]], type[_T]]:
+ """Add a decorator factory to add __display_name__ property to the class."""
+
+ def decorator(cls: type[_T]) -> type[_T]:
+ class DisplayNameProperty:
+ def __get__(self, instance, owner):
+ if owner.__name__.lower().endswith(postfix.lower()):
+ return owner.__name__[: -len(postfix)]
+
+ return owner.__name__
+
+ cls.__display_name__ = DisplayNameProperty()
+ return cls
+
+ return decorator