-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
40 lines (28 loc) · 934 Bytes
/
utils.py
File metadata and controls
40 lines (28 loc) · 934 Bytes
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
from __future__ import division
import math
def str_escape(string):
return "".join(
x for x in string if x.isalpha() or x.isdigit() or x == " "
)
def is_info_hash(string):
if string is not None:
try:
_ = int(string, 16)
return len(string) == 40
except ValueError:
return False
else:
return False
def get_files_size(files):
return sizeof_fmt(reduce(lambda r, e: r + e["length"], files, 0))
def sizeof_fmt(num, suffix="B"):
for unit in ["", "K", "M", "G", "T", "P", "E", "Z"]:
if abs(num) < 1024.0:
return "%3.1f %s%s" % (num, unit, suffix)
num /= 1024.0
return "%.1f %s%s" % (num, "Y", suffix)
def paginator(page, results_count, page_size=10):
if results_count > page_size:
return " (page {0}/{1})".format(page + 1, int(math.ceil(results_count / page_size)))
else:
return ""