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

added contains, fix an enum bug

parent 74a93365
No related branches found
No related tags found
No related merge requests found
# v2.14.29
* `ComparableEnum` will now correctly disassemble other enums
* added `contains`
* fixed a bug in `ComparableEnum` that would ValueError instead of returning
False when an unknown element of the enum was compared against it
......@@ -2,6 +2,8 @@
Functions and decorators
========================
.. autofunction:: satella.coding.contains
.. autoclass:: satella.coding.class_or_instancemethod
.. autofunction:: satella.coding.chain_callables
......
__version__ = '2.14.29a2'
__version__ = '2.14.29a3'
......@@ -14,13 +14,13 @@ from .metaclasses import metaclass_maker, wrap_with, dont_wrap, wrap_property, D
CopyDocsFrom
from .misc import update_if_not_none, update_key_if_none, update_attr_if_none, queue_iterator, \
update_key_if_not_none, source_to_function, update_key_if_true, \
get_arguments, call_with_arguments, chain_callables, Closeable
get_arguments, call_with_arguments, chain_callables, Closeable, contains
from .overloading import overload, class_or_instancemethod
from .recast_exceptions import rethrow_as, silence_excs, catch_exception, log_exceptions, \
raises_exception
__all__ = [
'Closeable',
'Closeable', 'contains',
'overload', 'class_or_instancemethod',
'update_if_not_none', 'DocsFromParent', 'update_key_if_none', 'queue_iterator',
'update_attr_if_none', 'update_key_if_not_none', 'source_to_function',
......
......@@ -6,6 +6,31 @@ from queue import Queue
from satella.coding.recast_exceptions import rethrow_as
def contains(needle, haystack) -> bool:
"""
A syntactic sugar for the following:
>>> for item in haystack:
>>> if needle == item:
>>> return True
>>> return False
Note that this is very like Python's in operator, however it's not quite same, since
in doesn't involve the __eq__ operator at every step!
This function for example allows you to circumvent Python's limitations concerning
:class:`~satella.coding.structures.ComparableEnum`
:param needle: needle to check for
:param haystack: haystack to check against
:return: whether haystack contains the element
"""
for item in haystack:
if needle == item:
return True
return False
class Closeable:
"""
A class that needs to clean up it's own resources.
......
......@@ -29,12 +29,15 @@ class ComparableEnum(enum.Enum):
>>> assert A.A == B.A
"""
def __eq__(self, other):
def __eq__(self, other) -> bool:
if isinstance(other, enum.Enum) and not isinstance(other, self.__class__):
other = other.value
if not isinstance(other, self.__class__):
return self.__class__(other) == self
try:
return self.__class__(other) == self
except ValueError: # other is not a correct member of this class!
return False
else:
return super().__eq__(other)
......
......@@ -3,13 +3,23 @@ import gc
import unittest
from satella.coding import update_key_if_not_none, overload, class_or_instancemethod, \
update_key_if_true, get_arguments, call_with_arguments, chain_callables, Closeable
from satella.coding.structures import HashableMixin
update_key_if_true, get_arguments, call_with_arguments, chain_callables, Closeable, \
contains
from satella.coding.structures import HashableMixin, ComparableEnum
from satella.coding.transforms import jsonify, intify
class TestCase(unittest.TestCase):
def test_contains(self):
class CEnum(ComparableEnum):
A = 1
B = 2
a = [CEnum.A, CEnum.B]
self.assertTrue(contains(2, a))
self.assertFalse(contains(3, a))
def test_closeable(self):
a = {'test': False}
......
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