Home >Backend Development >Python Tutorial >ython Projects to Kickstart Python Learning
Get Started with DevOps Automation Using Python Scripts
Welcome to the World of DevOps and Python
It’s a common myth that DevOps engineers don’t code, but the truth is far from it. DevOps engineers often rely on programming skills to automate processes, manage infrastructure, and simplify workflows. Python and Go are two of the most favored languages in the DevOps world because of their versatility and ease of use.
Today, we’ll embark on an exciting journey to create three Python projects that not only introduce you to the crux of programming but also help you build meaningful, practical applications.
Here’s what we’ll explore:
So, are you ready to dive into this journey? Let’s get started!
Before we dive into coding, let’s ensure you have everything you need to get started. These projects are beginner-friendly, but having the following tools and skills in place will make the process smoother:
1. Basic Python Knowledge
Understanding variables, functions, loops, and how to work with libraries will be helpful. Don’t worry if you’re new — these projects will reinforce your learning!
2. Python Environment
Make sure Python is installed on your system. You can download it from python.org. A code editor like VS Code or PyCharm is also recommended.
3. API Fundamentals
You’ll work with APIs in all three projects. Familiarity with making HTTP requests and handling JSON responses is a bonus, but I’ll guide you through each step.
4. Tools to Install
Once you’re ready with these pre-requisites, we can jump into our first project — building a weather program!
Let’s dive into our first Python project — a Weather Program. This simple yet practical script will take a city name as input from the user and display its current weather. To achieve this, we’ll use the WeatherAPI to fetch real-time weather data in JSON format and display it in a user-friendly way.
How It Works:
Step-by-Step Instructions:
import requests def get_city_weather(city, api_key): city_name = city.lower() url = f"http://api.weatherapi.com/v1/current.json?key={api_key}&q={city_name}" response = requests.get(url) if response.status_code == 200: data = response.json() description = data['current']['condition']['text'] temp = data['current']['temp_c'] wind = data['current']['wind_kph'] print(f"\nWeather in {city.capitalize()}:") print(f"Temperature: {temp}°C") print(f"Wind Speed: {wind} Km/hr") print(f"Condition: {description.capitalize()}") else: print(f"{city_name} not found.") def main(): city = input("Enter Your City: ") API_KEY = "<Your_API_KEY>" get_city_weather(city, API_KEY) if __name__ == "__main__": main()
Set Up Your API Key:
Copy the API key and replace in the code with the actual key.
Run the Script:
Example Output:
Enter Your City: Ganganagar Weather in Ganganagar: Temperature: 24°C Wind Speed: 10 Km/hr Condition: Clear
And that’s it! You’ve successfully created your first Python project. It’s a simple yet powerful way to see how APIs work in real-world applications.
Now, let’s build a GitHub Pull Request (PR) Tracker. This project leverages the GitHub API to fetch details about pull requests for a specific repository. We’ll filter the data to extract the usernames of PR creators, count the number of PRs each creator has made, and display this information.
How It Works:
Step-by-Step Instructions:
import requests def get_city_weather(city, api_key): city_name = city.lower() url = f"http://api.weatherapi.com/v1/current.json?key={api_key}&q={city_name}" response = requests.get(url) if response.status_code == 200: data = response.json() description = data['current']['condition']['text'] temp = data['current']['temp_c'] wind = data['current']['wind_kph'] print(f"\nWeather in {city.capitalize()}:") print(f"Temperature: {temp}°C") print(f"Wind Speed: {wind} Km/hr") print(f"Condition: {description.capitalize()}") else: print(f"{city_name} not found.") def main(): city = input("Enter Your City: ") API_KEY = "<Your_API_KEY>" get_city_weather(city, API_KEY) if __name__ == "__main__": main()
Run the Script:
Expected Output:
When the script runs successfully, it fetches details of pull requests from the argoproj/argo-cd repository. This will showcase the following output:
Enter Your City: Ganganagar Weather in Ganganagar: Temperature: 24°C Wind Speed: 10 Km/hr Condition: Clear
Use Cases:
And there you have it! A functional script that fetches live data from GitHub and processes it for real-world insights.
Our final project is a gem — a script that integrates Jenkins and Slack to automate build notifications. This Python script triggers a Jenkins pipeline, monitors its status, and sends a notification to your Slack channel when the pipeline is complete.
How It Works:
Step-by-Step Instructions:
Create a Python File:
Create a file named jenkins-slack-integration.py in your code editor.
Insert the Following Code:
import requests url = 'https://api.github.com/repos/argoproj/argo-cd/pulls' response = requests.get(url) if response.status_code == 200: pull_requests = response.json() pr_creators = {} for pull in pull_requests: creator = pull['user']['login'] if creator in pr_creators: pr_creators[creator] += 1 else: pr_creators[creator] = 1 print(f"PR Creator counts: {pr_creators}") for creator, count in pr_creators.items(): print(f"Creator: {creator}: {count} PRs") else: print(f"Failed to make connection. Status Code: {response.status_code}")
Set Up Jenkins:
Create a pipeline project in Jenkins named Jenkins-Python-pipeline.
Add the following Hello World pipeline script:
PR Creator counts: {'dependabot[bot]': 7, 'devopsjedi': 1, 'aali309': 3, 'adriananeci': 1, 'amine7536': 1, 'lf32': 1, 'OpenGuidou': 1, 'ivan-cai': 1, 'surajyadav1108': 2, 'vasilegroza': 1, 'toyamagu-2021': 1, 'dvcanton': 1, 'vivian-xu': 1, 'rahulbollisetty': 1, 'blakepettersson': 1, 'dacofr': 1, 'mrysavy': 1, 'damsien': 1, 'lsq645599166': 1, 'jpbelangerupgrade': 1, 'Aaron-9900': 1} Creator: dependabot[bot]: 7 PRs Creator: devopsjedi: 1 PRs Creator: aali309: 3 PRs Creator: adriananeci: 1 PRs Creator: amine7536: 1 PRs Creator: lf32: 1 PRs Creator: OpenGuidou: 1 PRs Creator: ivan-cai: 1 PRs Creator: surajyadav1108: 2 PRs Creator: vasilegroza: 1 PRs Creator: toyamagu-2021: 1 PRs Creator: dvcanton: 1 PRs Creator: vivian-xu: 1 PRs Creator: rahulbollisetty: 1 PRs Creator: blakepettersson: 1 PRs Creator: dacofr: 1 PRs Creator: mrysavy: 1 PRs Creator: damsien: 1 PRs Creator: lsq645599166: 1 PRs Creator: jpbelangerupgrade: 1 PRs Creator: Aaron-9900: 1 PRs # The details will vary accroding to the time when you run the script.
Generate a Jenkins API token:
Set Up Slack:
Run the Script:
Example Output in Slack:
Pipeline Built Successfully with status: **SUCCESS**
This project is a fantastic example of how Python can bridge the gap between CI/CD tools and communication platforms, automating notifications and improving collaboration.
Congratulations on completing these three exciting Python projects! Each project was designed to teach you how Python can be used in real-world scenarios:
These projects are just the tip of the iceberg. As you explore further, you’ll see how Python’s versatility makes it a must-have skill for any DevOps engineer. Beyond coding, it enables automation, enhances productivity, and bridges the gap between complex workflows and user-friendly solutions.
Keep building, experimenting, and learning — this is the essence of both Python and DevOps! Remember, the best way to master programming is by doing.
Thank you for joining me on this journey! If you enjoyed this blog, feel free to share it with your friends and fellow learners.
? For more informative blog, Follow me on Hashnode, X(Twitter) and LinkedIn.
Till then, Happy Coding!!
Happy Learning! ?
The above is the detailed content of ython Projects to Kickstart Python Learning. For more information, please follow other related articles on the PHP Chinese website!