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
4621e695
Commit
4621e695
authored
4 years ago
by
Piotr Maślanka
Browse files
Options
Downloads
Patches
Plain Diff
fixed `AlreadySeen` to support non-hashable objects
parent
72c98c73
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
CHANGELOG.md
+2
-0
2 additions, 0 deletions
CHANGELOG.md
satella/coding/sequences/iterators.py
+26
-4
26 additions, 4 deletions
satella/coding/sequences/iterators.py
with
28 additions
and
4 deletions
CHANGELOG.md
+
2
−
0
View file @
4621e695
# v2.14.36
*
fixed
`AlreadySeen`
to support non-hashable objects
This diff is collapsed.
Click to expand it.
satella/coding/sequences/iterators.py
+
26
−
4
View file @
4621e695
...
...
@@ -185,12 +185,21 @@ class ConstruableIterator:
class
AlreadySeen
(
tp
.
Generic
[
K
]):
"""
Class to filter out unique objects
Class to filter out unique objects. Objects must be hashable, barring that
they must be eq-able, however passing it an non-hashable object will result in
O(n^2) complexity, as the class uses a list to keep track of the objects.
Usage:
>>>
als
=
AlreadySeen
()
>>>
for
elem
in
sequence
:
>>>
if
als
.
is_unique
(
elem
):
>>>
...
process
the
element
...
"""
__slots__
=
(
'
set
'
,)
def
__init__
(
self
):
self
.
set
=
set
()
self
.
set
=
None
# type: tp.Union[list,
set
]
def
is_unique
(
self
,
key
:
K
)
->
bool
:
"""
...
...
@@ -202,9 +211,22 @@ class AlreadySeen(tp.Generic[K]):
:return: whether the element was seen for the first time
"""
try
:
return
key
not
in
self
.
set
finally
:
hash
(
key
)
if
self
.
set
is
None
:
self
.
set
=
set
()
if
key
in
self
.
set
:
return
False
self
.
set
.
add
(
key
)
return
True
except
TypeError
:
warnings
.
warn
(
'
Passed argument is not hashable, you pay with
'
'
O(n^2) complexity!
'
,
RuntimeWarning
)
if
self
.
set
is
None
:
self
.
set
=
[]
if
key
in
self
.
set
:
return
False
self
.
set
.
append
(
key
)
return
True
def
unique
(
lst
:
Iteratable
)
->
tp
.
Iterator
[
T
]:
...
...
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