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
14c84479
Commit
14c84479
authored
7 years ago
by
Piotr Maślanka
Browse files
Options
Downloads
Patches
Plain Diff
fix
parent
dd7746b0
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.py
+15
-20
15 additions, 20 deletions
satella/coding/structures.py
tests/test_coding/test_structures.py
+5
-3
5 additions, 3 deletions
tests/test_coding/test_structures.py
with
20 additions
and
23 deletions
satella/coding/structures.py
+
15
−
20
View file @
14c84479
...
...
@@ -16,6 +16,9 @@ __all__ = [
'
TimeBasedHeap
'
]
returns_bool
=
typed
(
returns
=
bool
)
returns_iterable
=
typed
(
returns
=
Iterable
)
class
CallableGroup
(
object
):
"""
...
...
@@ -108,15 +111,9 @@ class Heap(object):
__slots__
=
(
'
heap
'
,
)
# this is rather private, plz
# TODO needs tests
@typed
(
object
,
(
None
,
Iterable
))
def
__init__
(
self
,
from_list
=
None
):
if
from_list
is
None
:
self
.
heap
=
[]
else
:
self
.
heap
=
list
(
from_list
)
heapq
.
heapify
(
self
.
heap
)
def
__init__
(
self
,
from_list
=
()):
self
.
heap
=
list
(
from_list
)
heapq
.
heapify
(
self
.
heap
)
@typed
(
object
,
Iterable
)
def
push_many
(
self
,
items
):
...
...
@@ -134,7 +131,7 @@ class Heap(object):
or:
heap.push(4, myobject)
"""
"""
heapq
.
heappush
(
self
.
heap
,
item
)
def
__copie
(
self
,
op
):
...
...
@@ -151,7 +148,6 @@ class Heap(object):
def
__iter__
(
self
):
return
self
.
heap
.
__iter__
()
@typed
(
returns
=
object
)
def
pop
(
self
):
"""
Return smallest element of the heap.
...
...
@@ -170,14 +166,14 @@ class Heap(object):
self
.
heap
=
list
(
self
.
heap
)
if
not
isinstance
(
self
.
heap
,
list
)
else
self
.
heap
heapq
.
heapify
(
self
.
heap
)
@
typed
(
returns
=
bool
)
@returns
_
bool
def
__bool__
(
self
):
"""
Is this empty?
"""
return
len
(
self
.
heap
)
>
0
@
typed
(
returns
=
I
terable
)
@returns
_i
terable
def
iter_ascending
(
self
):
"""
Return an iterator returning all elements in this heap sorted ascending.
...
...
@@ -188,8 +184,8 @@ class Heap(object):
while
cph
:
yield
cph
.
pop
()
@
typed
(
object
,
object
,
returns
=
I
terable
)
def
get
_less_than
(
self
,
less
):
@returns
_i
terable
def
pop
_less_than
(
self
,
less
):
"""
Return all elements less (sharp inequality) than particular value.
...
...
@@ -198,11 +194,11 @@ class Heap(object):
:return: Iterator
"""
while
self
:
if
self
.
heap
[
0
]
<
less
:
if
self
.
heap
[
0
]
>=
less
:
return
yield
self
.
pop
()
@
typed
(
returns
=
I
terable
)
@returns
_i
terable
def
iter_descending
(
self
):
"""
Return an iterator returning all elements in this heap sorted descending.
...
...
@@ -224,7 +220,7 @@ class Heap(object):
def
__repr__
(
self
):
return
u
'
<satella.coding.Heap>
'
@
typed
(
returns
=
bool
)
@returns
_
bool
def
__contains__
(
self
,
item
):
return
item
in
self
.
heap
...
...
@@ -260,7 +256,6 @@ class TimeBasedHeap(Heap):
"""
self
.
push
(
timestamp
,
item
)
@typed
(
None
,
(
float
,
int
),
returns
=
list
)
def
pop_less_than
(
self
,
timestamp
):
"""
Return a list of items with timestamps less than your value.
...
...
@@ -268,7 +263,7 @@ class TimeBasedHeap(Heap):
Items will be removed from heap
:return: list of tuple(timestamp::float, item)
"""
return
list
(
self
.
get
_less_than
(
timestamp
))
return
list
(
self
.
pop
_less_than
(
timestamp
))
def
remove
(
self
,
item
):
"""
...
...
This diff is collapsed.
Click to expand it.
tests/test_coding/test_structures.py
+
5
−
3
View file @
14c84479
...
...
@@ -24,10 +24,12 @@ class TestTimeBasedHeap(unittest.TestCase):
class
TestHeap
(
unittest
.
TestCase
):
def
test_tbh_iter
(
self
):
tbh
=
Heap
([(
10
,
'
ala
'
),
(
20
,
'
ma
'
),
(
30
,
'
kota
'
),
(
5
,
'
yo
'
)])
tbh
=
Heap
()
tb
=
[(
10
,
'
ala
'
),
(
20
,
'
ma
'
),
(
30
,
'
kota
'
),
(
5
,
'
yo
'
)]
self
.
assertEqual
(
[(
5
,
'
yo
'
),
(
10
,
'
ala
'
),
(
20
,
'
ma
'
),
(
30
,
'
kota
'
)]
,
list
(
tbh
.
iter_ascending
()))
self
.
assertEqual
(
reversed
([(
5
,
'
yo
'
),
(
10
,
'
ala
'
),
(
20
,
'
ma
'
),
(
30
,
'
kota
'
)]
),
list
(
tbh
.
iter_descending
()))
self
.
assertEqual
(
sorted
(
tb
)
,
list
(
tbh
.
iter_ascending
()))
self
.
assertEqual
(
sorted
(
tb
,
reverse
=
True
),
list
(
tbh
.
iter_descending
()))
def
test_tbh
(
self
):
...
...
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