-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import enum | ||
from functools import total_ordering | ||
|
||
|
||
@total_ordering | ||
@enum.unique | ||
class BaseUniqueSortedEnum(enum.Enum): | ||
"""Base unique enum class with ordering.""" | ||
|
||
def __new__(cls, *args, **kwargs): | ||
obj = object.__new__(cls) | ||
obj.index = len(cls.__members__) + 1 | ||
return obj | ||
|
||
def __hash__(self) -> int: | ||
return hash( | ||
f"{self.__module__}_{self.__class__.__name__}_{self.name}_{self.value}" | ||
) | ||
|
||
def __eq__(self, other) -> bool: | ||
self._check_type(other) | ||
return super().__eq__(other) | ||
|
||
def __lt__(self, other) -> bool: | ||
self._check_type(other) | ||
return self.index < other.index | ||
|
||
def _check_type(self, other) -> None: | ||
if type(self) != type(other): | ||
raise TypeError(f"Different types of Enum: {self} != {other}") | ||
|
||
|
||
class Dog(BaseUniqueSortedEnum): | ||
BLOODHOUND = "BLOODHOUND" | ||
WEIMARANER = "WEIMARANER" | ||
SAME = "SAME" | ||
|
||
|
||
class Cat(BaseUniqueSortedEnum): | ||
BRITISH = "BRITISH" | ||
SCOTTISH = "SCOTTISH" | ||
SAME = "SAME" | ||
|
||
|
||
assert Dog.BLOODHOUND < Dog.WEIMARANER | ||
assert Dog.BLOODHOUND <= Dog.WEIMARANER | ||
assert Dog.BLOODHOUND != Dog.WEIMARANER | ||
assert Dog.BLOODHOUND == Dog.BLOODHOUND | ||
assert Dog.WEIMARANER == Dog.WEIMARANER | ||
assert Dog.WEIMARANER > Dog.BLOODHOUND | ||
assert Dog.WEIMARANER >= Dog.BLOODHOUND | ||
|
||
assert Cat.BRITISH < Cat.SCOTTISH | ||
assert Cat.BRITISH <= Cat.SCOTTISH | ||
assert Cat.BRITISH != Cat.SCOTTISH | ||
assert Cat.BRITISH == Cat.BRITISH | ||
assert Cat.SCOTTISH == Cat.SCOTTISH | ||
assert Cat.SCOTTISH > Cat.BRITISH | ||
assert Cat.SCOTTISH >= Cat.BRITISH | ||
|
||
assert hash(Dog.BLOODHOUND) == hash(Dog.BLOODHOUND) | ||
assert hash(Dog.WEIMARANER) == hash(Dog.WEIMARANER) | ||
assert hash(Dog.BLOODHOUND) != hash(Dog.WEIMARANER) | ||
assert hash(Dog.SAME) != hash(Cat.SAME) | ||
|
||
# raise TypeError | ||
Dog.SAME <= Cat.SAME | ||
Dog.SAME < Cat.SAME | ||
Dog.SAME > Cat.SAME | ||
Dog.SAME >= Cat.SAME | ||
Dog.SAME != Cat.SAME |