Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
satella
Manage
Activity
Members
Labels
Plan
Issues
1
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
public
satella
Commits
87687b31
Commit
87687b31
authored
1 year ago
by
Piotr Maślanka
Browse files
Options
Downloads
Patches
Plain Diff
improve OmniHashableMixin
parent
ddd42940
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
satella/coding/structures/mixins/hashable.py
+20
-32
20 additions, 32 deletions
satella/coding/structures/mixins/hashable.py
tests/test_coding/test_structures.py
+3
-0
3 additions, 0 deletions
tests/test_coding/test_structures.py
with
23 additions
and
32 deletions
satella/coding/structures/mixins/hashable.py
+
20
−
32
View file @
87687b31
import
operator
import
typing
as
tp
from
abc
import
ABCMeta
,
abstractmethod
...
...
@@ -69,7 +70,7 @@ class ComparableAndHashableBy(metaclass=ABCMeta):
class
ComparableAndHashableByInt
(
metaclass
=
ABCMeta
):
"""
A mix-in. Provides comparis
i
on (lt, gt, ge, le, eq) and hashing by __int__ of this class.
A mix-in. Provides comparison (lt, gt, ge, le, eq) and hashing by __int__ of this class.
"""
__slots__
=
()
...
...
@@ -180,40 +181,27 @@ class OmniHashableMixin(metaclass=ABCMeta):
"""
Note that this will only compare _HASH_FIELDS_TO_USE
"""
if
not
isinstance
(
other
,
type
(
self
)):
return
False
if
not
isinstance
(
other
,
OmniHashableMixin
):
return
super
().
__eq__
(
other
)
cmpr_by
=
self
.
_HASH_FIELDS_TO_USE
try
:
if
isinstance
(
cmpr_by
,
str
):
return
getattr
(
self
,
cmpr_by
)
==
getattr
(
other
,
cmpr_by
)
for
field_name
in
self
.
_HASH_FIELDS_TO_USE
:
if
getattr
(
self
,
field_name
)
!=
getattr
(
other
,
field_name
):
return
False
return
True
except
AttributeError
:
return
False
return
_generic_eq
(
self
,
other
,
False
,
operator
.
eq
,
'
eq
'
,
)
def
__ne__
(
self
,
other
)
->
bool
:
if
not
isinstance
(
other
,
type
(
self
)):
return
True
return
_generic_eq
(
self
,
other
,
True
,
operator
.
ne
,
'
ne
'
)
if
not
isinstance
(
other
,
OmniHashableMixin
):
return
super
().
__ne__
(
other
)
cmpr_by
=
self
.
_HASH_FIELDS_TO_USE
def
_generic_eq
(
self
,
other
,
truth
,
comparator
,
name
):
if
not
isinstance
(
other
,
type
(
self
)):
return
truth
if
not
isinstance
(
other
,
OmniHashableMixin
):
return
comparator
(
self
,
other
)
try
:
if
isinstance
(
cmpr_by
,
str
):
return
getattr
(
self
,
cmpr_by
)
!=
getattr
(
other
,
cmpr_by
)
cmpr_by
=
self
.
_HASH_FIELDS_TO_USE
try
:
if
isinstance
(
cmpr_by
,
str
):
return
comparator
(
getattr
(
self
,
cmpr_by
),
getattr
(
other
,
cmpr_by
))
for
field_name
in
cmpr_by
:
if
getattr
(
self
,
field_name
)
!=
getattr
(
other
,
field_name
):
return
T
ru
e
return
False
except
AttributeError
:
return
T
ru
e
for
field_name
in
cmpr_by
:
if
getattr
(
self
,
field_name
)
!=
getattr
(
other
,
field_name
):
return
t
ru
th
return
not
truth
except
AttributeError
:
return
t
ru
th
This diff is collapsed.
Click to expand it.
tests/test_coding/test_structures.py
+
3
−
0
View file @
87687b31
...
...
@@ -273,6 +273,9 @@ class TestStructures(unittest.TestCase):
b
=
MyClass
(
1
)
c
=
MyClass
(
2
)
self
.
assertEqual
(
a
,
b
)
self
.
assertEqual
(
a
,
1
)
self
.
assertGreater
(
a
,
0
)
self
.
assertLess
(
c
,
1
)
self
.
assertNotEqual
(
a
,
c
)
self
.
assertLess
(
a
,
c
)
self
.
assertGreaterEqual
(
a
,
b
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment