Skip to content

Commit

Permalink
feat(enum): add StringEnum to core (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
portellaa authored Dec 19, 2022
1 parent 1876125 commit e2f0fa9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
prospector==1.7.*
pylint==2.15.6
pytest==7.1.*
30 changes: 30 additions & 0 deletions src/core/ydata/core/enum/string_enum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from enum import Enum


class StringEnum(Enum):
@classmethod
def _missing_(cls, value):
if isinstance(value, str):
upper_value = value.upper()

key = StringEnum._key_from_str_(upper_value)
if key is not None:
return key

lower_value = value.lower()

key = StringEnum._key_from_str_(lower_value)
if key is not None:
return key

raise ValueError(f"{value} is not a valid {cls.__name__}")

@classmethod
def _key_from_str_(cls, value: str):
if value in cls.__members__:
return cls(value)

return None

def __eq__(self, o: object) -> bool:
return self.value == o.value

0 comments on commit e2f0fa9

Please sign in to comment.