From 08e66da856a87b249d39829a860df1a2f087f02d Mon Sep 17 00:00:00 2001 From: wind-Lv <2235199513@qq.com> Date: Fri, 20 Mar 2020 13:42:55 +0800 Subject: [PATCH 1/7] 'allocation_content_length' --- maths/allocation_content_length.py | 39 ++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 maths/allocation_content_length.py diff --git a/maths/allocation_content_length.py b/maths/allocation_content_length.py new file mode 100644 index 000000000000..16245206e44b --- /dev/null +++ b/maths/allocation_content_length.py @@ -0,0 +1,39 @@ +#-*- coding:utf-8 -*- + + +def allocation_content_length(content_length, portioning): + """ + >>> content_length = 16647 + >>> portioning = 4 + >>> shares = content_length // portioning + + >>> f'0-{shares*1}' + '0-4161' + >>> f'{shares*1+1}-{shares*2}' + '4162-8322' + >>> f'{shares*2+1}-{shares*3}' + '8323-12483' + >>> f'{shares*3+1}-{shares*4}' + '12484-16647' + """ + + shares = content_length // portioning + + allocation_list = [f'0-{shares*1}'] + for i in range(1,portioning-1): + length = f'{shares*i+1}-{shares*(i+1)}' + allocation_list.append(length) + allocation_list.append(f'{(shares*(portioning-1))+1}-{content_length}') + + return allocation_list + + +def main(): + the_list = allocation_content_length(16647,4) + print(the_list) + + +if __name__ == '__main__': + main() + + From 5eff5b84b058717adeb44abd61c141bb36e8788f Mon Sep 17 00:00:00 2001 From: wind-Lv <2235199513@qq.com> Date: Fri, 20 Mar 2020 16:25:52 +0800 Subject: [PATCH 2/7] 'allocation_number' --- maths/allocation_number.py | 52 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 maths/allocation_number.py diff --git a/maths/allocation_number.py b/maths/allocation_number.py new file mode 100644 index 000000000000..38e2a14e7a20 --- /dev/null +++ b/maths/allocation_number.py @@ -0,0 +1,52 @@ +#-*- coding:utf-8 -*- + + +def allocation_num(num, x) -> int: + """ + Divide the numbers into x parts. + + parameter + ------------ + : param num: the number + : param x: number of copies + + return + ------------ + : return: split completed list + + example + ------------ + >>> allocation_num(16647, 4) + ['0-4161', '4162-8322', '8323-12483', '12484-16647'] + + >>> allocation_num(888, 888) + Traceback (most recent call last): + ... + ValueError: param x can't past or be equal to the param num! + + >>> allocation_num(888, 111) + Traceback (most recent call last): + ... + ValueError: param x can't past or be equal to the param num! + """ + + + if x >= num: + raise ValueError('param x can\'t past or be equal to the param num!') + + shares = num // x + + allocation_list = [f'0-{shares*1}'] + for i in range(1,x-1): + length = f'{shares*i+1}-{shares*(i+1)}' + allocation_list.append(length) + allocation_list.append(f'{(shares*(x-1))+1}-{num}') + + return allocation_list + + +if __name__ == '__main__': + the_list = allocation_num(888,888) + print(the_list) + + From 47a16e7357f13bddb5524bb037955364fa893ad0 Mon Sep 17 00:00:00 2001 From: wind-Lv <61381242+wind-Lv@users.noreply.github.com> Date: Fri, 20 Mar 2020 17:20:19 +0800 Subject: [PATCH 3/7] Delete allocation_content_length.py --- maths/allocation_content_length.py | 39 ------------------------------ 1 file changed, 39 deletions(-) delete mode 100644 maths/allocation_content_length.py diff --git a/maths/allocation_content_length.py b/maths/allocation_content_length.py deleted file mode 100644 index 16245206e44b..000000000000 --- a/maths/allocation_content_length.py +++ /dev/null @@ -1,39 +0,0 @@ -#-*- coding:utf-8 -*- - - -def allocation_content_length(content_length, portioning): - """ - >>> content_length = 16647 - >>> portioning = 4 - >>> shares = content_length // portioning - - >>> f'0-{shares*1}' - '0-4161' - >>> f'{shares*1+1}-{shares*2}' - '4162-8322' - >>> f'{shares*2+1}-{shares*3}' - '8323-12483' - >>> f'{shares*3+1}-{shares*4}' - '12484-16647' - """ - - shares = content_length // portioning - - allocation_list = [f'0-{shares*1}'] - for i in range(1,portioning-1): - length = f'{shares*i+1}-{shares*(i+1)}' - allocation_list.append(length) - allocation_list.append(f'{(shares*(portioning-1))+1}-{content_length}') - - return allocation_list - - -def main(): - the_list = allocation_content_length(16647,4) - print(the_list) - - -if __name__ == '__main__': - main() - - From a2c3f9ca2ede57749638ce578b8a40e8a341b34e Mon Sep 17 00:00:00 2001 From: wind-Lv <61381242+wind-Lv@users.noreply.github.com> Date: Fri, 20 Mar 2020 17:55:13 +0800 Subject: [PATCH 4/7] Update allocation_number.py --- maths/allocation_number.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/maths/allocation_number.py b/maths/allocation_number.py index 38e2a14e7a20..d1031a59ed78 100644 --- a/maths/allocation_number.py +++ b/maths/allocation_number.py @@ -4,6 +4,12 @@ def allocation_num(num, x) -> int: """ Divide the numbers into x parts. + + This algorithm can be used in multi thread Download. + For byte splitting. + For example: + for i in allocation_list: + requests.get(url,headers={'Range':f'bytes={i}'}) parameter ------------ @@ -24,7 +30,7 @@ def allocation_num(num, x) -> int: ... ValueError: param x can't past or be equal to the param num! - >>> allocation_num(888, 111) + >>> allocation_num(888, 999) Traceback (most recent call last): ... ValueError: param x can't past or be equal to the param num! @@ -46,7 +52,7 @@ def allocation_num(num, x) -> int: if __name__ == '__main__': - the_list = allocation_num(888,888) - print(the_list) + import doctest + doctest.testmod() From f89fd39be77a557e909255c8f8f730eee06ad26c Mon Sep 17 00:00:00 2001 From: wind-Lv <61381242+wind-Lv@users.noreply.github.com> Date: Fri, 20 Mar 2020 20:36:54 +0800 Subject: [PATCH 5/7] Update allocation_number.py --- maths/allocation_number.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/allocation_number.py b/maths/allocation_number.py index d1031a59ed78..ffdadb3e9fa4 100644 --- a/maths/allocation_number.py +++ b/maths/allocation_number.py @@ -1,7 +1,7 @@ #-*- coding:utf-8 -*- -def allocation_num(num, x) -> int: +def allocation_num(num, x) -> list: """ Divide the numbers into x parts. From 6a7333b7b299da235864ae8c29670e6e43031afa Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 20 Mar 2020 15:04:21 +0100 Subject: [PATCH 6/7] number_of_bytes and partitions --- maths/allocation_number.py | 41 +++++++++++++++----------------------- 1 file changed, 16 insertions(+), 25 deletions(-) diff --git a/maths/allocation_number.py b/maths/allocation_number.py index ffdadb3e9fa4..ed912f055b5d 100644 --- a/maths/allocation_number.py +++ b/maths/allocation_number.py @@ -1,58 +1,49 @@ -#-*- coding:utf-8 -*- +from typing import List -def allocation_num(num, x) -> list: +def allocation_num(number_of_bytes: int, partitions: int) -> List[str]: """ - Divide the numbers into x parts. + Divide a number of bytes into x partitions. - This algorithm can be used in multi thread Download. - For byte splitting. + 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 num: the number - : param x: number of copies + : param number_of_bytes + : param partitions return ------------ - : return: split completed list + : return: list of bytes to be assigned to each worker thread - example + Examples: ------------ >>> allocation_num(16647, 4) ['0-4161', '4162-8322', '8323-12483', '12484-16647'] - >>> allocation_num(888, 888) Traceback (most recent call last): ... ValueError: param x can't past or be equal to the param num! - >>> allocation_num(888, 999) Traceback (most recent call last): ... ValueError: param x can't past or be equal to the param num! """ - - - if x >= num: - raise ValueError('param x can\'t past or be equal to the param num!') - - shares = num // x - - allocation_list = [f'0-{shares*1}'] - for i in range(1,x-1): - length = f'{shares*i+1}-{shares*(i+1)}' + 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'{(shares*(x-1))+1}-{num}') - + allocation_list.append(f'{(bytes_per_partition * (x - 1)) + 1}-{number_of_bytes}') return allocation_list if __name__ == '__main__': import doctest doctest.testmod() - - From 9cff42cc5ad24eb99816fa3f0b83af6ebcd1e1a7 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 20 Mar 2020 15:19:14 +0100 Subject: [PATCH 7/7] Update allocation_number.py --- maths/allocation_number.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/maths/allocation_number.py b/maths/allocation_number.py index ed912f055b5d..04a8f1dac1f4 100644 --- a/maths/allocation_number.py +++ b/maths/allocation_number.py @@ -27,12 +27,18 @@ def allocation_num(number_of_bytes: int, partitions: int) -> List[str]: >>> allocation_num(888, 888) Traceback (most recent call last): ... - ValueError: param x can't past or be equal to the param num! + ValueError: partitions can not >= number_of_bytes! >>> allocation_num(888, 999) Traceback (most recent call last): ... - ValueError: param x can't past or be equal to the param num! + 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 @@ -40,7 +46,8 @@ def allocation_num(number_of_bytes: int, partitions: int) -> List[str]: 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 * (x - 1)) + 1}-{number_of_bytes}') + allocation_list.append(f'{(bytes_per_partition * (partitions - 1)) + 1}-' + f'{number_of_bytes}') return allocation_list