Home >Backend Development >Python Tutorial >ython Projects to Kickstart Python Learning

ython Projects to Kickstart Python Learning

Patricia Arquette
Patricia ArquetteOriginal
2024-12-21 06:27:09214browse

Get Started with DevOps Automation Using Python Scripts

? Introduction

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:

  • Weather Program — A program that takes a city name as input from the user and displays its current weather. (We’ll use the WeatherAPI to fetch data in JSON format — sounds exciting, right?)
  • GitHub Pull Request Tracker — A real-time project to list users who have made pull requests to a GitHub repository using GitHub’s API.
  • SlackBot for Jenkins Pipeline — A marvel of a project that triggers a Jenkins pipeline and delivers its status to your Slack channel via a custom SlackBot.

So, are you ready to dive into this journey? Let’s get started!


? Pre-Requisites

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

  • requests library for making API calls. Install it using pip install requests.
  • Slack API credentials and setup for the third project. Don’t worry; I’ll provide detailed instructions when we get there.
  • GitHub account for the second project and access to Jenkins for the third project.

Once you’re ready with these pre-requisites, we can jump into our first project — building a weather program!


? Project 1: 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:

  • The script prompts the user to enter the name of a city.
  • Using the WeatherAPI and an API key, the script fetches the weather data for the specified city.
  • The JSON response from the API is parsed to extract information like temperature, wind speed, and weather conditions.
  • The results are displayed in a neat, readable format.

Step-by-Step Instructions:

  • Create a Python File:
    • Open your preferred code editor (VS Code or PyCharm is recommended) and create a new file named weather.py.
    • Insert the Following Code:
 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:

    • Visit WeatherAPI and create a free account.
    • After signing in, generate your API key by selecting the Free Plan.
  • Copy the API key and replace in the code with the actual key.

  • Run the Script:

    • Open a terminal in the folder where you saved weather.py.
    • Run the script using python weather.py.
    • Enter a city name (e.g., “Ganganagar”) when prompted. The script will display the current weather conditions for that city.

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.


? Project 2: GitHub Pull Request Tracker

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:

  • The script makes an HTTP GET request to the GitHub API for pull request details of a repository.
  • It parses the JSON response to extract usernames of PR creators.
  • These usernames are stored in a dictionary, with the count of PRs they’ve created.
  • The program outputs the dictionary and a detailed list of PR creators with their respective counts.

Step-by-Step Instructions:

  • Create a Python File:
    • Open your code editor and create a new file named gb_tracker.py.
    • Insert the Following Code:
 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:

  • Open a terminal in the folder containing gb_tracker.py.
  • Run the script using python gb_tracker.py.

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:

  • Dev Environment: Use it to monitor PR activity in a repository and keep track of contributors.
  • Collaboration: Identify frequent contributors and their level of activity for better team management.

And there you have it! A functional script that fetches live data from GitHub and processes it for real-world insights.


? Project 3: SlackBot for Jenkins Pipeline

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:

  • Trigger Pipeline: The script starts by triggering a Jenkins pipeline.
  • Monitor Build Status: It checks the status of the pipeline at regular intervals until the build completes.
  • Send Slack Notification: Once the build is complete, the script uses a Slack bot to notify the status in a designated Slack channel.

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:

  • Go to your Jenkins user settings.

ython Projects to Kickstart Python Learning

  • Create a new token and copy it.

ython Projects to Kickstart Python Learning

  • Replace in the script with this token.

Set Up Slack:

  • Create a Slack channel named #devops-updates.
  • Create a bot named demo and invite it to the channel.
  • Generate a bot token and replace in the script with this token.

Run the Script:

  • Before running, Make sure you have added the bot in the slack channel using invite@demo in the #devops-updates.
  • Execute the script using python jenkins-slack-integration.py.
  • Once the pipeline completes, the bot will post a message in the Slack channel with the pipeline status.

Example Output in Slack:

ython Projects to Kickstart Python Learning

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.


? Conclusion

Congratulations on completing these three exciting Python projects! Each project was designed to teach you how Python can be used in real-world scenarios:

  • Weather Program: Showed you how to use APIs and handle JSON data to fetch and display meaningful information.
  • GitHub PR Tracker: Taught you how to interact with GitHub’s API and work with live data in a structured format.
  • SlackBot for Jenkins Pipeline: Demonstrated Python’s power to automate DevOps tasks and enhance team communication with seamless integrations.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn