-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
57 lines (46 loc) · 1.82 KB
/
parser.py
File metadata and controls
57 lines (46 loc) · 1.82 KB
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
from urllib.parse import urlparse, unquote
def parse_after_question_mark(url):
url = unquote(url)
parsed_url = urlparse(url)
params = parsed_url.query.split("&")
dictionary = {}
for param in params:
if param != '':
dictionary.update({param.split("=")[0] : param.split("=")[1]})
return dictionary
def parse_between_quotes(pattern, url):
url1 = unquote(pattern)
parsed_url1 = urlparse(url1)
url2 = unquote(url)
parsed_url2 = urlparse(url2)
path1 = parsed_url1.path
path2 = parsed_url2.path
path1_parts = list(filter(None, path1.split("/")))
path2_parts = list(filter(None, path2.split("/")))
dictionary = {}
for i in range(len(path1_parts)):
if path1_parts[i].startswith("<") and path1_parts[i].endswith(">"):
sep = path1_parts[i][1:-1].split(":")
try:
if i < len(path2_parts):
if sep[0] == path1_parts[i][1:-1]:
dictionary.update({sep[0]:str(path2_parts[i])})
else:
if sep[1] == "str":
dictionary.update({sep[0]:str(path2_parts[i])})
elif sep[1] == "int":
dictionary.update({sep[0]:int(path2_parts[i])})
else:
raise ValueError("Invalid format")
else:
dictionary.update({sep[0]:None})
except:
raise ValueError("Invalid format")
return dictionary
if __name__ == "__main__":
pattern = "/main/accounts/<id:int>/<name:str>"
url = "/main/accounts/10/John?name=John&age=25"
print(parse_after_question_mark(url))
print(parse_between_quotes(pattern, url))
# {'name': 'John', 'age': '25'}
# {'id': 10, 'name': 'John'}