From 8449aedbce11b2d0e42ea97bfab2212eeff6bfc1 Mon Sep 17 00:00:00 2001 From: shellhub Date: Mon, 5 Oct 2020 00:02:18 +0800 Subject: [PATCH 1/4] fixed allocation_number --- maths/allocation_number.py | 43 ++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/maths/allocation_number.py b/maths/allocation_number.py index 4e74bb2e6950..77695766aeed 100644 --- a/maths/allocation_number.py +++ b/maths/allocation_number.py @@ -1,29 +1,24 @@ +""" +In a multi-threaded download, this algorithm could be used to provide +each worker thread with a block of non-overlapping bytes to download. +For example: + for i in allocation_list: + requests.get(url,headers={'Range':f'bytes={i}'}) +""" from __future__ import annotations def allocation_num(number_of_bytes: int, partitions: int) -> list[str]: """ Divide a number of bytes into x partitions. + :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 - In a multi-threaded download, this algorithm could be used to provide - each worker thread with a block of non-overlapping bytes to download. - For example: - for i in allocation_list: - requests.get(url,headers={'Range':f'bytes={i}'}) - - parameter - ------------ - : param number_of_bytes - : param partitions - - return - ------------ - : return: list of bytes to be assigned to each worker thread - - Examples: - ------------ >>> allocation_num(16647, 4) - ['0-4161', '4162-8322', '8323-12483', '12484-16647'] + ['1-4161', '4162-8322', '8323-12483', '12484-16647'] + >>> allocation_num(100000, 6) + ['1-16666', '16667-33332', '33333-49998', '49999-66664', '66665-83330', '83331-100000'] >>> allocation_num(888, 888) Traceback (most recent call last): ... @@ -42,13 +37,11 @@ def allocation_num(number_of_bytes: int, partitions: int) -> list[str]: if partitions >= number_of_bytes: raise ValueError("partitions can not >= number_of_bytes!") bytes_per_partition = number_of_bytes // partitions - allocation_list = [f"0-{bytes_per_partition}"] - for i in range(1, partitions - 1): - length = f"{bytes_per_partition * i + 1}-{bytes_per_partition * (i + 1)}" - allocation_list.append(length) - allocation_list.append( - f"{(bytes_per_partition * (partitions - 1)) + 1}-" f"{number_of_bytes}" - ) + 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 From 5d293ca239fb4da4f43e38af4b53193dd32b19cc Mon Sep 17 00:00:00 2001 From: shellhub Date: Mon, 5 Oct 2020 00:11:03 +0800 Subject: [PATCH 2/4] fixed pre-commit --- maths/allocation_number.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/maths/allocation_number.py b/maths/allocation_number.py index 77695766aeed..ef605bf5c7c6 100644 --- a/maths/allocation_number.py +++ b/maths/allocation_number.py @@ -40,7 +40,9 @@ def allocation_num(number_of_bytes: int, partitions: int) -> list[str]: 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 + 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 From 0b53a415fcbde095588d8da0e1bbc6b26af777b3 Mon Sep 17 00:00:00 2001 From: shellhub Date: Mon, 5 Oct 2020 00:16:57 +0800 Subject: [PATCH 3/4] fixed line too long --- maths/allocation_number.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/allocation_number.py b/maths/allocation_number.py index ef605bf5c7c6..6ffeff87935b 100644 --- a/maths/allocation_number.py +++ b/maths/allocation_number.py @@ -17,8 +17,8 @@ def allocation_num(number_of_bytes: int, partitions: int) -> list[str]: >>> allocation_num(16647, 4) ['1-4161', '4162-8322', '8323-12483', '12484-16647'] - >>> allocation_num(100000, 6) - ['1-16666', '16667-33332', '33333-49998', '49999-66664', '66665-83330', '83331-100000'] + >>> allocation_num(50000, 5) + ['1-10000', '10001-20000', '20001-30000', '30001-40000', '40001-50000'] >>> allocation_num(888, 888) Traceback (most recent call last): ... From 8242b6e669bbb7ddb6e61ae0bc470cbe0beb7a12 Mon Sep 17 00:00:00 2001 From: shellhub Date: Mon, 5 Oct 2020 11:23:42 +0800 Subject: [PATCH 4/4] fixed bug --- maths/allocation_number.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/maths/allocation_number.py b/maths/allocation_number.py index 6ffeff87935b..d419e74d01ff 100644 --- a/maths/allocation_number.py +++ b/maths/allocation_number.py @@ -19,14 +19,10 @@ def allocation_num(number_of_bytes: int, partitions: int) -> list[str]: ['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, 888) - Traceback (most recent call last): - ... - ValueError: partitions can not >= number_of_bytes! >>> allocation_num(888, 999) Traceback (most recent call last): ... - ValueError: partitions can not >= number_of_bytes! + ValueError: partitions can not > number_of_bytes! >>> allocation_num(888, -4) Traceback (most recent call last): ... @@ -34,8 +30,8 @@ def allocation_num(number_of_bytes: int, partitions: int) -> list[str]: """ if partitions <= 0: raise ValueError("partitions must be a positive number!") - if partitions >= number_of_bytes: - raise ValueError("partitions can not >= number_of_bytes!") + 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):