Skip to content

Commit b064469

Browse files
Merge branch 'iam-veeramalla:main' into main
2 parents 025068f + 70f5215 commit b064469

13 files changed

+401
-24
lines changed

Day-11/01-dictionaries.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Dictionaries
2+
3+
## Overview:
4+
A dictionary in Python is a data structure that allows you to store and retrieve values using keys. It is also known as a hashmap or associative array in other programming languages. Dictionaries are implemented as hash tables, providing fast access to values based on their keys.
5+
6+
## Creating a Dictionary:
7+
```python
8+
my_dict = {'name': 'John', 'age': 25, 'city': 'New York'}
9+
```
10+
11+
## Accessing Values:
12+
```python
13+
print(my_dict['name']) # Output: John
14+
```
15+
16+
## Modifying and Adding Elements:
17+
```python
18+
my_dict['age'] = 26 # Modifying a value
19+
my_dict['occupation'] = 'Engineer' # Adding a new key-value pair
20+
```
21+
22+
## Removing Elements:
23+
```python
24+
del my_dict['city'] # Removing a key-value pair
25+
```
26+
27+
## Checking Key Existence:
28+
```python
29+
if 'age' in my_dict:
30+
print('Age is present in the dictionary')
31+
```
32+
33+
## Iterating Through Keys and Values:
34+
```python
35+
for key, value in my_dict.items():
36+
print(key, value)
37+
```

Day-11/02-sets.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Sets and Set Operations
2+
3+
#### Overview:
4+
A set in Python is an unordered collection of unique elements. It is useful for mathematical operations like union, intersection, and difference.
5+
6+
#### Creating a Set:
7+
```python
8+
my_set = {1, 2, 3, 4, 5}
9+
```
10+
11+
#### Adding and Removing Elements:
12+
```python
13+
my_set.add(6) # Adding an element
14+
my_set.remove(3) # Removing an element
15+
```
16+
17+
#### Set Operations:
18+
```python
19+
set1 = {1, 2, 3, 4}
20+
set2 = {3, 4, 5, 6}
21+
22+
union_set = set1.union(set2) # Union of sets
23+
intersection_set = set1.intersection(set2) # Intersection of sets
24+
difference_set = set1.difference(set2) # Difference of sets
25+
```
26+
27+
#### Subset and Superset:
28+
```python
29+
is_subset = set1.issubset(set2) # Checking if set1 is a subset of set2
30+
is_superset = set1.issuperset(set2) # Checking if set1 is a superset of set2
31+
```
32+
33+
### Practice Exercises and Examples
34+
35+
#### Example: Managing a Dictionary of Server Configurations and Optimizing Retrieval
36+
37+
##### Scenario:
38+
Suppose you are managing server configurations using a dictionary.
39+
40+
```python
41+
server_config = {
42+
'server1': {'ip': '192.168.1.1', 'port': 8080, 'status': 'active'},
43+
'server2': {'ip': '192.168.1.2', 'port': 8000, 'status': 'inactive'},
44+
'server3': {'ip': '192.168.1.3', 'port': 9000, 'status': 'active'}
45+
}
46+
```
47+
48+
##### Function for Retrieval:
49+
```python
50+
def get_server_status(server_name):
51+
return server_config.get(server_name, {}).get('status', 'Server not found')
52+
```
53+
54+
##### Example Usage:
55+
```python
56+
server_name = 'server2'
57+
status = get_server_status(server_name)
58+
print(f"{server_name} status: {status}")
59+
```
60+

Day-11/03-lists-vs-sets.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Lists vs. Sets
2+
3+
## Lists
4+
5+
- **Ordered Collection:**
6+
- Lists are ordered collections of elements. The order in which elements are added is preserved.
7+
- Elements can be accessed by their index.
8+
9+
```python
10+
my_list = [1, 2, 3, 4, 5]
11+
print(my_list[0]) # Output: 1
12+
```
13+
14+
- **Mutable:**
15+
- Lists are mutable, meaning you can modify their elements after creation.
16+
17+
```python
18+
my_list[1] = 10
19+
```
20+
21+
- **Allows Duplicate Elements:**
22+
- Lists can contain duplicate elements.
23+
24+
```python
25+
my_list = [1, 2, 2, 3, 4]
26+
```
27+
28+
- **Use Cases:**
29+
- Use lists when you need an ordered collection with the ability to modify elements.
30+
31+
## Sets
32+
33+
- **Unordered Collection:**
34+
- Sets are unordered collections of unique elements. The order in which elements are added is not preserved.
35+
- Elements cannot be accessed by their index.
36+
37+
```python
38+
my_set = {1, 2, 3, 4, 5}
39+
```
40+
41+
- **Mutable:**
42+
- Sets are mutable, meaning you can add and remove elements after creation.
43+
44+
```python
45+
my_set.add(6)
46+
```
47+
48+
- **No Duplicate Elements:**
49+
- Sets do not allow duplicate elements. If you try to add a duplicate, it won't raise an error, but the set won't change.
50+
51+
```python
52+
my_set = {1, 2, 2, 3, 4} # Results in {1, 2, 3, 4}
53+
```
54+
55+
- **Use Cases:**
56+
- Use sets when you need an unordered collection of unique elements, and you want to perform set operations like union, intersection, and difference.
57+
58+
### Common Operations:
59+
60+
- **Adding Elements:**
61+
- Lists use `append()` or `insert()` methods.
62+
- Sets use `add()` method.
63+
64+
- **Removing Elements:**
65+
- Lists use `remove()`, `pop()`, or `del` statement.
66+
- Sets use `remove()` or `discard()` methods.
67+
68+
- **Checking Membership:**
69+
- Lists use the `in` operator.
70+
- Sets use the `in` operator as well, which is more efficient for sets.
71+
72+
```python
73+
# Lists
74+
if 3 in my_list:
75+
print("3 is in the list")
76+
77+
# Sets
78+
if 3 in my_set:
79+
print("3 is in the set")
80+
```
81+
82+
### Choosing Between Lists and Sets
83+
84+
- **Use Lists When:**
85+
- You need to maintain the order of elements.
86+
- Duplicate elements are allowed.
87+
- You need to access elements by index.
88+
89+
- **Use Sets When:**
90+
- Order doesn't matter.
91+
- You want to ensure unique elements.
92+
- You need to perform set operations like union, intersection, or difference.

Day-11/04-demo-github-integration.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Program to demonstrate integration with GitHub to fetch the
2+
# details of Users who created Pull requests(Active) on Kubernetes Github repo.
3+
4+
import requests
5+
6+
# URL to fetch pull requests from the GitHub API
7+
url = f'https://api.github.com/repos/kubernetes/kubernetes/pulls'
8+
9+
# Make a GET request to fetch pull requests data from the GitHub API
10+
response = requests.get(url) # Add headers=headers inside get() for authentication
11+
12+
# Only if the response is successful
13+
if response.status_code == 200:
14+
# Convert the JSON response to a dictionary
15+
pull_requests = response.json()
16+
17+
# Create an empty dictionary to store PR creators and their counts
18+
pr_creators = {}
19+
20+
# Iterate through each pull request and extract the creator's name
21+
for pull in pull_requests:
22+
creator = pull['user']['login']
23+
if creator in pr_creators:
24+
pr_creators[creator] += 1
25+
else:
26+
pr_creators[creator] = 1
27+
28+
# Display the dictionary of PR creators and their counts
29+
print("PR Creators and Counts:")
30+
for creator, count in pr_creators.items():
31+
print(f"{creator}: {count} PR(s)")
32+
else:
33+
print(f"Failed to fetch data. Status code: {response.status_code}")

Day-11/04-practicals.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Practice Exercises and Examples
2+
3+
## Example: Managing a Dictionary of Server Configurations and Optimizing Retrieval
4+
5+
### Scenario:
6+
Suppose you are managing server configurations using a dictionary.
7+
8+
```python
9+
server_config = {
10+
'server1': {'ip': '192.168.1.1', 'port': 8080, 'status': 'active'},
11+
'server2': {'ip': '192.168.1.2', 'port': 8000, 'status': 'inactive'},
12+
'server3': {'ip': '192.168.1.3', 'port': 9000, 'status': 'active'}
13+
}
14+
```
15+
16+
### Function for Retrieval:
17+
```python
18+
def get_server_status(server_name):
19+
return server_config.get(server_name, {}).get('status', 'Server not found')
20+
```
21+
22+
### Example Usage:
23+
```python
24+
server_name = 'server2'
25+
status = get_server_status(server_name)
26+
print(f"{server_name} status: {status}")
27+
```
28+
29+
In this example, the function `get_server_status` optimizes the retrieval of the server status by using the `get` method and providing a default value if the server name is not found.

Day-11/04-practicals.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Server configurations dictionary
2+
server_config = {
3+
'server1': {'ip': '192.168.1.1', 'port': 8080, 'status': 'active'},
4+
'server2': {'ip': '192.168.1.2', 'port': 8000, 'status': 'inactive'},
5+
'server3': {'ip': '192.168.1.3', 'port': 9000, 'status': 'active'}
6+
}
7+
8+
# Retrieving information
9+
def get_server_status(server_name):
10+
return server_config.get(server_name, {}).get('status', 'Server not found')
11+
12+
# Example usage
13+
server_name = 'server2'
14+
status = get_server_status(server_name)
15+
print(f"{server_name} status: {status}")

Day-11/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Dictionaries and Sets

Day-12/README.md

Whitespace-only changes.

Day-12/server.conf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Server Configuration File
2+
3+
# Network Settings
4+
PORT = 8080
5+
MAX_CONNECTIONS=600
6+
TIMEOUT = 30
7+
8+
# Security Settings
9+
SSL_ENABLED = true
10+
SSL_CERT = /path/to/certificate.pem
11+
12+
# Logging Settings
13+
LOG_LEVEL = INFO
14+
LOG_FILE = /var/log/server.log
15+
16+
# Other Settings
17+
ENABLE_FEATURE_X = true

Day-12/update_server.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
def update_server_config(file_path, key, value):
2+
# Read the existing content of the server configuration file
3+
with open(file_path, 'r') as file:
4+
lines = file.readlines()
5+
6+
# Update the configuration value for the specified key
7+
with open(file_path, 'w') as file:
8+
for line in lines:
9+
# Check if the line starts with the specified key
10+
if key in line:
11+
# Update the line with the new value
12+
file.write(key + "=" + value + "\n")
13+
else:
14+
# Keep the existing line as it is
15+
file.write(line)
16+
17+
# Path to the server configuration file
18+
server_config_file = 'server.conf'
19+
20+
# Key and new value for updating the server configuration
21+
key_to_update = 'MAX_CONNECTIONS'
22+
new_value = '600' # New maximum connections allowed
23+
24+
# Update the server configuration file
25+
update_server_config(server_config_file, key_to_update, new_value)

0 commit comments

Comments
 (0)