Skip to content

Instantly share code, notes, and snippets.

@Hellowlol
Created September 4, 2016 08:56
Show Gist options
  • Save Hellowlol/ddd3c77fe72927b3bafe67d3a209f580 to your computer and use it in GitHub Desktop.
Save Hellowlol/ddd3c77fe72927b3bafe67d3a209f580 to your computer and use it in GitHub Desktop.
calc chunk
import math
def convert_size(size, ):
if (size == 0):
return '0B'
size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
i = int(math.floor(math.log(size,1024)))
p = math.pow(1024,i)
s = round(size/p,2)
return '%s %s' % (s,size_name[i])
def sizeof(num):
for x in ['bytes', 'KB', 'MB', 'GB', 'TB']:
if num < 1000.0:
return '%3.2f %s' % (num, x)
num /= 1000.0
return '%3.2f %s' % (num, 'TB')
def comp(size, mode='auto', min_piece_number=1000, max_piece_number=1800, min_piece_length=None, max_piece_length=16777216):
"""
0-250MB: 256KB (262144 bytes)
250-1024MB: 1MB (1048576 bytes)
1-5GB: 2MB (2097152 bytes)
5-20GB: 4MB (4194304 bytes)
20-40GB: 8MB (8388608 bytes)
40 GB+: 16MB (16777216 bytes)
"""
#if self.piece:
# return self.piece # user should trump everything
# this is to mb, remove it
size = int(size * 1024 * 1024)
if mode == 'auto':
# add more sizes
if size <= 262144000: # 250mb
piece = 262144 # 256 kb
elif size <= 1073741824: # 1024 mb
piece = 1048576 # 1mb
elif size <= 5368709120: # 5120 mb
piece = 2097152 # 2 mb
elif size <= 21474836480: # 20480 mb
piece = 8388608 # 8 mb
elif size >= 41943040000: # 40960 mb
piece = 16777216 # 16 mb
return convert_size(piece), int(size / piece)
elif mode == 'calc':
piece = 16384
while (size / piece) > 1024 and piece < (16777216): # max 16 mb
piece *= 2
num_pieces = size / piece
if size % piece:
num_pieces += 1
return convert_size(piece), int(num_pieces)
elif mode == 'beta':
block = 16384
if min_piece_length:
piece = min_piece_length
else:
piece = 16384
while True:
n_pieces = (size / piece)
# Stop at optimal size or there isnt any enough pieces
if n_pieces >= min_piece_number and n_pieces <= max_piece_number or n_pieces < 1024:
break
# User is da boss^^
elif max_piece_length and max_piece_length <= piece:
break
else:
# Larger faster plx
if (size / (piece * 2) > 1024):
piece *= 2
else:
piece += block
# Make sure we cover them all..
num_pieces = size / piece
if size % piece:
num_pieces += 1
return convert_size(piece), int(num_pieces)
#z = math.sqrt(size)
#return num_piece,
#print convert_size(x)
#print sizeof(x)
#print convert_size(z)
l = [
(0.01, 0),
(7, 0),
(75, 0),
(125, 0),
(250, 262144),
(1024, 1048576),
(5120, 2097152),
(20480, 4194304),
(40960, 8388608),
]
def mb_to_byte(size, chunk, *args):
b = int(size * 1024 * 1024)
c = b / chunk
return "mb %s bytes %s chunk %s " % (size, b, c)
#print mb_to_byte(40000)
for t in l:
print 'auto', comp(t[0], mode='auto'), 'calc', comp(t[0], mode='calc'), 'beta', comp(t[0], mode='beta')
#print 'beta', comp(t[0], mode='beta')
#print 1000000 # corrent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment