Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 137 additions & 0 deletions 4b-dictionary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#!/usr/bin/python3

# Added for testing
# Added for testing line 2
# 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'))

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 = [{
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'
}
}
}]
Comment on lines +101 to +112
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incorrect data structure for dictionary update

The new_emp variable is incorrectly defined as a list containing a dictionary instead of just a dictionary. This will cause a TypeError when calling emp_dict.update(new_emp) since update() expects a dictionary, not a list.

Code suggestion
Check the AI-generated fix before applying
Suggested change
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'
}
}
}]
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'
}
}
}

Code Review Run #37b90a


Should Bito avoid suggestions like this for future reviews? (Manage Rules)

  • Yes, avoid them


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)
24 changes: 24 additions & 0 deletions 7-regex-examples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import re

urls = ["https://www.facebook.com","https://www.google.com","https://www.amazon.in"]

def checkValidURL(url):
url_reg_ex = r"^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?"
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)
Comment on lines +12 to +14
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fragile URL parsing with chained splits

The parseDomain function has fragile string parsing that will fail for URLs without 'www' or with different domain structures. Use proper URL parsing library like urllib.parse instead of chained string splits.

Code suggestion
Check the AI-generated fix before applying
Suggested change
def parseDomain(url):
domain = url.split("//")[1].split("www")[1].split(".")[1]
print(domain)
from urllib.parse import urlparse
def parseDomain(url):
parsed_url = urlparse(url)
domain = parsed_url.netloc.replace('www.', '').split('.')[0]
print(domain)

Code Review Run #37b90a


Should Bito avoid suggestions like this for future reviews? (Manage Rules)

  • Yes, avoid them



if __name__ == "__main__":
for url in urls:
url_status = checkValidURL(url)
if url_status:
try:
parseDomain(url)
except (ValueError, IndexError) as e:
print(f"Error parsing domain for {url}: {e}")
Comment on lines +23 to +24

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing indentation in except block

There's a syntax error on line 24 due to missing indentation in the except block. The print statement should be indented to be part of the exception handler.

Code suggestion
Check the AI-generated fix before applying
Suggested change
except (ValueError, IndexError) as e:
print(f"Error parsing domain for {url}: {e}")
except (ValueError, IndexError) as e:
print(f"Error parsing domain for {url}: {e}")

Code Review Run #b1d35b


Should Bito avoid suggestions like this for future reviews? (Manage Rules)

  • Yes, avoid them

3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# Python-examples
# Python-examples
Utility functions and scripts