Introduction to Senior-Level Python Scripts
As a beginner-to-intermediate Python developer, you're likely looking for ways to improve your skills and create more complex, useful projects. One way to do this is by learning from and utilizing scripts that demonstrate senior-level concepts and techniques. In this article, we'll explore six Python scripts that will help you look like a senior developer, covering topics such as file automation, API wrappers, data processing, and more.
1. File Automation Script
Let's start with a simple yet powerful script that automates file organization. This script will create separate folders for different file types and move the corresponding files into those folders.
import os
import shutil
# Define the folder path and file types
folder_path = '/path/to/your/folder'
file_types = {
'Documents': ['.txt', '.docx', '.pdf'],
'Images': ['.jpg', '.png', '.gif'],
'Videos': ['.mp4', '.avi', '.mkv']
}
# Create folders for each file type
for folder_name, extensions in file_types.items():
folder = os.path.join(folder_path, folder_name)
if not os.path.exists(folder):
os.makedirs(folder)
# Move files to their corresponding folders
for filename in os.listdir(folder_path):
file_path = os.path.join(folder_path, filename)
if os.path.isfile(file_path):
for folder_name, extensions in file_types.items():
if os.path.splitext(filename)[1].lower() in extensions:
shutil.move(file_path, os.path.join(folder_path, folder_name, filename))
break
What makes this script senior-level is its ability to dynamically create folders and move files based on their extensions. This demonstrates a good understanding of file system operations and automation.
2. API Wrapper Script
Next, let's look at a script that wraps the GitHub API to retrieve a user's repositories. This script will handle API rate limiting and pagination.
import requests
import json
# Define the API endpoint and authentication token
endpoint = 'https://api.github.com/users/{username}/repos'
token = 'your_github_token'
# Define the function to retrieve repositories
def get_repositories(username):
headers = {'Authorization': f'token {token}'}
params = {'per_page': 100}
repositories = []
page = 1
while True:
params['page'] = page
response = requests.get(endpoint.format(username=username), headers=headers, params=params)
if response.status_code == 200:
repositories.extend(response.json())
if len(response.json()) < params['per_page']:
break
page += 1
else:
break
return repositories
# Example usage
username = 'your_github_username'
repositories = get_repositories(username)
print(json.dumps(repositories, indent=4))
What makes this script senior-level is its ability to handle API rate limiting and pagination, which requires a good understanding of API design and usage.
3. Data Processing Script
Now, let's look at a script that processes a CSV file and generates statistics about the data.
import pandas as pd
import numpy as np
# Load the CSV file
df = pd.read_csv('data.csv')
# Generate statistics about the data
mean = df.mean()
median = df.median()
std_dev = df.std()
min_val = df.min()
max_val = df.max()
# Print the statistics
print('Mean:', mean)
print('Median:', median)
print('Standard Deviation:', std_dev)
print('Minimum:', min_val)
print('Maximum:', max_val)
# Example usage
# Generate a histogram of the data
import matplotlib.pyplot as plt
df.hist(bins=50)
plt.show()
What makes this script senior-level is its ability to generate complex statistics about the data and visualize the results using a histogram.
4. CLI Tool Script
Next, let's look at a script that creates a simple CLI tool for managing to-do lists.
import argparse
import json
# Define the CLI arguments
parser = argparse.ArgumentParser(description='To-Do List CLI Tool')
parser.add_argument('--add', help='Add a new task')
parser.add_argument('--list', action='store_true', help='List all tasks')
parser.add_argument('--delete', help='Delete a task')
# Load the to-do list from file
try:
with open('todo.json', 'r') as f:
todo_list = json.load(f)
except FileNotFoundError:
todo_list = []
# Define the functions for each CLI command
def add_task(task):
todo_list.append(task)
with open('todo.json', 'w') as f:
json.dump(todo_list, f)
def list_tasks():
print('To-Do List:')
for i, task in enumerate(todo_list):
print(f'{i+1}. {task}')
def delete_task(task_number):
try:
task_number = int(task_number) - 1
if task_number < 0 or task_number >= len(todo_list):
print('Invalid task number')
else:
del todo_list[task_number]
with open('todo.json', 'w') as f:
json.dump(todo_list, f)
except ValueError:
print('Invalid task number')
# Parse the CLI arguments and execute the corresponding function
args = parser.parse_args()
if args.add:
add_task(args.add)
elif args.list:
list_tasks()
elif args.delete:
delete_task(args.delete)
What makes this script senior-level is its ability to create a CLI tool with multiple commands and options, which requires a good understanding of argument parsing and command-line interface design.
5. Web Scraping Helper Script
Now, let's look at a script that helps with web scraping by extracting all links from a webpage.
import requests
from bs4 import BeautifulSoup
# Define the URL of the webpage
url = 'https://www.example.com'
# Send a GET request to the webpage
response = requests.get(url)
# Parse the HTML content using BeautifulSoup
soup = BeautifulSoup(response.content, 'html.parser')
# Extract all links from the webpage
links = []
for link in soup.find_all('a'):
href = link.get('href')
if href:
links.append(href)
# Print the extracted links
print('Extracted Links:')
for link in links:
print(link)
What makes this script senior-level is its ability to extract complex data from a webpage, which requires a good understanding of HTML parsing and web scraping techniques.
6. System Utility Script
Finally, let's look at a script that monitors system resources such as CPU usage, memory usage, and disk usage.
import psutil
# Define the function to monitor system resources
def monitor_system_resources():
cpu_usage = psutil.cpu_percent()
memory_usage = psutil.virtual_memory().percent
disk_usage = psutil.disk_usage('/').percent
print(f'CPU Usage: {cpu_usage}%')
print(f'Memory Usage: {memory_usage}%')
print(f'Disk Usage: {disk_usage}%')
# Example usage
monitor_system_resources()
What makes this script senior-level is its ability to monitor complex system resources, which requires a good understanding of system administration and resource monitoring.
Conclusion
In this article, we've explored six Python scripts that demonstrate senior-level concepts and techniques. These scripts cover a range of topics, from file automation and API wrappers to data processing, CLI tools, web scraping helpers, and system utilities. By studying and using these scripts, you'll be able to improve your Python skills and create more complex, useful projects. If you want to learn more about Python and software development, be sure to follow me for more articles and tutorials. Happy coding!
Found this useful? Follow me on Dev.to for more Python automation tips every week. Drop a comment below — I reply to every one!
Top comments (0)