Skip to content
Snippets Groups Projects
Commit 857e1e7d authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

v2.8.18 - added HashableIntEnum

parent 18954f64
No related branches found
No related tags found
No related merge requests found
# v2.8.18
* add `HashableIntEnum`
* made `ComparableIntEnum` inherit from `HashableIntEnum`
* put those two in the docs
......@@ -82,6 +82,23 @@ If you need to provide a quick __repr__ for your classes:
.. autoclass:: satella.coding.structures.ReprableMixin
:members:
HashableIntEnum
===============
An enum.IntEnum that can be __hash__ed
.. autoclass:: satella.coding.structures.HashableIntEnum
:members:
ComparableIntEnum
=================
An enum.IntEnum that you can compare by it's values
.. autoclass:: satella.coding.structures.ComparableIntEnum
:members:
==========
Singletons
==========
......
......@@ -4,7 +4,8 @@ from .dictionaries import DictObject, apply_dict_object, DictionaryView, TwoWayD
from .hashable_objects import HashableWrapper
from .heaps import Heap, SetHeap, TimeBasedHeap, TimeBasedSetHeap
from .immutable import Immutable, frozendict
from .mixins import OmniHashableMixin, ReprableMixin, StrEqHashableMixin, ComparableIntEnum
from .mixins import OmniHashableMixin, ReprableMixin, StrEqHashableMixin, ComparableIntEnum, \
HashableIntEnum
from .proxy import Proxy
from .ranking import Ranking
from .singleton import Singleton, SingletonWithRegardsTo
......@@ -18,6 +19,7 @@ __all__ = [
'ReprableMixin',
'StrEqHashableMixin',
'ComparableIntEnum',
'HashableIntEnum',
'DirtyDict',
'SortedList',
'SelfCleaningDefaultDict',
......
......@@ -5,11 +5,25 @@ import typing as tp
from abc import ABCMeta, abstractmethod
class ComparableIntEnum(enum.IntEnum):
class HashableIntEnum(enum.IntEnum):
"""
An enum.IntEnum that implements comparision, stemming from it's values
An enum.IntEnum that implements hashability, stemming from it's values, as well
as hashability
"""
def __hash__(self) -> int:
return hash(self.value)
class ComparableIntEnum(HashableIntEnum):
"""
An enum.IntEnum that implements comparision, stemming from it's values, as well
as hashability
"""
def __hash__(self) -> int:
return hash(self.value)
def __lt__(self, other: 'ComparableIntEnum') -> bool:
return self.value < other.value
......
......@@ -11,11 +11,18 @@ from satella.coding.structures import TimeBasedHeap, Heap, typednamedtuple, \
OmniHashableMixin, DictObject, apply_dict_object, Immutable, frozendict, SetHeap, \
DictionaryView, HashableWrapper, TwoWayDictionary, Ranking, SortedList, SliceableDeque, \
DirtyDict, KeyAwareDefaultDict, Proxy, ReprableMixin, TimeBasedSetHeap, ExpiringEntryDict, SelfCleaningDefaultDict, \
CacheDict, StrEqHashableMixin, ComparableIntEnum
CacheDict, StrEqHashableMixin, ComparableIntEnum, HashableIntEnum
class TestMisc(unittest.TestCase):
def test_hashable_int_enum(self):
class A(HashableIntEnum):
A = 0
B = 1
self.assertEqual({A.A: 't', A.B: 'c'}[A.A], 't')
def test_comparable_int_enum(self):
class Enum(ComparableIntEnum):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment