Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
swh-core
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
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
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Platform
Development
swh-core
Commits
aeac32db
Verified
Commit
aeac32db
authored
6 years ago
by
Antoine R. Dumont
Browse files
Options
Downloads
Patches
Plain Diff
utils.grouper: Rename fillvalue to stop_value and fix docstring
parent
ddecea09
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!25
utils.grouper: Improve implementation
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
swh/core/tests/test_utils.py
+3
-3
3 additions, 3 deletions
swh/core/tests/test_utils.py
swh/core/utils.py
+17
-10
17 additions, 10 deletions
swh/core/utils.py
with
20 additions
and
13 deletions
swh/core/tests/test_utils.py
+
3
−
3
View file @
aeac32db
...
...
@@ -29,10 +29,10 @@ class UtilsLib(unittest.TestCase):
self
.
assertEqual
(
out
,
[[
9
,
8
,
7
,
6
],
[
5
,
4
,
3
,
2
],
[
1
]])
def
test_grouper_with_
fill
value
(
self
):
def
test_grouper_with_
stop_
value
(
self
):
# given
actual_data
=
utils
.
grouper
(((
i
,
i
+
1
)
for
i
in
range
(
0
,
9
)),
2
,
fill
value
=
(
None
,
None
))
stop_
value
=
(
None
,
None
))
out
=
[]
for
d
in
actual_data
:
...
...
@@ -47,7 +47,7 @@ class UtilsLib(unittest.TestCase):
# given
actual_data
=
utils
.
grouper
((
i
for
i
in
range
(
9
,
0
,
-
1
)),
4
,
fill
value
=
'
a
'
)
stop_
value
=
'
a
'
)
out
=
[]
for
d
in
actual_data
:
...
...
This diff is collapsed.
Click to expand it.
swh/core/utils.py
+
17
−
10
View file @
aeac32db
...
...
@@ -25,24 +25,31 @@ def cwd(path):
os
.
chdir
(
prev_cwd
)
def
grouper
(
iterable
,
n
,
fillvalue
=
None
):
"""
Collect data into fixed-length chunks or blocks.
def
grouper
(
iterable
,
n
,
stop_value
=
None
):
"""
Collect data into fixed-length size iterables. The last block might
contain less elements as it will hold only the remaining number
of elements.
The invariant here is that the number of elements in the input
iterable and the sum of the number of elements of all iterables
generated from this function should be equal.
Args:
iterable (Iterable): an iterable
n (int): size of block to slice the iterable into
fill
value (Optional[Something]): value to use as
fill-in
values (typically for the last loop, the
iterable might be
less than n
elements
)
. None by default but could be anything
relevant for
the caller (e.g tuple of (None, None))
stop_
value (Optional[Something]): value to use as
stop value
for the last iterable. That
iterable might be
less than n
elements. None by default but could be anything
relevant for
the caller (e.g tuple of (None, None))
Returns:
fixed-length chunks of blocks as iterables
Yields:
fixed-length blocks as iterables. As mentioned, the last
iterable might be less populated.
"""
args
=
[
iter
(
iterable
)]
*
n
for
_data
in
itertools
.
zip_longest
(
*
args
,
fillvalue
=
fill
value
):
yield
(
d
for
d
in
_data
if
d
is
not
fill
value
)
for
_data
in
itertools
.
zip_longest
(
*
args
,
fillvalue
=
stop_
value
):
yield
(
d
for
d
in
_data
if
d
is
not
stop_
value
)
def
backslashescape_errors
(
exception
):
...
...
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