-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
57 lines (35 loc) · 915 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import re
from urllib.parse import urlparse
def remove_special(s):
return re.sub(r"[?|:*/\\<>\"]+", '', s)
def flatten(lst):
return [a for b in lst for a in b]
def intersect(a, b):
for i in a:
if i in b:
return True
return False
def word_count(s):
return s.count(' ') + s.count('\n') + 1
def freq(lst):
tbl = {}
for i in lst:
tbl[i] = tbl.get(i, 0) + 1
return tbl
def sort_dict(d):
return sorted(d.items(), key=lambda l: l[1], reverse=True)
def is_absolute(url) -> bool:
return bool(urlparse(url).netloc)
def identity(*args):
if len(args) == 1:
return args[0]
return args
def sluggify(s):
return re.sub(r"[\W]+", '-', s.lower(), flags=re.ASCII)
def deep_flatten(lst):
if not isinstance(lst, list):
return [lst]
ret = []
for i in lst:
ret.extend(deep_flatten(i))
return ret