Skip to content

Commit 83b71c7

Browse files
committed
Polish Amir's changes
1 parent 64a20d7 commit 83b71c7

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

python_toolbox/sequence_tools.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,14 @@ def combinations(sequence, n=None, start=0):
6969
yield [thing] + sub_result
7070

7171

72-
class NoFillValue(object):
73-
'''Don't fill last partition with default fill values.'''
72+
class NO_FILL_VALUE(object):
73+
'''
74+
Sentinel that means: Don't fill last partition with default fill values.
75+
'''
7476

7577

7678
def partitions(sequence, partition_size=None, n_partitions=None,
77-
allow_remainder=True, fillvalue=NoFillValue):
79+
allow_remainder=True, fill_value=NO_FILL_VALUE):
7880
'''
7981
Partition `sequence` into equal partitions of size `partition_size`, or
8082
determine size automatically given the number of partitions as
@@ -96,14 +98,14 @@ def partitions(sequence, partition_size=None, n_partitions=None,
9698
exception will be raised.
9799
98100
If you want the remainder partition to be of equal size with the other
99-
partitions, you can specify `fillvalue` as the padding for the last
100-
partition. A specified value for `fillvalue` implies `allow_remainder=True`
101+
partitions, you can specify `fill_value` as the padding for the last
102+
partition. A specified value for `fill_value` implies `allow_remainder=True`
101103
and will cause an exception to be raised if specified with
102104
`allow_remainder=False`.
103105
104106
Example:
105107
106-
>>> partitions([0, 1, 2, 3, 4], 3, fillvalue=None)
108+
>>> partitions([0, 1, 2, 3, 4], 3, fill_value=None)
107109
[[0, 1, 2], [3, 4, None]]
108110
'''
109111

@@ -115,9 +117,9 @@ def partitions(sequence, partition_size=None, n_partitions=None,
115117
raise Exception('You must specify *either* `partition_size` *or* '
116118
'`n_paritions`.')
117119

118-
if fillvalue != NoFillValue and not allow_remainder:
119-
raise ValueError("fillvalue cannot be specified if allow_remainder "
120-
"is False.")
120+
if fill_value != NO_FILL_VALUE and not allow_remainder:
121+
raise ValueError('`fill_value` cannot be specified if '
122+
'`allow_remainder` is `False`.')
121123

122124
remainder_length = sequence_length % (partition_size if partition_size
123125
is not None else n_partitions)
@@ -138,8 +140,9 @@ def partitions(sequence, partition_size=None, n_partitions=None,
138140
blocks = [sequence[i : i + partition_size] for i in
139141
xrange(0, enlarged_length, partition_size)]
140142

141-
if fillvalue != NoFillValue:
142-
filler = itertools.repeat(fillvalue, enlarged_length - sequence_length)
143+
if fill_value != NO_FILL_VALUE and blocks:
144+
filler = itertools.repeat(fill_value,
145+
enlarged_length - sequence_length)
143146
blocks[-1].extend(filler)
144147

145148
return blocks

test_python_toolbox/test_sequence_tools/test_partitions.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,12 @@ def test_allow_remainder():
6161
partitions(r, n_partitions=5, allow_remainder=False)
6262

6363

64-
def test_fillvalue():
65-
'''Test `fillvalue` keyword arguemnt for `partitions`.'''
64+
def test_fill_value():
65+
'''Test `fill_value` keyword arguemnt for `partitions`.'''
6666
r = range(5)
6767

6868
assert partitions(r, 3) == [[0, 1, 2], [3, 4]]
69-
assert partitions(r, 3, fillvalue=None) == [[0, 1, 2], [3, 4, None]]
70-
with cute_testing.RaiseAssertor(text='fillvalue'):
71-
partitions(r, 2, fillvalue=None, allow_remainder=False)
69+
assert partitions(r, 3, fill_value=None) == [[0, 1, 2], [3, 4, None]]
70+
with cute_testing.RaiseAssertor(text='fill_value'):
71+
partitions(r, 2, fill_value=None, allow_remainder=False)
72+
assert partitions([], 3, fill_value=None) == []

0 commit comments

Comments
 (0)