TheAlgorithms/Python · error · ValueError
partitions must be a positive number!
Error message
partitions must be a positive number!
What it means
Raised by allocation_num in maths/allocation_number.py when the partitions argument is zero or negative. The function splits a byte range into 'partitions' contiguous chunks; a non-positive partition count makes the loop and the floor division undefined, so it is rejected first (before the partitions > number_of_bytes check).
Source
Thrown at maths/allocation_number.py:33
:param number_of_bytes: the total of bytes.
:param partitions: the number of partition need to be allocated.
:return: list of bytes to be assigned to each worker thread
>>> allocation_num(16647, 4)
['1-4161', '4162-8322', '8323-12483', '12484-16647']
>>> allocation_num(50000, 5)
['1-10000', '10001-20000', '20001-30000', '30001-40000', '40001-50000']
>>> allocation_num(888, 999)
Traceback (most recent call last):
...
ValueError: partitions can not > number_of_bytes!
>>> allocation_num(888, -4)
Traceback (most recent call last):
...
ValueError: partitions must be a positive number!
"""
if partitions <= 0:
raise ValueError("partitions must be a positive number!")
if partitions > number_of_bytes:
raise ValueError("partitions can not > number_of_bytes!")
bytes_per_partition = number_of_bytes // partitions
allocation_list = []
for i in range(partitions):
start_bytes = i * bytes_per_partition + 1
end_bytes = (
number_of_bytes if i == partitions - 1 else (i + 1) * bytes_per_partition
)
allocation_list.append(f"{start_bytes}-{end_bytes}")
return allocation_list
if __name__ == "__main__":
import doctest
doctest.testmod()
View on GitHub (pinned to f5988cc097)
Solutions
- Validate partitions >= 1 at the call site before invoking allocation_num.
- If partitions is derived (e.g. total // chunk_size), clamp with max(1, ...) or guard chunk_size > total.
- Validate and cast config values to positive ints when loading.
Example fix
# before parts = allocation_num(total, total // chunk_size) # chunk_size > total -> 0 # after partitions = max(1, total // chunk_size) if chunk_size <= total else 1 parts = allocation_num(total, partitions)
Defensive patterns
Strategy: validation
Validate before calling
partitions = int(partitions)
if partitions < 1:
raise ValueError(f'partitions must be >= 1, got {partitions}')
ranges = allocation_num(number_of_bytes, partitions) Prevention
- Clamp derived partition counts with max(1, ...).
- Validate split counts from user config as positive integers.
When it happens
Trigger: Calling allocation_num(888, -4) or allocation_num(50000, 0). Non-integer partitions like 2.5 would not raise here but produce wrong chunking (range() over a float raises TypeError).
Common situations: User-supplied split counts from CLI/config parsed as 0 or negative, computed partition counts like number_of_bytes // chunk_size underflowing to 0 for tiny inputs, or defaults left unset.
Related errors
- partitions can not > number_of_bytes!
- Limit for the Catalan sequence must be ≥ 0
- Number should not be negative.
- Negative arguments are not supported
- the value of input must be a natural number
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/7d91eb9c9c44b1fb.
Report an issue: GitHub.