-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathALTutils.py
114 lines (98 loc) · 2.98 KB
/
ALTutils.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
""" UTILS """
def capitalize_first_letter(s):
"""
Capitilizes the first letter of each word in a given sentence.
Parameters
----------
s : str
The sentence we want to capitalize.
Returns
-------
s : str
The modified sentence.
"""
lst = [word[0].upper() + word[1:] for word in s.split()]
s = " ".join(lst)
return s
def add_left_zero(s):
"""
Adds '0' to the left of a string.
Parameters
----------
s : str
The string we want to modify.
Returns
-------
: str
The string with a '0' concatentated to its left.
"""
if len(s) == 1:
return '0' + s
else:
return s
def urlDecode(input_str):
"""
URL decodes a given string
Parameters
----------
input_str : str
The string to be decoded
Returns
-------
: str
The URL decoded string
"""
input_str = input_str.replace("%20", " ")
input_str = input_str.replace("+", " ")
input_str = input_str.replace("%21", "!")
input_str = input_str.replace("%22", "\"")
input_str = input_str.replace("%23", "#")
input_str = input_str.replace("%24", "$")
input_str = input_str.replace("%25", "%")
input_str = input_str.replace("%26", "&")
input_str = input_str.replace("%27", "\'")
input_str = input_str.replace("%28", "(")
input_str = input_str.replace("%29", ")")
input_str = input_str.replace("%30", "*")
input_str = input_str.replace("%31", "+")
input_str = input_str.replace("%2C", ",")
input_str = input_str.replace("%2E", ".")
input_str = input_str.replace("%2F", "/")
input_str = input_str.replace("%2C", ",")
input_str = input_str.replace("%3A", ":")
input_str = input_str.replace("%3A", "")
input_str = input_str.replace("%3C", "<")
input_str = input_str.replace("%3D", "=")
input_str = input_str.replace("%3E", ">")
input_str = input_str.replace("%3F", "?")
input_str = input_str.replace("%40", "@")
input_str = input_str.replace("%5B", "[")
input_str = input_str.replace("%5C", "\\")
input_str = input_str.replace("%5D", "]")
input_str = input_str.replace("%5E", "^")
input_str = input_str.replace("%5F", "-")
input_str = input_str.replace("%60", "`")
return input_str
def extract_string(s, start):
"""
Extracts the substring between the last occurrence of start and the first subsequent whitespace character
Parameters
----------
s : str
The string which will be searched
start : str
Starting location
Returns
-------
The matching string, else None is returned
"""
index = s.rfind(start)
if index == -1:
return None
index += len(start)
end_index = len(s)
for i in range(index, len(s)):
if s[i].isspace():
end_index = i
break
return s[index:end_index]