Skip to content

Commit 4d1a80a

Browse files
committed
Reverting changes to partitions
1 parent a77519f commit 4d1a80a

File tree

2 files changed

+15
-71
lines changed

2 files changed

+15
-71
lines changed

source_py3/python_toolbox/sequence_tools/misc.py

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,20 @@ def partitions(sequence, partition_size=None, *, n_partitions=None,
5757
allow_remainder=True, larger_on_remainder=False,
5858
fill_value=NO_FILL_VALUE):
5959
'''
60-
Break `sequence` into partitions.
61-
62-
Partitions `sequence` into partitions of size `partition_size`, or
60+
Partition `sequence` into equal partitions of size `partition_size`, or
6361
determine size automatically given the number of partitions as
6462
`n_partitions`.
6563
66-
This function attempts to break the sequence into partitions with an equal
67-
number of items; if the sequence can't be divided into precisely equal
68-
partitions, the first partitions will have more elements then the
69-
partitions that come after them.
64+
If the sequence can't be divided into precisely equal partitions, the last
65+
partition will contain less members than all the other partitions.
7066
7167
Example:
7268
7369
>>> partitions([0, 1, 2, 3, 4], 2)
7470
[[0, 1], [2, 3], [4]]
7571
76-
You need to give *either* a `partition_size` *or* an `n_partitions`
77-
argument, not both.
72+
(You need to give *either* a `partition_size` *or* an `n_partitions`
73+
argument, not both.)
7874
7975
Specify `allow_remainder=False` to enforce that the all the partition sizes
8076
be equal; if there's a remainder while `allow_remainder=False`, an
@@ -99,14 +95,10 @@ def partitions(sequence, partition_size=None, *, n_partitions=None,
9995
[[0, 1, 2], [3, 4, 'meow']]
10096
10197
'''
102-
103-
from python_toolbox import cute_iter_tools
10498

10599
sequence = ensure_iterable_is_sequence(sequence)
106100

107101
sequence_length = len(sequence)
108-
if 0 in (n_partitions, partition_size):
109-
raise ValueError
110102

111103
### Validating input: #####################################################
112104
# #
@@ -141,25 +133,23 @@ def partitions(sequence, partition_size=None, *, n_partitions=None,
141133

142134
naive_length = partition_size * n_partitions
143135

144-
cutoff_indices = list(range(0, sequence_length, partition_size)) + [None]
136+
blocks = [sequence[i : i + partition_size] for i in
137+
range(0, naive_length, partition_size)]
145138

146139
if naive_length != sequence_length:
147-
assert cutoff_indices
140+
assert blocks
148141
if larger_on_remainder:
149-
if len(cutoff_indices) >= 3:
150-
del blocks[-2]
151-
elif fill_value is not NO_FILL_VALUE: # (We use elif because fill is
152-
# never done if
153-
# `larger_on_remainder=True`.)
142+
if len(blocks) >= 2:
143+
small_block_to_append_back = blocks[-1]
144+
del blocks[-1]
145+
blocks[-1] += small_block_to_append_back
146+
elif fill_value != NO_FILL_VALUE: # (We use elif because fill is never
147+
# done if `larger_on_remainder=True`.)
154148
filler = itertools.repeat(fill_value,
155149
naive_length - sequence_length)
156150
blocks[-1].extend(filler)
157151

158-
return [
159-
sequence[cutoff_left:cutoff_right] for (cutoff_left, cutoff_right)
160-
in cute_iter_tools.iterate_overlapping_subsequences(cutoff_indices)
161-
]
162-
152+
return blocks
163153

164154

165155
def is_immutable_sequence(thing):

source_py3/test_python_toolbox/test_sequence_tools/test_partitions.py

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -102,49 +102,3 @@ def test_fill_value():
102102
assert partitions(r, 3, fill_value=None) == [[0, 1, 2], [3, 4, None]]
103103
assert partitions([], 3, fill_value=None) == []
104104

105-
106-
def test_different_types():
107-
sequences = (
108-
list(range(5)),
109-
tuple(range(5)),
110-
range(5), #blocktodo on py2 ensure it's xrange
111-
)
112-
113-
for sequence in sequences:
114-
assert partitions(sequence, partition_size=2) == [
115-
sequence[:2], sequence[2:4], sequence[4:]
116-
]
117-
assert partitions(sequence, partition_size=2,
118-
larger_on_remainder=True) == [
119-
sequence[:2], sequence[2:]
120-
]
121-
122-
def test_n_partitions():
123-
r = list(range(10))
124-
with cute_testing.RaiseAssertor(ValueError):
125-
partitions(r, n_partitions=0)
126-
assert partitions(r, n_partitions=1) == [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]
127-
assert partitions(r, n_partitions=2) == [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]
128-
assert partitions(r, n_partitions=3) == \
129-
[[0, 1, 2, 3], [4, 5, 6,], [7, 8, 9]]
130-
assert partitions(r, n_partitions=4) == \
131-
[[0, 1, 2], [3, 4, 5], [6, 7], [8, 9]]
132-
assert partitions(r, n_partitions=5) == \
133-
[[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]]
134-
assert partitions(r, n_partitions=6) == \
135-
[[0, 1], [2, 3], [4, 5], [6, 7], [8], [9]]
136-
assert partitions(r, n_partitions=7) == \
137-
[[0, 1], [2, 3], [4, 5], [6], [7], [8], [9]]
138-
assert partitions(r, n_partitions=8) == \
139-
[[0, 1], [2, 3], [4], [5], [6], [7], [8], [9]]
140-
assert partitions(r, n_partitions=9) == \
141-
[[0, 1], [2], [3], [4], [5], [6], [7], [8], [9]]
142-
assert partitions(r, n_partitions=10) == \
143-
[[0], [1], [2], [3], [4], [5], [6], [7], [8], [9]]
144-
assert partitions(r, n_partitions=11) == \
145-
[[0], [1], [2], [3], [4], [5], [6], [7], [8], [9], []]
146-
assert partitions(r, n_partitions=12) == \
147-
[[0], [1], [2], [3], [4], [5], [6], [7], [8], [9], [], []]
148-
149-
# blocktodo: add same batch of tests but with larger_on_remainder
150-

0 commit comments

Comments
 (0)