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
2b293727
Commit
2b293727
authored
1 year ago
by
Piotr Maślanka
Browse files
Options
Downloads
Patches
Plain Diff
improve OmniHashableMixin
parent
ddd42940
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
satella/coding/structures/mixins/hashable.py
+19
-31
19 additions, 31 deletions
satella/coding/structures/mixins/hashable.py
with
19 additions
and
31 deletions
satella/coding/structures/mixins/hashable.py
+
19
−
31
View file @
2b293727
import
operator
import
typing
as
tp
import
typing
as
tp
from
abc
import
ABCMeta
,
abstractmethod
from
abc
import
ABCMeta
,
abstractmethod
...
@@ -180,40 +181,27 @@ class OmniHashableMixin(metaclass=ABCMeta):
...
@@ -180,40 +181,27 @@ class OmniHashableMixin(metaclass=ABCMeta):
"""
"""
Note that this will only compare _HASH_FIELDS_TO_USE
Note that this will only compare _HASH_FIELDS_TO_USE
"""
"""
if
not
isinstance
(
other
,
type
(
self
)):
return
_generic_eq
(
self
,
other
,
False
,
operator
.
eq
,
'
eq
'
,
)
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
def
__ne__
(
self
,
other
)
->
bool
:
def
__ne__
(
self
,
other
)
->
bool
:
if
not
isinstance
(
other
,
type
(
self
)):
return
_generic_eq
(
self
,
other
,
True
,
operator
.
ne
,
'
ne
'
)
return
True
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
try
:
if
not
isinstance
(
other
,
OmniHashableMixin
):
if
isinstance
(
cmpr_by
,
str
):
return
getattr
(
super
(),
name
)(
other
)
return
getattr
(
self
,
cmpr_by
)
!=
getattr
(
other
,
cmpr_by
)
for
field_name
in
cmpr_by
:
cmpr_by
=
self
.
_HASH_FIELDS_TO_USE
if
getattr
(
self
,
field_name
)
!=
getattr
(
other
,
field_name
):
try
:
return
True
if
isinstance
(
cmpr_by
,
str
):
return
False
return
comparator
(
getattr
(
self
,
cmpr_by
),
getattr
(
other
,
cmpr_by
))
except
AttributeError
:
return
True
for
field_name
in
self
.
_HASH_FIELDS_TO_USE
:
if
getattr
(
self
,
field_name
)
!=
getattr
(
other
,
field_name
):
return
truth
return
not
truth
except
AttributeError
:
return
truth
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