-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
40 lines (32 loc) · 947 Bytes
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# coding=utf-8
import os
import hashlib
from functools import partial
from config import UPLOAD_FOLDER
HERE = os.path.abspath(os.path.dirname(__file__))
here = os.path.abspath(os.path.abspath(__file__))
def get_file_md5(f, chunk_size=8192):
h = hashlib.md5()
while True:
chunk = f.read(chunk_size)
if not chunk:
break
h.update(chunk)
return h.hexdigest()
def humanize_bytes(bytesize, precision=2):
abbrevs = (
(1 << 50, 'PB'),
(1 << 40, 'TB'),
(1 << 30, 'GB'),
(1 << 20, 'MB'),
(1 << 10, 'kB'),
(1, 'bytes')
)
if bytesize == 1:
return '1 byte'
for factor, suffix in abbrevs:
if bytesize >= factor:
break
# return '%.*f %s' % (precision, bytesize / factor, suffix)
return '{1:.{0}f} {2}'.format(precision, bytesize / factor, suffix)
get_file_path = partial(os.path.join, HERE, UPLOAD_FOLDER)