aboutsummaryrefslogtreecommitdiff
path: root/poker/utils/enum.py
diff options
context:
space:
mode:
authorPaul Harrison <paul@harrison.sh>2022-11-18 12:59:18 +0000
committerPaul Harrison <paul@harrison.sh>2022-12-15 16:02:14 +0000
commit5efd77728dd5a1c6a2fb5070b981ad4d028c3fe1 (patch)
tree247e31cd5136072964954a00b40a27c0f1968f51 /poker/utils/enum.py
parent3899da2f5270d61fa86b090e35f92323c82a5161 (diff)
feat: Implement auto-naming string enum
Diffstat (limited to 'poker/utils/enum.py')
-rw-r--r--poker/utils/enum.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/poker/utils/enum.py b/poker/utils/enum.py
new file mode 100644
index 0000000..91e83a5
--- /dev/null
+++ b/poker/utils/enum.py
@@ -0,0 +1,27 @@
+from enum import StrEnum
+from typing import Any
+
+
+class AutoName(StrEnum):
+ """Use the lower case name as the value for Python Enum (default would be integers).
+
+ Inherits from str to ensure all types are string and as a bonus it becomes JSON
+ serializable.
+
+ See here for more information:
+ https://docs.python.org/3/library/enum.html#using-automatic-values
+ """
+
+ def __str__(self) -> str:
+ """Return string representation."""
+ return str(self.value)
+
+ @staticmethod
+ def _generate_next_value_(
+ name: str, start: int, count: int, last_values: list[Any]
+ ) -> str:
+ """Enum standard structure - the next value.
+
+ See this on @staticmethod: https://github.com/python/mypy/issues/7591
+ """
+ return name.lower()