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

add StrEqHashableMixin

parent ac8c4b67
No related branches found
No related tags found
No related merge requests found
......@@ -3,3 +3,4 @@
* added for_argument, attach_arguments and auto_adapt_to_methods to __all__ in `satella.coding`
* added time_us to __all__ in `satella.time`
* refactored `satella.cassandra.parallel_for`
* added `StrEqHashableMixin`
......@@ -58,6 +58,16 @@ If you need quick __hash__ and __eq__ operators from listed fields of the class.
.. autoclass:: satella.coding.structures.OmniHashableMixin
:members:
StrEqHashableMixin
==================
A class that outfits your class with __eq__ and __hash__ based off the str() value of the
class. So you got to define __str__ at the very least!
.. autoclass:: satella.coding.structures.StrEqHashableMixin
:members:
ReprableMixin
=============
......
__version__ = '2.8.15_a6'
__version__ = '2.8.15_a7'
......@@ -4,7 +4,7 @@ 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
from .mixins import OmniHashableMixin, ReprableMixin, StrEqHashableMixin
from .proxy import Proxy
from .ranking import Ranking
from .singleton import Singleton, SingletonWithRegardsTo
......@@ -16,6 +16,7 @@ __all__ = [
'KeyAwareDefaultDict',
'Proxy',
'ReprableMixin',
'StrEqHashableMixin',
'DirtyDict',
'SortedList',
'SelfCleaningDefaultDict',
......
......@@ -4,6 +4,18 @@ import typing as tp
from abc import ABCMeta, abstractmethod
class StrEqHashableMixin:
"""
A mix-in that outfits your class with an __eq__ and __hash__ operator that take
their values from __str__ representation of your class.
"""
def __eq__(self, other):
return str(self) == str(other)
def __hash__(self) -> int:
return hash(str(self))
class OmniHashableMixin(metaclass=ABCMeta):
"""
A mix-in. Provides hashing and equal comparison for your own class using specified fields.
......
......@@ -11,11 +11,23 @@ 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
CacheDict, StrEqHashableMixin
class TestMisc(unittest.TestCase):
def test_str_eq_hashable_mixin(self):
class MyClass(StrEqHashableMixin):
def __init__(self, v: str):
self.v = v
def __str__(self) -> str:
return self.v
a = MyClass('a')
self.assertEqual(hash('a'), hash(a))
self.assertEqual(a, 'a')
def test_proxy(self):
a = Proxy(2)
self.assertEqual(int(a), 2)
......
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