From 1c76a8dca5bccd6b4a851e2356a084325533868f Mon Sep 17 00:00:00 2001 From: aashishchauhan06 <32122638+aashishchauhan06@users.noreply.github.com> Date: Thu, 20 Jun 2024 12:10:39 +0530 Subject: [PATCH 01/16] Create 4b-dictionary.py --- 4b-dictionary.py | 133 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 4b-dictionary.py diff --git a/4b-dictionary.py b/4b-dictionary.py new file mode 100644 index 0000000..f990691 --- /dev/null +++ b/4b-dictionary.py @@ -0,0 +1,133 @@ +#!/usr/bin/python3 + +# Examples for dictionary +# Empty Dictionary +emp_dict = {} + +# dictionary with integer keys +emp_dict = {100: 'Sanjeev', 101: 'Jassi'} +print(emp_dict) + +# dictionary with mixed keys +emp_dict = {100: 'Sanjeev', 'skills': ['Python', 'AWS']} +print(emp_dict) + +# using dict() function +emp_dict = dict({100: 'Sanjeev', 101: 'Jassi'}) +print(emp_dict) + +# Let's create emp_dict of this format +''' +{ + 'Employee ID': + { Name: 'string' + Joined: 'yyyy-mm-dd' + Title: 'string' + Skills: [‘list’, ‘of’, ‘skills’] + Project: {‘project_name’: ‘project description’} + } +} +''' + +# Initializing Employee +emp_dict = { + 100: + { + 'name': "Sanjeev", + 'joined': "2017-08-14", + 'title': "Cloud Security Engineer", + 'skills': ['Python', 'AWS', 'AppSec'], + 'projects': { + 'CSPM implementation': 'Implement Cloud Security Posture for AWS' + } + }, + 101: + { + 'name': "Jassi", + 'joined': "2017-10-27", + 'title': "Cloud Security Manager", + 'skills': ['Python', 'AWS', 'AWS Security'], + 'projects': { + 'CSPM implementation': 'Implement Cloud Security Posture for AWS and Azure' + } + } +} + +print(emp_dict) + +# Get the type of emp_dict +print(type(emp_dict)) + +# get keys of a dictionary using keys() +emp_ids = emp_dict.keys() +print(emp_ids) + +# Get values of a dictionary using values() +emp_details = emp_dict.values() +print(emp_details) + +# get key and value both using items() +emps = emp_dict.items() +print(emps) + +# Length of a dictionary (number of items) using len() +print(len(emp_dict)) + +# Iterate through a dictionary +for id in emp_dict.keys(): + print(f"Employee ID: {id}") + print(f"\tEmployee Details: {emp_dict[id]}") + +# Accessing elements from dictionary +# get vs [] for retrieving elements +# Sanjeev +print(emp_dict[100]['name']) + +# ['Python', 'AWS', 'AppSec'] +print(emp_dict[101].get('skills')) + +# Try to access keys which doesn't exist +# None +print(emp_dict[100].get('mailid')) + +# KeyError: 'location' Comment below line to execute other lines below +# print(emp_dict[101]['location']) + +# Add one more employee to emp_dict using update() +new_emp = { + 102: + { + 'name': "Rakesh", + 'joined': "2018-01-07", + 'title': "Business Analyst", + 'skills': ['Power BI', 'MBA', 'Marketing Expert'], + 'projects': { + 'Flexmind Marketing': 'Increase the membership my targeted marketing' + } + } +} + +emp_dict.update(new_emp) +print(emp_dict) + +# Update the existing value. Update title of emp_id: 100 as "Sr. Cloud Security Engineer" +emp_dict[100]['title'] = "Sr. Cloud Security Engineer" +print(emp_dict[100]) + +# Learn to delete +# pop, clear, del +# Pop employee id 101 +emp_dict.pop(101, "Not found") +print(len(emp_dict)) + +# Delete the employee 102 +del emp_dict[102] +print(len(emp_dict)) + +# Clear the dictionary +emp_dict.clear() +print(emp_dict) + +# Delete the dictionary itself and printing it would throw an error +del emp_dict +# print(emp_dict) From 715dc063cdbb535ca0c6463224bf89ab33105b31 Mon Sep 17 00:00:00 2001 From: aashishchauhan06 <32122638+aashishchauhan06@users.noreply.github.com> Date: Thu, 20 Jun 2024 12:11:01 +0530 Subject: [PATCH 02/16] Create 7-regex-examples.py --- 7-regex-examples.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 7-regex-examples.py diff --git a/7-regex-examples.py b/7-regex-examples.py new file mode 100644 index 0000000..aff3757 --- /dev/null +++ b/7-regex-examples.py @@ -0,0 +1,21 @@ +import re + +urls = ["https://www.facebook.com","https://www.google.com","https://www.amazon.in"] + +def checkValidURL(url): + url_reg_ex = "^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?" + data = re.search(url_reg_ex,url) + if data is not None: + return True + return False + +def parseDomain(url): + domain = url.split("//")[1].split("www")[1].split(".")[1] + print(domain) + + +if __name__ == "__main__": + for url in urls: + url_status = checkValidURL(url) + if url_status: + parseDomain(url) From 260bf4918ff9a3bd294ee7e52c1ae2d973891297 Mon Sep 17 00:00:00 2001 From: aashishchauhan06 <32122638+aashishchauhan06@users.noreply.github.com> Date: Thu, 20 Jun 2024 17:47:17 +0530 Subject: [PATCH 03/16] Update 4b-dictionary.py --- 4b-dictionary.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/4b-dictionary.py b/4b-dictionary.py index f990691..863c687 100644 --- a/4b-dictionary.py +++ b/4b-dictionary.py @@ -1,3 +1,4 @@ +''' #!/usr/bin/python3 # Examples for dictionary @@ -131,3 +132,4 @@ # Delete the dictionary itself and printing it would throw an error del emp_dict # print(emp_dict) +''' From 285fb2301453dedea594ab24d04b2a080007efcf Mon Sep 17 00:00:00 2001 From: aashishchauhan06 <32122638+aashishchauhan06@users.noreply.github.com> Date: Thu, 20 Jun 2024 17:48:47 +0530 Subject: [PATCH 04/16] Update 4b-dictionary.py --- 4b-dictionary.py | 134 ----------------------------------------------- 1 file changed, 134 deletions(-) diff --git a/4b-dictionary.py b/4b-dictionary.py index 863c687..8b13789 100644 --- a/4b-dictionary.py +++ b/4b-dictionary.py @@ -1,135 +1 @@ -''' -#!/usr/bin/python3 -# Examples for dictionary -# Empty Dictionary -emp_dict = {} - -# dictionary with integer keys -emp_dict = {100: 'Sanjeev', 101: 'Jassi'} -print(emp_dict) - -# dictionary with mixed keys -emp_dict = {100: 'Sanjeev', 'skills': ['Python', 'AWS']} -print(emp_dict) - -# using dict() function -emp_dict = dict({100: 'Sanjeev', 101: 'Jassi'}) -print(emp_dict) - -# Let's create emp_dict of this format -''' -{ - 'Employee ID': - { Name: 'string' - Joined: 'yyyy-mm-dd' - Title: 'string' - Skills: [‘list’, ‘of’, ‘skills’] - Project: {‘project_name’: ‘project description’} - } -} -''' - -# Initializing Employee -emp_dict = { - 100: - { - 'name': "Sanjeev", - 'joined': "2017-08-14", - 'title': "Cloud Security Engineer", - 'skills': ['Python', 'AWS', 'AppSec'], - 'projects': { - 'CSPM implementation': 'Implement Cloud Security Posture for AWS' - } - }, - 101: - { - 'name': "Jassi", - 'joined': "2017-10-27", - 'title': "Cloud Security Manager", - 'skills': ['Python', 'AWS', 'AWS Security'], - 'projects': { - 'CSPM implementation': 'Implement Cloud Security Posture for AWS and Azure' - } - } -} - -print(emp_dict) - -# Get the type of emp_dict -print(type(emp_dict)) - -# get keys of a dictionary using keys() -emp_ids = emp_dict.keys() -print(emp_ids) - -# Get values of a dictionary using values() -emp_details = emp_dict.values() -print(emp_details) - -# get key and value both using items() -emps = emp_dict.items() -print(emps) - -# Length of a dictionary (number of items) using len() -print(len(emp_dict)) - -# Iterate through a dictionary -for id in emp_dict.keys(): - print(f"Employee ID: {id}") - print(f"\tEmployee Details: {emp_dict[id]}") - -# Accessing elements from dictionary -# get vs [] for retrieving elements -# Sanjeev -print(emp_dict[100]['name']) - -# ['Python', 'AWS', 'AppSec'] -print(emp_dict[101].get('skills')) - -# Try to access keys which doesn't exist -# None -print(emp_dict[100].get('mailid')) - -# KeyError: 'location' Comment below line to execute other lines below -# print(emp_dict[101]['location']) - -# Add one more employee to emp_dict using update() -new_emp = { - 102: - { - 'name': "Rakesh", - 'joined': "2018-01-07", - 'title': "Business Analyst", - 'skills': ['Power BI', 'MBA', 'Marketing Expert'], - 'projects': { - 'Flexmind Marketing': 'Increase the membership my targeted marketing' - } - } -} - -emp_dict.update(new_emp) -print(emp_dict) - -# Update the existing value. Update title of emp_id: 100 as "Sr. Cloud Security Engineer" -emp_dict[100]['title'] = "Sr. Cloud Security Engineer" -print(emp_dict[100]) - -# Learn to delete -# pop, clear, del -# Pop employee id 101 -emp_dict.pop(101, "Not found") -print(len(emp_dict)) - -# Delete the employee 102 -del emp_dict[102] -print(len(emp_dict)) - -# Clear the dictionary -emp_dict.clear() -print(emp_dict) - -# Delete the dictionary itself and printing it would throw an error -del emp_dict -# print(emp_dict) -''' From 3ade6da9cd006ef8a155823edf35c3cf25d57d12 Mon Sep 17 00:00:00 2001 From: aashishchauhan06 <32122638+aashishchauhan06@users.noreply.github.com> Date: Thu, 20 Jun 2024 17:49:04 +0530 Subject: [PATCH 05/16] Delete 4b-dictionary.py --- 4b-dictionary.py | 1 - 1 file changed, 1 deletion(-) delete mode 100644 4b-dictionary.py diff --git a/4b-dictionary.py b/4b-dictionary.py deleted file mode 100644 index 8b13789..0000000 --- a/4b-dictionary.py +++ /dev/null @@ -1 +0,0 @@ - From 2d9854b205d44e1c4bc9cc5079059a2424b81883 Mon Sep 17 00:00:00 2001 From: aashishchauhan06 <32122638+aashishchauhan06@users.noreply.github.com> Date: Thu, 20 Jun 2024 22:15:01 +0530 Subject: [PATCH 06/16] Create 4b-dictionary.py --- 4b-dictionary.py | 133 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 4b-dictionary.py diff --git a/4b-dictionary.py b/4b-dictionary.py new file mode 100644 index 0000000..f990691 --- /dev/null +++ b/4b-dictionary.py @@ -0,0 +1,133 @@ +#!/usr/bin/python3 + +# Examples for dictionary +# Empty Dictionary +emp_dict = {} + +# dictionary with integer keys +emp_dict = {100: 'Sanjeev', 101: 'Jassi'} +print(emp_dict) + +# dictionary with mixed keys +emp_dict = {100: 'Sanjeev', 'skills': ['Python', 'AWS']} +print(emp_dict) + +# using dict() function +emp_dict = dict({100: 'Sanjeev', 101: 'Jassi'}) +print(emp_dict) + +# Let's create emp_dict of this format +''' +{ + 'Employee ID': + { Name: 'string' + Joined: 'yyyy-mm-dd' + Title: 'string' + Skills: [‘list’, ‘of’, ‘skills’] + Project: {‘project_name’: ‘project description’} + } +} +''' + +# Initializing Employee +emp_dict = { + 100: + { + 'name': "Sanjeev", + 'joined': "2017-08-14", + 'title': "Cloud Security Engineer", + 'skills': ['Python', 'AWS', 'AppSec'], + 'projects': { + 'CSPM implementation': 'Implement Cloud Security Posture for AWS' + } + }, + 101: + { + 'name': "Jassi", + 'joined': "2017-10-27", + 'title': "Cloud Security Manager", + 'skills': ['Python', 'AWS', 'AWS Security'], + 'projects': { + 'CSPM implementation': 'Implement Cloud Security Posture for AWS and Azure' + } + } +} + +print(emp_dict) + +# Get the type of emp_dict +print(type(emp_dict)) + +# get keys of a dictionary using keys() +emp_ids = emp_dict.keys() +print(emp_ids) + +# Get values of a dictionary using values() +emp_details = emp_dict.values() +print(emp_details) + +# get key and value both using items() +emps = emp_dict.items() +print(emps) + +# Length of a dictionary (number of items) using len() +print(len(emp_dict)) + +# Iterate through a dictionary +for id in emp_dict.keys(): + print(f"Employee ID: {id}") + print(f"\tEmployee Details: {emp_dict[id]}") + +# Accessing elements from dictionary +# get vs [] for retrieving elements +# Sanjeev +print(emp_dict[100]['name']) + +# ['Python', 'AWS', 'AppSec'] +print(emp_dict[101].get('skills')) + +# Try to access keys which doesn't exist +# None +print(emp_dict[100].get('mailid')) + +# KeyError: 'location' Comment below line to execute other lines below +# print(emp_dict[101]['location']) + +# Add one more employee to emp_dict using update() +new_emp = { + 102: + { + 'name': "Rakesh", + 'joined': "2018-01-07", + 'title': "Business Analyst", + 'skills': ['Power BI', 'MBA', 'Marketing Expert'], + 'projects': { + 'Flexmind Marketing': 'Increase the membership my targeted marketing' + } + } +} + +emp_dict.update(new_emp) +print(emp_dict) + +# Update the existing value. Update title of emp_id: 100 as "Sr. Cloud Security Engineer" +emp_dict[100]['title'] = "Sr. Cloud Security Engineer" +print(emp_dict[100]) + +# Learn to delete +# pop, clear, del +# Pop employee id 101 +emp_dict.pop(101, "Not found") +print(len(emp_dict)) + +# Delete the employee 102 +del emp_dict[102] +print(len(emp_dict)) + +# Clear the dictionary +emp_dict.clear() +print(emp_dict) + +# Delete the dictionary itself and printing it would throw an error +del emp_dict +# print(emp_dict) From ae348596802014d1f7cbf21ed7f075d456da0747 Mon Sep 17 00:00:00 2001 From: aashishchauhan06 <32122638+aashishchauhan06@users.noreply.github.com> Date: Mon, 8 Jul 2024 15:09:48 +0530 Subject: [PATCH 07/16] Update 4b-dictionary.py --- 4b-dictionary.py | 1 + 1 file changed, 1 insertion(+) diff --git a/4b-dictionary.py b/4b-dictionary.py index f990691..5f58e5e 100644 --- a/4b-dictionary.py +++ b/4b-dictionary.py @@ -1,5 +1,6 @@ #!/usr/bin/python3 +# Added for testing # Examples for dictionary # Empty Dictionary emp_dict = {} From a5a7698a7bb2986ec35c710b224f3928babffd57 Mon Sep 17 00:00:00 2001 From: aashishchauhan06 <32122638+aashishchauhan06@users.noreply.github.com> Date: Mon, 8 Jul 2024 15:25:42 +0530 Subject: [PATCH 08/16] Update 4b-dictionary.py --- 4b-dictionary.py | 1 + 1 file changed, 1 insertion(+) diff --git a/4b-dictionary.py b/4b-dictionary.py index 5f58e5e..fadce6d 100644 --- a/4b-dictionary.py +++ b/4b-dictionary.py @@ -1,6 +1,7 @@ #!/usr/bin/python3 # Added for testing +# Added for testing line 2 # Examples for dictionary # Empty Dictionary emp_dict = {} From 044e7d9b6d13b82f8029cd0c35fc9944398c6552 Mon Sep 17 00:00:00 2001 From: aashishchauhan06 <32122638+aashishchauhan06@users.noreply.github.com> Date: Fri, 20 Dec 2024 23:29:06 +0530 Subject: [PATCH 09/16] Update 7-regex-examples.py --- 7-regex-examples.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/7-regex-examples.py b/7-regex-examples.py index aff3757..49c6a52 100644 --- a/7-regex-examples.py +++ b/7-regex-examples.py @@ -15,7 +15,10 @@ def parseDomain(url): if __name__ == "__main__": - for url in urls: - url_status = checkValidURL(url) - if url_status: - parseDomain(url) + for url in urls: + url_status = checkValidURL(url) + if url_status: + try: + parseDomain(url) + except Exception as e: + print(f"Error parsing domain for {url}: {e}") From d215cb3a1583e7289d44e465cd9950491de5822c Mon Sep 17 00:00:00 2001 From: aashishchauhan06 <32122638+aashishchauhan06@users.noreply.github.com> Date: Fri, 20 Dec 2024 23:34:18 +0530 Subject: [PATCH 10/16] Update 7-regex-examples.py --- 7-regex-examples.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/7-regex-examples.py b/7-regex-examples.py index 49c6a52..3daab87 100644 --- a/7-regex-examples.py +++ b/7-regex-examples.py @@ -3,7 +3,7 @@ urls = ["https://www.facebook.com","https://www.google.com","https://www.amazon.in"] def checkValidURL(url): - url_reg_ex = "^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?" + url_reg_ex = r"^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?" data = re.search(url_reg_ex,url) if data is not None: return True From e2617e50c5678fe17b98f0289ec1a7b111d00ec1 Mon Sep 17 00:00:00 2001 From: aashishchauhan06 <32122638+aashishchauhan06@users.noreply.github.com> Date: Fri, 20 Dec 2024 23:35:07 +0530 Subject: [PATCH 11/16] Update 4b-dictionary.py --- 4b-dictionary.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/4b-dictionary.py b/4b-dictionary.py index fadce6d..766c58d 100644 --- a/4b-dictionary.py +++ b/4b-dictionary.py @@ -92,8 +92,10 @@ # None print(emp_dict[100].get('mailid')) -# KeyError: 'location' Comment below line to execute other lines below -# print(emp_dict[101]['location']) +try: + print(emp_dict[101]['location']) +except KeyError: + print("Location not found for employee 101") # Add one more employee to emp_dict using update() new_emp = { From da89f1aaec265c83eb9f6c299b20b7b92cf8614a Mon Sep 17 00:00:00 2001 From: aashishchauhan06 <32122638+aashishchauhan06@users.noreply.github.com> Date: Tue, 24 Dec 2024 13:39:21 +0530 Subject: [PATCH 12/16] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 975ccf5..fdffe7a 100644 --- a/README.md +++ b/README.md @@ -1 +1,2 @@ -# Python-examples \ No newline at end of file +# Python-examples +Utility functions From d2f998a19c3fd3c4ec6fc1e0d4441d814204eb6c Mon Sep 17 00:00:00 2001 From: aashishchauhan06 <32122638+aashishchauhan06@users.noreply.github.com> Date: Tue, 31 Dec 2024 17:44:10 +0530 Subject: [PATCH 13/16] Update 4b-dictionary.py --- 4b-dictionary.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/4b-dictionary.py b/4b-dictionary.py index 766c58d..bb94f2a 100644 --- a/4b-dictionary.py +++ b/4b-dictionary.py @@ -98,7 +98,7 @@ print("Location not found for employee 101") # Add one more employee to emp_dict using update() -new_emp = { +new_emp = [{ 102: { 'name': "Rakesh", @@ -109,7 +109,7 @@ 'Flexmind Marketing': 'Increase the membership my targeted marketing' } } -} +}] emp_dict.update(new_emp) print(emp_dict) From 8e614da15350ccf7604dfb761a24c28f9fd2c5f3 Mon Sep 17 00:00:00 2001 From: aashishchauhan06 <32122638+aashishchauhan06@users.noreply.github.com> Date: Fri, 17 Jan 2025 22:43:54 +0530 Subject: [PATCH 14/16] Update 7-regex-examples.py --- 7-regex-examples.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/7-regex-examples.py b/7-regex-examples.py index 3daab87..be33eed 100644 --- a/7-regex-examples.py +++ b/7-regex-examples.py @@ -20,5 +20,5 @@ def parseDomain(url): if url_status: try: parseDomain(url) - except Exception as e: - print(f"Error parsing domain for {url}: {e}") + except (ValueError, IndexError) as e: + print(f"Error parsing domain for {url}: {e}") From fa8ef06a083a95f4c38e45d9489ab64213986181 Mon Sep 17 00:00:00 2001 From: aashishchauhan06 <32122638+aashishchauhan06@users.noreply.github.com> Date: Tue, 21 Jan 2025 17:05:36 +0530 Subject: [PATCH 15/16] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index fdffe7a..b18da26 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ # Python-examples -Utility functions +Utility functions and scripts From 37903450ddb25232c4c1af71e52986579c411919 Mon Sep 17 00:00:00 2001 From: aashishchauhan06 <32122638+aashishchauhan06@users.noreply.github.com> Date: Tue, 1 Apr 2025 14:29:22 +0530 Subject: [PATCH 16/16] Update 7-regex-examples.py --- 7-regex-examples.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/7-regex-examples.py b/7-regex-examples.py index be33eed..49e5af1 100644 --- a/7-regex-examples.py +++ b/7-regex-examples.py @@ -21,4 +21,4 @@ def parseDomain(url): try: parseDomain(url) except (ValueError, IndexError) as e: - print(f"Error parsing domain for {url}: {e}") + print(f"Error parsing domain for {url}: {e}")