Skip to content

Commit b2fe51e

Browse files
[ADD] Move from richcmp to __eq__() (#182)
* [ADD] Move from richcmp->eq * [FIX] Remove unnecessary code
1 parent 772665a commit b2fe51e

3 files changed

Lines changed: 178 additions & 351 deletions

File tree

ConfigSpace/conditions.pyx

Lines changed: 39 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -115,41 +115,27 @@ cdef class AbstractCondition(ConditionComponent):
115115
self.child_vector_id = -1
116116
self.parent_vector_id = -1
117117

118-
def __richcmp__(self, other: Any, int op):
118+
def __eq__(self, other: Any) -> bool:
119119
"""
120-
Todo: With Cython 2.7 this function can be replaced by using the six
121-
Python special methods (__eq__(), __lt__(),...).
122-
(--> https://cython.readthedocs.io/en/latest/src/userguide/special_methods.html\
123-
#rich-comparisons)
124-
125-
Before 2.7 there were no separate methods for the individual rich
126-
comparison operations. Instead there is a single method __richcmp__()
127-
which takes an integer indicating which operation is to be performed,
128-
as follows:
129-
< 0
130-
== 2
131-
> 4
132-
<= 1
133-
!= 3
134-
>= 5
135-
"""
136-
if isinstance(other, self.__class__):
120+
This method implements a comparison between self and another
121+
object.
137122

138-
if op == 2:
139-
if self.child != other.child:
140-
return False
141-
elif self.parent != other.parent:
142-
return False
143-
return self.value == other.value
123+
Additionally, it defines the __ne__() as stated in the
124+
documentation from python:
125+
By default, object implements __eq__() by using is, returning NotImplemented
126+
in the case of a false comparison: True if x is y else NotImplemented.
127+
For __ne__(), by default it delegates to __eq__() and inverts the result
128+
unless it is NotImplemented.
144129

145-
elif op == 3:
146-
if (self.child != other.child or
147-
self.parent != other.parent or self.value != other.value):
148-
return True
149-
else:
150-
return False
130+
"""
131+
if not isinstance(other, self.__class__):
132+
return False
151133

152-
return NotImplemented
134+
if self.child != other.child:
135+
return False
136+
elif self.parent != other.parent:
137+
return False
138+
return self.value == other.value
153139

154140
def set_vector_idx(self, hyperparameter_to_idx: dict):
155141
self.child_vector_id = hyperparameter_to_idx[self.child.name]
@@ -545,42 +531,6 @@ cdef class InCondition(AbstractCondition):
545531
self.value = values
546532
self.vector_values = [self.parent._inverse_transform(value) for value in self.values]
547533

548-
def __richcmp__(self, other: Any, int op):
549-
"""
550-
Todo: With Cython 2.7 this function can be replaced by using the six
551-
Python special methods (__eq__(), __lt__(),...).
552-
(--> https://cython.readthedocs.io/en/latest/src/userguide/special_methods.html\
553-
#rich-comparisons)
554-
555-
Before 2.7 there were no separate methods for the individual rich
556-
comparison operations. Instead there is a single method __richcmp__()
557-
which takes an integer indicating which operation is to be performed,
558-
as follows:
559-
< 0
560-
== 2
561-
> 4
562-
<= 1
563-
!= 3
564-
>= 5
565-
"""
566-
if isinstance(other, self.__class__):
567-
568-
if op == 2:
569-
if self.child != other.child:
570-
return False
571-
elif self.parent != other.parent:
572-
return False
573-
return self.values == other.values
574-
575-
elif op == 3:
576-
if (self.child != other.child or
577-
self.parent != other.parent or self.values != other.values):
578-
return True
579-
else:
580-
return False
581-
582-
return NotImplemented
583-
584534
def __repr__(self) -> str:
585535
return "%s | %s in {%s}" % (self.child.name, self.parent.name,
586536
", ".join(
@@ -618,47 +568,31 @@ cdef class AbstractConjunction(ConditionComponent):
618568
raise ValueError("All Conjunctions and Conditions must have "
619569
"the same child.")
620570

621-
def __richcmp__(self, other: Any, int op):
571+
def __eq__(self, other: Any) -> bool:
622572
"""
623-
Todo: With Cython 2.7 this function can be replaced by using the six
624-
Python special methods (__eq__(), __lt__(),...).
625-
(--> https://cython.readthedocs.io/en/latest/src/userguide/special_methods.html\
626-
#rich-comparisons)
627-
628-
Before 2.7 there were no separate methods for the individual rich
629-
comparison operations. Instead there is a single method __richcmp__()
630-
which takes an integer indicating which operation is to be performed,
631-
as follows:
632-
< 0
633-
== 2
634-
> 4
635-
<= 1
636-
!= 3
637-
>= 5
573+
This method implements a comparison between self and another
574+
object.
575+
576+
Additionally, it defines the __ne__() as stated in the
577+
documentation from python:
578+
By default, object implements __eq__() by using is, returning NotImplemented
579+
in the case of a false comparison: True if x is y else NotImplemented.
580+
For __ne__(), by default it delegates to __eq__() and inverts the result
581+
unless it is NotImplemented.
582+
638583
"""
639-
if isinstance(other, self.__class__):
640-
if len(self.components) != len(other.components):
641-
if op == 2:
642-
return False
643-
if op == 3:
644-
return True
645-
else:
646-
return NotImplemented
647-
648-
for component, other_component in \
649-
zip(self.components, other.components):
650-
eq = component == other_component
651-
if op == 2:
652-
if not eq:
653-
return False
654-
elif op == 3:
655-
if eq:
656-
return False
657-
else:
658-
raise NotImplemented
659-
return True
584+
if not isinstance(other, self.__class__):
585+
return False
586+
587+
if len(self.components) != len(other.components):
588+
return False
589+
590+
for component, other_component in zip(self.components, other.components):
591+
if (component != other_component):
592+
return False
593+
594+
return True
660595

661-
return NotImplemented
662596

663597
def __copy__(self):
664598
return self.__class__(*[copy.copy(comp) for comp in self.components])

ConfigSpace/forbidden.pyx

Lines changed: 35 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -57,39 +57,29 @@ cdef class AbstractForbiddenComponent(object):
5757
def __repr__(self):
5858
pass
5959

60-
def __richcmp__(self, other: Any, int op):
60+
def __eq__(self, other: Any) -> bool:
6161
"""
62-
Todo: With Cython 2.7 this function can be replaced by using the six
63-
Python special methods (__eq__(), __lt__(),...).
64-
(--> https://cython.readthedocs.io/en/latest/src/userguide/special_methods.html\
65-
#rich-comparisons)
66-
67-
Before 2.7 there were no separate methods for the individual rich
68-
comparison operations. Instead there is a single method __richcmp__()
69-
which takes an integer indicating which operation is to be performed,
70-
as follows:
71-
< 0
72-
== 2
73-
> 4
74-
<= 1
75-
!= 3
76-
>= 5
62+
This method implements a comparison between self and another
63+
object.
64+
65+
Additionally, it defines the __ne__() as stated in the
66+
documentation from python:
67+
By default, object implements __eq__() by using is, returning NotImplemented
68+
in the case of a false comparison: True if x is y else NotImplemented.
69+
For __ne__(), by default it delegates to __eq__() and inverts the result
70+
unless it is NotImplemented.
71+
7772
"""
73+
if not isinstance(other, self.__class__):
74+
return False
75+
7876
if self.value is None:
7977
self.value = self.values
78+
if other.value is None:
79+
other.value = other.values
8080

81-
if isinstance(other, self.__class__):
82-
if other.value is None:
83-
other.value = other.values
84-
if op == 2:
85-
return (self.value == other.value and
86-
self.hyperparameter.name == other.hyperparameter.name)
87-
88-
elif op == 3:
89-
return False == (self.value == other.value and
90-
self.hyperparameter.name == other.hyperparameter.name)
91-
92-
return NotImplemented
81+
return (self.value == other.value and
82+
self.hyperparameter.name == other.hyperparameter.name)
9383

9484
def __hash__(self) -> int:
9585
"""Override the default hash behavior (that returns the id or the object)"""
@@ -353,39 +343,28 @@ cdef class AbstractForbiddenConjunction(AbstractForbiddenComponent):
353343
def __copy__(self):
354344
return self.__class__([copy(comp) for comp in self.components])
355345

356-
def __richcmp__(self, other: Any, int op):
346+
def __eq__(self, other: Any) -> bool:
357347
"""
358-
Todo: With Cython 2.7 this function can be replaced by using the six
359-
Python special methods (__eq__(), __lt__(),...).
360-
(--> https://cython.readthedocs.io/en/latest/src/userguide/special_methods.html\
361-
#rich-comparisons)
362-
363-
Before 2.7 there were no separate methods for the individual rich
364-
comparison operations. Instead there is a single method __richcmp__()
365-
which takes an integer indicating which operation is to be performed,
366-
as follows:
367-
< 0
368-
== 2
369-
> 4
370-
<= 1
371-
!= 3
372-
>= 5
348+
This method implements a comparison between self and another
349+
object.
350+
351+
Additionally, it defines the __ne__() as stated in the
352+
documentation from python:
353+
By default, object implements __eq__() by using is, returning NotImplemented
354+
in the case of a false comparison: True if x is y else NotImplemented.
355+
For __ne__(), by default it delegates to __eq__() and inverts the result
356+
unless it is NotImplemented.
373357
"""
374358

375-
if isinstance(other, self.__class__):
376-
if op == 2:
377-
if self.n_components != other.n_components:
378-
return False
379-
return all([self.components[i] == other.components[i]
380-
for i in range(self.n_components)])
359+
if not isinstance(other, self.__class__):
360+
return False
381361

382-
elif op == 3:
383-
if self.n_components == other.n_components:
384-
return False
385-
return any([self.components[i] != other.components[i]
386-
for i in range(self.n_components)])
362+
if self.n_components != other.n_components:
363+
return False
364+
365+
return all([self.components[i] == other.components[i]
366+
for i in range(self.n_components)])
387367

388-
return NotImplemented
389368

390369
cpdef set_vector_idx(self, hyperparameter_to_idx):
391370
for component in self.components:

0 commit comments

Comments
 (0)