From 0853ddb5216813eccb699ac8456ea1dbc2101b2d Mon Sep 17 00:00:00 2001 From: dzhav Date: Sun, 26 Nov 2023 15:31:23 +0300 Subject: [PATCH 1/8] parser --- parser/parser.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 parser/parser.py diff --git a/parser/parser.py b/parser/parser.py new file mode 100644 index 0000000..f3d1031 --- /dev/null +++ b/parser/parser.py @@ -0,0 +1,45 @@ +from urllib.parse import urlparse, unquote +import re + + +url1 = "main/accounts//" +url2 = "main/accounts/10/John?name=John&age=25" + +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(url1, url2): + url1 = unquote(url1) + parsed_url1 = urlparse(url1) + url2 = unquote(url2) + parsed_url2 = urlparse(url2) + + path1 = parsed_url1.path + path2 = parsed_url2.path + + path1_parts = path1.split("/") + path2_parts = path2.split("/") + + dictionary = {} + for i in range(len(path1_parts)): + if path1_parts[i].startswith("<") and path1_parts[i].endswith(">"): + if i < len(path2_parts): + dictionary.update({path1_parts[i].strip("<>"):path2_parts[i]}) + else: + dictionary.update({path1_parts[i].strip("<>"):None}) + return dictionary + + +print(parse_after_question_mark(url2)) +print(parse_between_quotes(url1, url2)) +# {'name': 'John', 'age': '25'} +# {'id': '10', 'name': 'John'} From b28d67c9a35d66246ac6d3c035c5037cdd3ef8f5 Mon Sep 17 00:00:00 2001 From: dzhav Date: Sun, 26 Nov 2023 15:36:03 +0300 Subject: [PATCH 2/8] update parser --- parser/__init__.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 parser/__init__.py diff --git a/parser/__init__.py b/parser/__init__.py new file mode 100644 index 0000000..e69de29 From 07d63ddc50551718448af8cbd4b933d058c103a9 Mon Sep 17 00:00:00 2001 From: dzhav Date: Sun, 26 Nov 2023 15:40:29 +0300 Subject: [PATCH 3/8] parser --- parser/parser.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/parser/parser.py b/parser/parser.py index f3d1031..00c969a 100644 --- a/parser/parser.py +++ b/parser/parser.py @@ -1,10 +1,6 @@ from urllib.parse import urlparse, unquote -import re -url1 = "main/accounts//" -url2 = "main/accounts/10/John?name=John&age=25" - def parse_after_question_mark(url): url = unquote(url) parsed_url = urlparse(url) @@ -29,6 +25,7 @@ def parse_between_quotes(url1, url2): path1_parts = path1.split("/") path2_parts = path2.split("/") + dictionary = {} for i in range(len(path1_parts)): if path1_parts[i].startswith("<") and path1_parts[i].endswith(">"): @@ -39,7 +36,11 @@ def parse_between_quotes(url1, url2): return dictionary -print(parse_after_question_mark(url2)) -print(parse_between_quotes(url1, url2)) -# {'name': 'John', 'age': '25'} -# {'id': '10', 'name': 'John'} +if __name__ == "__main__": + url1 = "main/accounts//" + url2 = "main/accounts/10/John?name=John&age=25" + print(parse_after_question_mark(url2)) + print(parse_between_quotes(url1, url2)) + # {'name': 'John', 'age': '25'} + # {'id': '10', 'name': 'John'} + \ No newline at end of file From 786bc572bdef73838b9811333d1a2d0145cf36eb Mon Sep 17 00:00:00 2001 From: dzhav Date: Sun, 26 Nov 2023 16:03:48 +0300 Subject: [PATCH 4/8] parser --- parser/parser.py | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/parser/parser.py b/parser/parser.py index 00c969a..e513071 100644 --- a/parser/parser.py +++ b/parser/parser.py @@ -13,18 +13,17 @@ def parse_after_question_mark(url): return dictionary -def parse_between_quotes(url1, url2): - url1 = unquote(url1) - parsed_url1 = urlparse(url1) - url2 = unquote(url2) - parsed_url2 = urlparse(url2) +def parse_between_quotes(pattern, url): + url1 = unquote(pattern) + parsed_url1 = urlparse(pattern) + url2 = unquote(url) + parsed_url2 = urlparse(url) path1 = parsed_url1.path path2 = parsed_url2.path - path1_parts = path1.split("/") - path2_parts = path2.split("/") - + path1_parts = list(filter(None, path1.split("/"))) + path2_parts = list(filter(None, path2.split("/"))) dictionary = {} for i in range(len(path1_parts)): @@ -37,10 +36,9 @@ def parse_between_quotes(url1, url2): if __name__ == "__main__": - url1 = "main/accounts//" - url2 = "main/accounts/10/John?name=John&age=25" - print(parse_after_question_mark(url2)) - print(parse_between_quotes(url1, url2)) + pattern = "/main/accounts//" + 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'} - \ No newline at end of file From cc423f2a47d0457e61dc65be1c7a2883a8ef5bb3 Mon Sep 17 00:00:00 2001 From: dzhav Date: Sun, 26 Nov 2023 16:20:31 +0300 Subject: [PATCH 5/8] parser --- parser/parser.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/parser/parser.py b/parser/parser.py index e513071..ae620ce 100644 --- a/parser/parser.py +++ b/parser/parser.py @@ -15,9 +15,9 @@ def parse_after_question_mark(url): def parse_between_quotes(pattern, url): url1 = unquote(pattern) - parsed_url1 = urlparse(pattern) + parsed_url1 = urlparse(url1) url2 = unquote(url) - parsed_url2 = urlparse(url) + parsed_url2 = urlparse(url2) path1 = parsed_url1.path path2 = parsed_url2.path From 3b642c680a105f63fa669945ec16b96f416013be Mon Sep 17 00:00:00 2001 From: dzhav Date: Sun, 26 Nov 2023 16:48:14 +0300 Subject: [PATCH 6/8] pars --- parser/parser.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/parser/parser.py b/parser/parser.py index ae620ce..d4054da 100644 --- a/parser/parser.py +++ b/parser/parser.py @@ -28,15 +28,28 @@ def parse_between_quotes(pattern, url): dictionary = {} for i in range(len(path1_parts)): if path1_parts[i].startswith("<") and path1_parts[i].endswith(">"): - if i < len(path2_parts): - dictionary.update({path1_parts[i].strip("<>"):path2_parts[i]}) - else: - dictionary.update({path1_parts[i].strip("<>"):None}) + 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: + dictionary.update({sep[0]:None}) + except: + raise ValueError("Invalid format") + + + return dictionary if __name__ == "__main__": - pattern = "/main/accounts//" + pattern = "/main/accounts//" url = "/main/accounts/10/John?name=John&age=25" print(parse_after_question_mark(url)) print(parse_between_quotes(pattern, url)) From 6bd7ddbbc2e8eb05126089cd66ae35b5cfda9ee6 Mon Sep 17 00:00:00 2001 From: dzhav Date: Sun, 26 Nov 2023 16:50:39 +0300 Subject: [PATCH 7/8] pars --- parser/parser.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/parser/parser.py b/parser/parser.py index d4054da..74d994f 100644 --- a/parser/parser.py +++ b/parser/parser.py @@ -43,15 +43,13 @@ def parse_between_quotes(pattern, url): except: raise ValueError("Invalid format") - - return dictionary if __name__ == "__main__": - pattern = "/main/accounts//" + pattern = "/main/accounts//" 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'} + # {'id': 10, 'name': 'John'} From 1482db5bd3117aa6d7fdeb1c159c1e89b6d23d87 Mon Sep 17 00:00:00 2001 From: dzhav Date: Sun, 26 Nov 2023 16:57:37 +0300 Subject: [PATCH 8/8] parser --- parser/parser.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/parser/parser.py b/parser/parser.py index 74d994f..54a0e0a 100644 --- a/parser/parser.py +++ b/parser/parser.py @@ -38,6 +38,8 @@ def parse_between_quotes(pattern, url): 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: @@ -47,7 +49,7 @@ def parse_between_quotes(pattern, url): if __name__ == "__main__": - pattern = "/main/accounts//" + pattern = "/main/accounts//" url = "/main/accounts/10/John?name=John&age=25" print(parse_after_question_mark(url)) print(parse_between_quotes(pattern, url))